Strongly Connected Components
Wikipedia-Strongly connected components
Basic idea
- DFS begins from an arbitrary start node
- the collection of search tree is a [[spanning forest]] of the graph
- The first node discovered by DFS is the root of Strongly connected components
Stack invariant
- Nodes are placed on [[stack]] in the order which they are visited
- when the DFS search recursively visits a node
v
and its desedants, those nodes are not neccessarily popped from the stack when recursive call returns. - the node remains on the stack after it has been visited if and only if there exists a path in the input graph from it to some node earlier on the stack.
- In other words, it means that in the #dfs a node would be only removed from the stack after all its connected paths has been traversed.
- when the #dfs will backtrack it would remove the node on a single path and return to the root in order to start a new path.
- At the end of the call that visits
v
and its descendants, we know whetherv
itself has a path to any node ealier on the stack. If so, the call returns, leavingv
on the stack to resever the invariant. If not, thenv
must be the root of its Strongly connected components, which consists ofv
together with any nodes later on stack thanv
. - the connected component root at
v
is then popped from the stack and returned, again preserving the invariant.
Bookeeping
- Each node
v
is assigned a unique integerv.index
, which numbers the nodes consecutively in the order in which they are discovered. - It also maintains a value
v.lowlink
that presents the smallest indx of any node on the stack know to be reachable fromv
throughv
's #dfs subtree, includingv
itself. - therefore
v
must be left the stack ifv.lowlink < v.index
, wherearesv
must be removed as the root of Strongly connected components ifv.lowlink == v.index
. - The value
v.lowlink
is computed during the #dfs fromv
, as this finds the node are reachable fromv
.