Sub topic 10
Connectivity When Edges Have Direction
This chapter assumes you can build an adjacency list, label components in an undirected graph with BFS or DFS, and read Big-O notation. Here we look at what changes when every edge points one way.
Reachability Is No Longer Symmetric
In an undirected graph, if you can walk from u to v, you can walk back the same way. That symmetry is why one traversal from any vertex finds that vertex's entire component.
A directed graph breaks this. An edge u -> v lets you move from u to v, but not back. Vertex v is reachable from u** if a path of edges, each followed in its own direction, leads from u to v. In a directed graph, v can be reachable from u while u is not reachable from v`.
Because of this, "connected" splits into two separate ideas:
| Term | Definition | Edge direction |
|---|---|---|
| Strongly connected component (SCC) | A maximal set of vertices where every vertex can reach every other | Respected |
| Weakly connected component | A maximal set of vertices that are connected once you ignore direction | Ignored |
Every SCC sits inside a single weak component. A weak component can contain many SCCs.
In this graph, vertex 0 can reach 3, but 3 cannot reach 0. All five vertices form one weak component, and they split into two SCCs: {0, 1, 2} and {3, 4}.
Seeing the Asymmetry in Code
The code below stores each edge only in the direction it points. It also builds a reversed adjacency list, radj, where every edge is flipped. A vertex's SCC is the set of vertices it can reach (forward search) that can also reach it back (search on the reversed graph).
from collections import deque
def reachable(adj, s):
seen = {s}
queue = deque([s])
while queue:
u = queue.popleft()
for v in adj[u]:
if v not in seen:
seen.add(v)
queue.append(v)
return seen
n = 5
edges = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4), (4, 3)]
adj = [[] for _ in range(n)]
radj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v) # forward direction only
radj[v].append(u) # reversed edge
def scc_of(v):
return reachable(adj, v) & reachable(radj, v)
print(sorted(reachable(adj, 0))) # [0, 1, 2, 3, 4]
print(sorted(reachable(adj, 3))) # [3, 4]
print(sorted(scc_of(0))) # [0, 1, 2]
print(sorted(scc_of(3))) # [3, 4]This method is correct, but it runs two full searches per vertex, which costs O(V · (V + E)) in total. The Kosaraju and Tarjan chapters find every SCC in O(V + E).
Common Errors
- Adding both directions by accident. If you write
adj[u].append(v)and alsoadj[v].append(u)for a directed graph, you have computed weak components. Do this only when weak components are what you need. - Treating "reachable from" as "connected to." One forward search from
sreturns everything downstream ofs. That set can include vertices that cannot get back tos. - Forgetting isolated vertices. A vertex with no edges is its own SCC and its own weak component. Loop over all
nvertices, not only the vertices that appear in the edge list.
Choosing Which Notion You Need
Use weak components when you are asking whether two things are related at all, such as which accounts belong to the same transaction network. Use SCCs when the question needs travel in both directions, such as finding groups of web pages that all link to one another, or cycles of mutually dependent modules.
Exercise
A directed graph has 4 vertices and edges (0, 1), (1, 2), (2, 1), (3, 2). List its strongly connected components and its weakly connected components.
Show answer
SCCs: {0}, {1, 2}, {3}. Only 1 and 2 can reach each other. Vertex 0 reaches 1 but nothing leads back to 0, and vertex 3 reaches 2 but nothing leads back to 3. Weak components: one component, {0, 1, 2, 3}, because ignoring direction connects every vertex.
Exercise
In the code above, what does reachable(radj, 3) return, and what does that set mean?
Show answer
It returns {0, 1, 2, 3, 4}, which is every vertex that can reach vertex 3 in the original graph. Intersecting it with reachable(adj, 3) = {3, 4} gives the SCC {3, 4}.