Strong Components in Linear Time Continued
Understand how these techniques can improve the efficiency of the algorithm.
We'll cover the following
Tarjan’s algorithm
An earlier but considerably more subtle linear-time algorithm to compute strong components was published by Bob Tarjan in 1972. Intuitively, Tarjan’s algorithm identifies a source component of , “deletes” it, and then “recursively” finds the remaining strong components; however, the entire computation happens during a single depth-first search.
Characterizing sink components in a directed graph
Fix an arbitrary depth-first traversal of some directed graph . For each vertex , let denote the smallest starting time among all vertices reachable from by a path of tree edges followed by, at most, one non-tree edge. Trivially, , because can reach itself through zero tree edges followed by zero non-tree edges. Tarjan observed that sink components could be characterized in terms of this low function.
Lemma: A vertex is the root of a sink component of if and only if and for every proper descendant of .
Proof: First, let be a vertex such that . Then there is no edge where is a descendant of and . On the other hand, cannot reach any vertex such that . It follows that can reach only its descendants, and therefore any descendant of can reach only descendants of . In particular, cannot reach its parent (if it has one), so is the root of its strong component.
Now suppose in addition that for every descendant of . Then each descendant can reach another vertex (which must be another descendant of ) such that . Thus, by induction, every descendant of can reach . It follows that the descendants of comprise the strong component whose root is . Moreover, must be a sink component because cannot reach any vertex outside of .
On the other hand, suppose is the root of a sink component . Then can reach another vertex if and only if . But can reach all of its descendants, and every vertex in is a descendant of , so descendants comprise . If for any other node , then would be another root of , which is impossible.
Computing for every vertex via depth-first search is straightforward; see the algorithm below.
The lemma above implies that after running , we can identify the root of every sink component in time (by a global whatever-first search) and then mark and delete those sink components in additional time (by calling whatever-first search at each root) and then recurse. Unfortunately, the resulting algorithm might require iterations, each removing only a single vertex, thus naively giving us a total running time of .
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy