Sub topic 11
Weakly Connected Components
This chapter assumes you have read the earlier chapters on adjacency lists, BFS labeling, and union-find. It also assumes you know the difference between reachability in directed and undirected graphs, as covered in the previous chapter on directed connectivity.
The Working Definition
A directed graph is weakly connected if it would be connected after you replace every directed edge with an undirected one. A weakly connected component (WCC) is a maximal set of vertices that are linked to each other when direction is ignored.
Weak connectivity answers this question: "Do these vertices belong to the same piece of the graph at all?" It does not ask whether you can actually travel from one to the other along the arrows. That stricter question belongs to strongly connected components, which the next chapter covers.
In this graph, no vertex can reach 0 or 2, and vertex 1 cannot reach anything. Even so, vertices 0, 1, and 2 form a single weakly connected component, because the underlying undirected graph links them through vertex 1.
Computing WCCs: Symmetrize, Then Traverse
The standard method has two steps:
- Build an adjacency list that stores every edge in both directions.
- Run the same BFS labeling you already know.
from collections import deque
def weakly_connected_components(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u) # ignore direction: store the reverse edge too
label = [-1] * n
count = 0
for start in range(n):
if label[start] != -1:
continue
label[start] = count
queue = deque([start])
while queue:
u = queue.popleft()
for v in adj[u]:
if label[v] == -1:
label[v] = count
queue.append(v)
count += 1
return count, label
print(weakly_connected_components(6, [(0, 1), (2, 1), (3, 4)]))
# (3, [0, 0, 0, 1, 1, 2])The cost is the same as undirected labeling, O(V + E) time. Storing each edge twice makes the adjacency list hold 2E entries, which is still O(V + E) space.
Union-find also works with no changes at all. A call to union(u, v) has no notion of direction, so feeding it the directed edge list gives WCCs directly. This makes union-find the natural choice when directed edges arrive one at a time and you only need weak connectivity.
The Most Common Error: Following Only Out-Edges
A frequent bug is to build the adjacency list from the directed edges as they are and then run the usual labeling loop:
from collections import deque
def buggy_wcc(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v) # missing: adj[v].append(u)
label = [-1] * n
count = 0
for start in range(n):
if label[start] != -1:
continue
label[start] = count
queue = deque([start])
while queue:
u = queue.popleft()
for v in adj[u]:
if label[v] == -1:
label[v] = count
queue.append(v)
count += 1
return count, label
print(buggy_wcc(6, [(0, 1), (2, 1), (3, 4)]))
# (4, [0, 0, 1, 2, 2, 3])The search that starts at 0 reaches 1 and stops there. Later, vertex 2 is still unlabeled, so it gets a new label, even though it belongs with 0 and 1. The buggy output also depends on vertex order. Run the same graph with the vertices numbered differently and you can get a different count. That makes the bug hard to catch in tests.
How Weak and Strong Components Relate
| Property | Weakly connected component | Strongly connected component |
|---|---|---|
| Edge direction | Ignored | Respected |
| Requirement for u and v | Linked by a path in the undirected version | u reaches v and v reaches u |
| Algorithm | BFS/DFS on the symmetrized graph, or union-find | Kosaraju or Tarjan |
| Count in the example above | 3 | 6 |
Every strongly connected component lies entirely inside one weakly connected component. As a result, a graph never has more WCCs than SCCs. When the counts are equal, each weak piece is also strongly connected. In practice, people often compute WCCs first to split a large graph into independent pieces, then run SCC analysis on each piece separately.
Exercise
A directed graph has 5 vertices and edges 1→0, 1→2, 3→2, 4→4. List its weakly connected components, and say what the buggy out-edges-only version returns if it scans vertices in order 0 to 4.
Show answer
The weakly connected components are {0, 1, 2, 3} and {4}, so there are 2. The buggy version labels 0 alone (0 has no out-edges). It then labels 1 and 2 together, from 1 which reaches 0 (already labeled) and 2. Next it labels 3 alone, because 2 is already labeled, and finally 4 alone. It reports 4 components: labels [0, 1, 1, 2, 3].
Exercise
Why can union-find compute weakly connected components from a directed edge list without any modification?
Show answer
union(u, v) merges the sets containing u and v, and the result is the same as union(v, u). The operation is symmetric, so it ignores edge direction, which is exactly what weak connectivity requires.