Sub topic 5
Collapsing Components: The Condensation DAG
This chapter assumes you know how to represent a directed graph as an adjacency list. It also assumes you can already find strongly connected components (SCCs) by hand: an SCC is a maximal set of vertices in which every vertex can reach every other vertex. Here we treat each SCC as a single unit and look at how the units connect to each other.
From vertices to components
The condensation of a directed graph G is a new graph with one node per SCC of G. It has an edge from component X to component Y when G has at least one edge u → v with u in X and v in Y, and X ≠ Y. Edges that stay inside one component are dropped.
Take this graph on 8 vertices:
| Vertex | Out-edges | Component |
|---|---|---|
| 0 | 1 | A |
| 1 | 2 | A |
| 2 | 0, 3 | A |
| 3 | 4 | B |
| 4 | 5 | B |
| 5 | 3 | B |
| 6 | 5, 7 | C |
| 7 | 6 | C |
Only two edges cross between components: 2 → 3 goes from A to B, and 6 → 5 goes from C to B. Every other edge is internal.
Why the condensation is always acyclic
Suppose the condensation had a cycle X → Y → … → X. Then every vertex in X could reach every vertex in Y, and every vertex in Y could reach back into X. All the vertices along the cycle would be mutually reachable, so they would form one SCC. That contradicts the maximality of X and Y. So the condensation is always a directed acyclic graph (DAG).
This fact is what makes SCCs useful. Any directed graph breaks down into strongly connected pieces arranged in a DAG, and that DAG can be topologically sorted. In the example, A and C are source components because no edges enter them. B is a sink component because no edges leave it. The algorithms in the next chapters depend on finding sink or source components in the right order.
Building it in code
Given a list comp where comp[u] is the component number of vertex u, one pass over the edges builds the condensation:
def condensation(n, adj, comp):
k = max(comp, default=-1) + 1
dag = [set() for _ in range(k)]
for u in range(n):
for v in adj[u]:
if comp[u] != comp[v]:
dag[comp[u]].add(comp[v])
return dag
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
comp = [0, 0, 0, 1, 1, 1, 2, 2] # A=0, B=1, C=2, labeled by hand
print(condensation(8, adj, comp)) # [{1}, set(), {1}]This runs in O(V + E) time because each edge is examined once.
Common errors
- Keeping internal edges. If you forget the
comp[u] != comp[v]check, every component with an internal edge gets a self-loop, and the result is no longer a DAG. - Duplicate edges. Several original edges can connect the same pair of components. Using a
setper node removes the duplicates. With plain lists you get a multigraph, which inflates in-degree counts if you later run Kahn's topological sort. - Assuming connectivity. The condensation can have many sources, many sinks, and isolated nodes. It is a DAG, which need not be a tree or a path.
Exercise
Add the edge 4 → 1 to the example graph. What are the SCCs now, and what does the condensation look like?
Show answer
Vertex 1 reaches 4 through 2 and 3, and 4 now reaches 1, so A and B merge into one SCC {0, 1, 2, 3, 4, 5}. C = {6, 7} stays separate. The condensation has two nodes and one edge, C → {0..5}, coming from 6 → 5.
Exercise
Why can't you build the condensation in a single pass before you know the SCCs?
Show answer
Whether an edge is internal or crosses between components depends on the full component labeling. Two vertices are in the same SCC only if each can reach the other, and that can depend on edges you haven't looked at yet.