[Reprinted] Tarjan Algorithm for strongly connected components

Source: Internet
Author: User

In directed graph G, if at least one path exists between two vertices, the two vertices are calledStrongly Connected(Stronugly connected ). If each vertex of directed graph G is strongly connected, G is calledStrongly Connected Graph. A very large strongly connected subgraph of A digraph of a non-strongly connected graph, calledStrongly Connected Component(Strongly CTED components ).

In, the subgraph {1, 2, 3, 4} is a strongly connected component, because the vertex is reachable between 2, 2, 3, and 4. {5} and {6} are also two strongly connected components.

Based on the definition, we use bidirectional traversal to obtain the intersection to obtain the strongly connected component. The time complexity is O (n ^ 2 + M ). The better way is kosaraju.AlgorithmOr the Tarjan algorithm, the time complexity of both is O (n + M ). This document describes the Tarjan algorithm.

 

[Tarjan algorithm]

The Tarjan algorithm is based on the image depth-first search algorithm,Each strongly connected component is a subtree in the search tree.. When searching, add unprocessed nodes in the current search tree to a stack. When backtracking, you can determine whether the nodes on the stack are a strongly connected component.

Define dfn (u) as the node u search order number (timestamp), low (u) the sequence number of the earliest node in the stack that can be traced back to the U or U subtree. Can be derived from the definition,

Low(U)=Min{Dfn(U), Low(V),(U, V)Is the edge of a tree, and U is the parent node dfn of v.(V),(U, V)Is the backward edge pointing to the node in the stack(Non-cross edge)}

When dfn (u) = low (u), all nodes in the Child tree with u as the root are a strongly connected component.

Algorithm pseudoCodeAs follows:

 

  Tarjan (u)  {
Dfn [u]
= Low [u] = ++ Index // Set sequence numbers and low initial values for node u
Stack. Push (u)
// Press node u Stack in progress For Each (U, v) in E // Enumerate each edge
If (V Is Not Visited) // If node V has not been accessed
Tarjan (V)
// Continue searching
Low [u]
= Min (low [u], low [v])
Else If (V in S) // If node v is still in the stack
Low [u]
= Min (low [u], dfn [v])
If (Dfn [u] = Low [u]) // If the node u is the root of the strongly connected component
Repeat
V
= S. Pop // Stack V to a vertex in the strongly connected component.
Print v
Until(U
= V)
}

 

Next, we will demonstrate the algorithm flow.

From Node 1 to DFS, add the traversal nodes to the stack. When node u = 6 is found, dfn [6] = low [6] finds a strongly connected component. Stack back to u = V, {6} is a strongly connected component.

Return to node 5. dfn [5] = low [5] is found. After stack rollback, {5} is a strongly connected component.

Return to node 3, search for node 4, and add node 4 to the stack. It is found that node 4 has a backward edge to node 1, and node 1 is still in the stack, so low [4] = 1. Node 6 is out of the stack, () is the cross edge, 3 is returned, () is the branch edge, so low [3] = low [4] = 1.

Go back to node 1 and Access Node 2. The access edges (2, 4) and 4 are still in the stack, so low [2] = dfn [4] = 5. After 1 is returned, dfn [1] = low [1] is found, and all nodes in the stack are taken out to form a connected component {1, 3, 4, 2 }.

So far, the algorithm has ended. After this algorithm, all three strongly connected components {1, 3, 4, 2}, {5}, {6} in the graph are obtained }.

 

It can be found that each vertex is accessed once during the running of the Tarjan algorithm, and only once in and out of the stack, each side is accessed only once, therefore, the time complexity of this algorithm is O (n + M ).

 

There is also a powerful Algorithm for Finding the strongly connected component of a directed graph, namely the kosaraju algorithm. Kosaraju is based on the two DFS methods for Directed Graphs and their inverse graphs, and its time complexity is O (n + M ). Compared with the Trajan algorithm, the kosaraju algorithm may be slightly more intuitive. However, Tarjan only needs to perform DFS for the source image once, and does not need to create inverse graphs, which is more concise. In actual tests, the running efficiency of the Tarjan algorithm is about 30% higher than that of the kosaraju algorithm. In addition, the Tarjan algorithm is closely related to the Tarjan Algorithm for Finding undirected graph's dual-connected components (cut points and bridges. Learning the Tarjan algorithm also helps you to deeply understand the Tarjan Algorithm for dual-connected components. The two can be compared and combined.

 

The Tarjan algorithm used to calculate the strongly connected component of a directed graph is named after its inventor Robert Tarjan. Robert Tarjan also invented the Tarjan Algorithm for the dual-connected component and the offline Tarjan Algorithm for the recent public ancestor. He expressed his high respect for Tarjan.

 

PS: C ++ code

  # Define  My_max 1004  

Bool Visited [my_max], instack [my_max];
Int Stack [my_max], top;
Int N, m;
Vector
< Int > Graph [my_max];
Int Dfn [my_max], low [my_max];

Int Index, num;
Void Tarjan ( Int U ){
Int V;
Dfn [u]
= Low [u] = Index ++ ;
Stack [
++ Top] = U;
Instack [u]
= True ;
Visited [u]
= True ;
For (Size_t I = 0 ; I < Graph [u]. Size (); ++ I ){
V
= Graph [u] [I];
If ( ! Visited [v]) {
Tarjan (v );
Low [u]
= Min (low [u], low [v]);
}
Else If (Instack [v])
Low [u]
= Min (low [u], dfn [v]);
}
If (Dfn [u] = Low [u]) {
++ Num;
Do {
V
= Stack [Top -- ];
Instack [v]
= False ;
}
While (V ! = U );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.