Original address: 77431043
First, explain
In the graph G, if there is at least one mutually accessible path between the two vertices, the two vertices are strongly connected (strongly connected). If there is strong connectivity to every two vertices of the graph G, the G is a strongly connected graph. The strongly connected sub-graph of the undirected graph of non-strong connected graphs is called the strong connected component (strongly connected components).
There are many algorithms for solving the strongly connected components of the undirected graph, such as the Kosaraju,gabow and Tarjan algorithms, in which the time complexity of Gabow and Tarjan algorithm is better than that of Kosaraju.
Understand:
It's a little hard to understand if you just see it, but it's easy when we think of it as a tree.
For example, if two points into a strong unicom, then obviously there will be a ring in the tree, the figure of l-m-j-l and a-l-m-b-a looping so the composition of the strong unicom components.
Second, Tarjan algorithm
The Tarjan algorithm is based on the depth-first search tree, which has two important variables Dfn[u]: Represents the order in which the nodes are traversed in a deep search. The low (U) represents the smallest ordinal number that can be found with the U node as the root, u, and the tree nodes below U. Note that Tarjan thinks that a single node is itself a strong link component, and is aware of shielding when processing data. For example, we start with a,
A:DFN[1] = 1; Low (1) =1
L:DFN[2] = 2; Low (2) =2
M:DFN[3] = 3; Low (3) =3
J:DFN[4] = 4; Low (4) =4
When we continue to search the J-node, we find that the L-node has been searched, and l:low (2) = 2, we find J:low (4) =4>l:low (2) = 2, so we assign it low (4) = 2, which means we found a ring at this point, Represents a strong unicom component.
Continue below:
J:DFN[4] = 4; Low (4) =2
M:DFN[3] = 3; Low (3) =2
B:DFN[5] = 4; Low (5) =5
Found B to a:
B:DFN[5] = 4; Low (5) =1
Start returning updates:
M:DFN[3] = 3; Low (3) =1
L:DFN[2] = 2; Low (2) =1
A:DFN[1] = 1; Low (1) =1
Found Dfn=low (1), pop-up stack.
voidTarjan (intu) {Dfn[u]=low[u]=++time;//The order starts at 1 and is initially set to the order number by default Dfn[u]=low[u]//The current node is stacked and placed in the stack, which is accessed. visit[u]=1; S.push (U); Instack[u]=1; //take the next path node of the U node V, when no V is desirable also indicates that the depth search has reached the bottom of the current, this is our function returned to look for another path. for(intj=0; J<g[u].size (); j + +){ intv=G[u][j]; if(visit[v]==0) {Tarjan (v); //when the deep search returns, if a subtree exists under the V node, update the U node's low[u]. low[u]=min (low[u],low[v]); } Else if(Instack[v]) {//The v-node has been accessed, and in the stack, it shows that there is a ring on the current path, which is only assigned, but does not mean that there are no more nodes at the bottom of the U subtree than the current ring. cannot be a deep termination condition. low[u]=min (low[u],dfn[v]); } } intm; intnum=0;//Count a count of a ring//when the depth search is finished, the Dfn[u]==low[u] is returned, and the equality description finds a ring that pops up the nodes in the stack. Note that the Tarjan algorithm thinks that a single node is also a ring. if(dfn[u]==Low[u]) { //eject the nodes in the stack and Count Do{m=S.top (); S.pop (); INSTACK[M]=0; Num++; } while(m!=u); //only the number of nodes in the ring is greater than two is the true ring. if(num>1){ //N points 22 intersect (reach each other), there are N (n-1)/2 Cablestotal+=num* (num-1)/2; } }}
about why only use Access once:
began to doubt that there would be many paths through a certain point, if you use visit record Access records, the next path will not be able to access the point? Then draw the ugly picture:
When we access to the 6 node and found a ring, and reached the bottom point, then according to the algorithm began to return, at the same time 2-6-5 this loop also traversed (at this point 5th has access to the stack and low=1). In other words, when we return to node 1th and start the stack, we have all visited the subtree of node 1th and the ring has been marked. Sub-nodes under node 1th do not lead to nodes above node 1th, such as node No. 0, or 1th can only be counted as a ring similar to 2-6-5. There is no need to judge from number No. 0 to number 5th. So go through it again. What I find ingenious is that the deep forward search process does not process the data, but begins to update the data during the deep return process, record the found loop, and reach the subtree node Dfn[u]==low[u] before the stack starts.
Strong connected, strong connected graph and strong connected component Tarjan algorithm for graphs