Sub topic 3
Tracking Components as Edges Arrive
This chapter assumes you already know the union-find class from the previous chapter, iterative DFS, and the difference between directed and undirected edges.
Tracking Components as Edges Arrive
BFS and DFS assume the whole graph exists before you start. When edges arrive one at a time, rerunning a traversal after each edge costs O(V + E) per update. Union-find handles each new edge with one union call in near-constant amortized time. It keeps a running count that drops by one whenever two different roots merge.
def incremental_counts(n, edges):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
count = n
history = []
for u, v in edges:
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv
count -= 1
history.append(count)
return history
print(incremental_counts(5, [(0, 1), (1, 2), (2, 0), (3, 4)]))
# [4, 3, 3, 2]Connectivity When Edges Have Direction
In a directed graph, the fact that u reaches v does not mean v reaches u. That gives two separate notions of a component.
| Kind | Definition | Typical method |
|---|---|---|
| Weakly connected | Connected once you ignore edge direction | BFS, DFS, or union-find on the undirected version |
| Strongly connected | Every vertex reaches every other along directed edges | Kosaraju or Tarjan |
Weakly Connected Components
To find weakly connected components, add every edge in both directions and run any undirected method from earlier chapters.
Strongly Connected Components with Kosaraju's Algorithm
Kosaraju's algorithm makes two passes:
- Run DFS on the original graph and record each vertex when it finishes.
- Reverse every edge. Then visit vertices in reverse finish order. Each traversal that starts from an unassigned vertex collects exactly one strongly connected component (SCC).
Both passes run in O(V + E).
Tarjan's Single-Pass Algorithm
Tarjan's algorithm finds SCCs with a single DFS and no reversed graph. It gives each vertex a discovery index and a "low-link", which is the smallest index reachable from that vertex's subtree through vertices still on the stack. When a vertex's low-link equals its own index, that vertex is the root of an SCC. The algorithm then pops the stack down to that vertex. Tarjan's algorithm is also O(V + E), but it uses less memory than Kosaraju's.
Using NetworkX for Connected Components
import networkx as nx
G = nx.Graph([(0, 1), (1, 2), (3, 4)])
G.add_node(5)
print([sorted(c) for c in nx.connected_components(G)])
# [[0, 1, 2], [3, 4], [5]]
D = nx.DiGraph([(0, 1), (1, 2), (2, 0), (2, 3)])
print(sorted(sorted(c) for c in nx.strongly_connected_components(D)))
# [[0, 1, 2], [3]]
print(sorted(sorted(c) for c in nx.weakly_connected_components(D)))
# [[0, 1, 2, 3]]Exercise
In the directed graph above, add the edge 3 → 0. How many strongly connected components are there now, and why?
Show answer
There is one. Vertex 3 can now reach 0, and 0 already reaches 3 through 1 and 2, so all four vertices reach each other.
Exercise
Edges stream in and you must report the component count after each one. Which method fits, and what does each update cost?
Show answer
Union-find fits. Each edge needs one union call, which runs in near-constant amortized time. Rerunning BFS after every edge would cost O(V + E) per update.