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 nextThink before coding
An explicit stack avoids Python's recursion limit on long chains. Every node is pushed at most once because it is marked when pushed, so the whole count is linear in nodes plus edges. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write count_components(graph) for an undirected adjacency dict (every edge listed both ways). Return the number of connected components.
The explanation opens here once every check passes.