PythiloLesson 23 of 32 · progress kept in this browser

Count connected groups with depth-first search

Depth-first search follows one path as far as it goes before backing up. Started from every unvisited node, it splits an undirected graph into connected components, such as friend circles or separate road networks.

stack = ["a"]
seen = {"a"}
node = stack.pop()
# push a's unseen neighbours next
Your task

Write count_components(graph) for an undirected adjacency dict (every edge listed both ways). Return the number of connected components.