Count Unreachable Pairs of Nodes in an Undirected Graph
Size every connected component, then subtract reachable pairs from the total
Two nodes are unreachable from each other exactly when they land in different connected components, so this never requires checking pairs directly. Find the size of every component with a BFS or DFS, then the count of REACHABLE pairs is just size*(size-1)/2 added up per component. Subtracting that sum from the total pairs among all n nodes, n*(n-1)/2, leaves exactly the unreachable pairs — one arithmetic pass after the components are known.
Details
Animated on: 7 nodes labeled 0..6 with undirected edges 0-2, 0-5, 2-4, 1-6, 5-4 — how many pairs of different nodes are unreachable from each other?
Problem:Count Unreachable Pairs of Nodes in an Undirected GraphMedium
Size every connected component, then subtract reachable pairs from the total
step 1 / 16
Undirected graph, n nodes
7 nodes and 5 undirected edges. A pair of nodes is unreachable exactly when they sit in different connected components, so the whole problem is: find the components, then count cross-component pairs.
1FUNCTION countPairs(n, edges):2 BUILD adjacency FROM edges3 FOR EACH node FROM 0 TO n - 14 IF node NOT visited5 size <- BFS(node, adjacency, visited)6 APPEND size TO sizes7 total <- n * (n - 1) / 28 reachable <- SUM(s * (s - 1) / 2 FOR s IN sizes)9 RETURN total - reachable
← / → step · space play · Home restart
Every step in words
Show all 16 steps
- 7 nodes and 5 undirected edges. A pair of nodes is unreachable exactly when they sit in different connected components, so the whole problem is: find the components, then count cross-component pairs.
- Node 0 has not been visited yet, so it starts a fresh component. BFS from it will reach everything connected to it.
- Visit 0: neighbors 2 and 5 were unvisited, so they join the queue. Component so far: {0}.
- Visit 2: neighbor 4 was unvisited, so it joins the queue. Component so far: {0,2}.
- Visit 5: every neighbor is already visited, nothing new joins the queue. Component so far: {0,2,5}.
- Visit 4: every neighbor is already visited, nothing new joins the queue. Component so far: {0,2,5,4}.
- Component {0,2,5,4} is exhausted — size 4. All 4*3/2 = 6 pairs inside it are reachable from each other.
- Node 1 has not been visited yet, so it starts a fresh component (separate from {0,2,5,4}). BFS from it will reach everything connected to it.
- Visit 1: neighbor 6 was unvisited, so it joins the queue. Component so far: {1}.
- Visit 6: every neighbor is already visited, nothing new joins the queue. Component so far: {1,6}.
- Component {1,6} is exhausted — size 2. All 2*1/2 = 1 pairs inside it are reachable from each other.
- Node 3 has not been visited yet, so it starts a fresh component (separate from {0,2,5,4,1,6}). BFS from it will reach everything connected to it.
- Visit 3: every neighbor is already visited, nothing new joins the queue. Component so far: {3}.
- Component {3} is exhausted — size 1. All 1*0/2 = 0 pairs inside it are reachable from each other.
- Component sizes: [4, 2, 1]. Total pairs among 7 nodes is 7*6/2 = 21. Reachable pairs sum to 4*3/2 + 2*1/2 + 1*0/2 = 7.
- Unreachable pairs = total - reachable = 21 - 7 = 14. RETURN 14.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.