Sub topic 1
Directed Graphs and Mutual Reachability
In a directed graph, every edge has a direction, so a path from u to v tells you nothing about a path back. This chapter sets up the idea that the rest of the tutorial builds on. Two vertices are mutually reachable when each one can reach the other.
Prerequisites
This chapter assumes you already know the following:
- Directed graphs stored as adjacency lists, where
adj[u]lists the verticesuhas edges to. - Depth-first search, in either its recursive or its stack-based form.
- Big-O notation.
- How to read basic Python.
Reachability Is One-Way
Vertex v is reachable from u if some directed path leads from u to v. Every vertex reaches itself through the empty path of length zero. Reachability is transitive: if u reaches v and v reaches w, then u reaches w. It is not symmetric, though. An edge 2 → 3 lets you go from 2 to 3. It gives no way to return.
The graph below is the running example for the whole tutorial.
Vertex 0 reaches 3 through 0 → 1 → 2 → 3. No edge leaves the group {3, 4, 5}, so vertex 3 can never get back to 0.
Testing Mutual Reachability
The direct way to test mutual reachability is to run one search from each endpoint:
def reachable(adj, s):
"""Return the set of vertices reachable from s."""
seen = {s}
stack = [s]
while stack:
u = stack.pop()
for v in adj[u]:
if v not in seen:
seen.add(v)
stack.append(v)
return seen
def mutually_reachable(adj, u, v):
return v in reachable(adj, u) and u in reachable(adj, v)
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(sorted(reachable(adj, 0))) # [0, 1, 2, 3, 4, 5]
print(sorted(reachable(adj, 3))) # [3, 4, 5]
print(mutually_reachable(adj, 0, 2)) # True
print(mutually_reachable(adj, 0, 3)) # False
print(mutually_reachable(adj, 6, 7)) # TrueEach call to reachable takes O(V + E) time. That cost is fine for a single pair of vertices. Grouping every vertex this way takes O(V · (V + E)), and on large graphs that is far too slow. The algorithms later in this tutorial find all the groups in a single O(V + E) pass.
Mutual Reachability Is an Equivalence Relation
Mutual reachability has three properties:
| Property | Why it holds |
|---|---|
| Reflexive | Every vertex reaches itself by the empty path. |
| Symmetric | The definition requires paths in both directions. |
| Transitive | Joining paths `u → v → w` and `w → v → u` gives paths both ways between `u` and `w`. |
Because the relation has all three properties, it splits the vertices into disjoint classes. In the example, the classes are {0, 1, 2}, {3, 4, 5}, and {6, 7}. The next chapter names these classes and studies them.
Common Errors
- Treating reachability as symmetric. A path
u → vsays nothing about a pathv → u. - Ignoring isolated or acyclic vertices. Each of these vertices forms a class of size one.
- Following edges backwards by accident. If you build the adjacency list from
(u, v)pairs asadj[v].append(u), you get the reversed graph and wrong answers.
Exercise
In the example graph, which vertices can reach vertex 6, and is vertex 5 mutually reachable with vertex 4?
Show answer
Only vertices 6 and 7 can reach 6, because the only edge into 6 comes from 7, and the only edge into 7 comes from 6. Vertex 5 is mutually reachable with 4: the path 4 → 5 goes one way, and 5 → 3 → 4 goes back.
Exercise
Why does running reachable() from every vertex to group the whole graph cost O(V · (V + E))?
Show answer
Each search can visit every vertex and edge, which costs O(V + E), and you run one search per vertex, so the total is V searches times O(V + E).