Sub topic 6
Reversing Every Edge: The Transpose Graph
This chapter assumes you can read Python, store a directed graph as an adjacency list, and follow what a strongly connected component (SCC) and the condensation DAG are from the earlier chapters.
What the Transpose Is
The transpose of a directed graph G, written Gᵀ, has the same vertices as G. Each edge points the other way. If G has an edge u → v, then Gᵀ has the edge v → u, and Gᵀ has no other edges.
The name comes from the adjacency matrix. Reversing every edge is the same as transposing that matrix.
Reachability Flips, Components Stay
A path u → … → v in G becomes the path v → … → u in Gᵀ when you reverse each edge along it. So:
- u reaches v in G exactly when v reaches u in Gᵀ.
- If u and v reach each other in G, they also reach each other in Gᵀ.
So G and Gᵀ have the same strongly connected components. The edges between components do change: each one reverses. The condensation of Gᵀ is the transpose of the condensation of G. A source component in G becomes a sink component in Gᵀ, and a sink becomes a source.
Here is the running example graph from this tutorial:
| Component | Edges out in G | Edges out in Gᵀ |
|---|---|---|
| {0, 1, 2} | to {3, 4, 5} | none |
| {3, 4, 5} | none | to {0, 1, 2} and {6, 7} |
| {6, 7} | to {3, 4, 5} | none |
The reversal between components is what Kosaraju's algorithm uses. A DFS that starts inside a sink component cannot leave it, so it visits exactly one SCC. The next chapter shows how finish times from the first DFS pick out such starting points.
Building the Transpose
One pass over every adjacency list is enough:
def transpose(n, adj):
radj = [[] for _ in range(n)]
for u in range(n):
for v in adj[u]:
radj[v].append(u)
return radj
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(transpose(8, adj))
# [[2], [0], [1], [2, 5], [3], [4, 6], [7], [6]]The loop creates n empty lists and appends one entry per edge, so it takes O(V + E) time and O(V + E) extra space.
Common Errors
- Shared lists. Writing
[[]] * ncreates n references to one list, so every vertex ends up with the same neighbors. Use a list comprehension, as in the code above. - Reversing in place. If you append reversed edges to
adjwhile you iterate over it, the original edges get mixed with the reversed ones. Always build a new structure. - Missing vertices. If you build
radjlazily, for example with a dict, a vertex with no incoming edges in G gets no entry. Pre-allocating all n lists avoids this. - Assuming the same neighbor order. Transposing twice gives back the same edge set. The order inside each list can differ, and that changes which way DFS explores, but the SCCs come out the same.
Exercise
In the example graph, a DFS on Gᵀ starts at vertex 0. Which vertices does it visit? What if it starts at vertex 3 instead?
Show answer
From 0 it visits only {0, 1, 2}, because {0, 1, 2} is a sink component in Gᵀ. From 3 it visits all eight vertices, because {3, 4, 5} has edges to both other components in Gᵀ.
Exercise
Does G have a cycle exactly when Gᵀ has a cycle? Explain using SCCs.
Show answer
Yes, as long as you count a self-loop as a one-vertex cycle. A directed cycle exists exactly when some SCC has more than one vertex or a vertex has a self-loop. G and Gᵀ have the same SCCs, and a self-loop u → u stays a self-loop when you reverse it.