3 Strongly connected components
This chapter shows where the cycles of a directed graph are, and which nodes reach each other through them.
Edges often mean “depends on”, “uses”, “calls” or “waits for”. A cycle of such edges needs attention. Packages on a cycle cannot be built one after the other. Functions that call each other and grammar rules that use each other have to be analyzed together. Processes that wait for each other in a cycle wait forever. The groups of nodes that reach each other are called strongly connected components.
Problems in this chapter
Section titled “Problems in this chapter”- 3.1 Find the groups of nodes that reach each otherYou divide a graph into its strongly connected components, see which of them contain a cycle, and check whether two given nodes are in the same one.
- 3.2 Condense each group into one node to get a DAGYou condense each strongly connected component into one node and get a graph without cycles that keeps the edges between the components.
- 3.3 Put the nodes in dependency order although the graph has cyclesYou get an order of the strongly connected components in which each comes after the ones it depends on.
- 3.4 Check whether every node reaches every otherYou check whether a graph is strongly connected, and use that to check a control-flow graph for blocks that never run or never reach the exit.
- 3.5 Find the edges and nodes whose removal splits a strongly connected componentYou find the strong bridges and strong articulation points of a graph, without which a strongly connected component splits into several.
The examples of this chapter use the grammar of a small programming language, whose rules use each
other: a statement can contain statements, and an expression can contain expressions. They also use
a small program: the dependencies between its packages, and the control flow of its method
loadloadloadLoad.
Reaching each other
Section titled “Reaching each other”A node a reaches a node b if a path of edges leads
from a to b. Two nodes are strongly connected if each reaches the other - that is, if a cycle
runs through both. A strongly connected component (SCC for short) is a group of nodes in which
every node reaches every other. The group is as large as it can be: no node outside it reaches its
nodes and is reached by them.
- SCC
- Edge on a cycle
Here, nodes 0, 1 and 2 reach each other through the cycle 0 -> 1 -> 2 -> 0, and so do
3 and 4. Node 2 reaches 3, but 3 does not reach 2: they are in different SCCs.
Node 5 lies on no cycle and is an SCC of its own.
Three facts follow from the definition, and the problems of this chapter build on them:
- Every node is in exactly one SCC. A node that lies on no cycle forms an SCC of one node.
- Every cycle lies inside one SCC. The orange edges lie on a cycle, and they never leave their frame.
- Between the SCCs, there are no cycles. If two SCCs reached each other, they would be one. So the SCCs, taken as nodes, form a DAG (directed acyclic graph). This DAG is called the condensed graph. The SCCs can be put in an order that follows its edges: here SCC 2, then SCC 1, then SCC 0.
A graph that is one SCC
Section titled “A graph that is one SCC”If the whole graph is one SCC, every node reaches every other: the graph is strongly connected. Inside an SCC, some edges and nodes may be indispensable: without one of them, the SCC splits into several. These are its strong bridges and strong articulation points. In the picture, every edge of SCC 2 is a strong bridge, and so is each of the two edges of SCC 1. Nodes 0, 1 and 2 are strong articulation points. Nodes 3 and 4 are not, since without one of them the other still forms an SCC.