I. Introduction to Algorithms
Tarjan algorithm A method of solving the strongly connected component of a forward graph by Robert Tarjan, which can achieve the complexity of linear time.
We define:
If two vertices can be connected to each other, 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. A strongly connected sub-graph of a graph, called a strongly connected component (strongly connected components).
For example: In, {1, 2, 3, 4}, {5}, {6} Three zones can be interconnected, known as the strong connected component of the graph.
Tarjan algorithm is based on graph depth First search algorithm, each strong connected component is a subtrees tree in the search tree. When searching, the unhandled node in the current search tree is added to a stack, which can be used to determine if the node in the stack is a strong connected component.
In the re-Tarjan algorithm, there is the following definition.
dfn[i]: the order in which the node is searched in DFS (timestamp)
low[i]: The number of nodes in the oldest stack that can be traced to the subtree of I or I
When dfn[i]==low[i], a subtree of I or I can form a strong connected component.
Two. Algorithm diagram
With 1 as the starting point of the Tarjan algorithm,
Dfs searched for node 6 sequentially
Backtracking found low[5]==dfn[5], low[6]==dfn[6], then {5}, {6} is two strongly connected components. Go back to the 3 node and expand Node 4.
Expand Node 1, find 1 re-stack update low[4],low[3] with a value of 1
Backtracking node 1, expanding Node 2
Since then, Tarjan algorithm has ended, {1, 2, 3, 4}, {5}, and {6} are three strongly connected components in the graph.
It is not difficult to find that the time complexity of Tarjan algorithm is O (e+v).
Learning Office
Template:
1 voidDfsintu)2 {3times++;//Record DFN Order4Dfn[u]=times;//Assign Value5Low[u]=times;//first assign the initial value6vis[u]=true;//Vis[i] To determine if i have searched;7insta[u]=true;//indicates whether in the stack, true is in the stack;8Stack[top]=u;//Top of Stack9top++;Ten for(inti=head[u];i!=-1; i=edge[i].next)//enumerate the edges attached to this point in the build order One { A intv=edge[i].to;//points to search for - if(!vis[v])//if not searched , the stack is not in - { theDFS (v);//continue the deep search at this point -Low[u]=min (Low[u],low[v]);//update the low value, this edge is a branch edge so compare U at this time -}//Low value (is its DFN value when not updated) and V - Else + if(insta[v]==true)//if searched and in the stack, this side is a back edge or a cross in the stack - { +Low[u]=min (Low[u],dfn[v]);//Update the low value to compare the low value of U at this time and the DFN value of v A } at } - - if(Low[u]==dfn[u])//equality means finding a strong connected component - { - while(top>0&&STACK[TOP]!=U)//start the fallback stack until you return to u - { intop--; -insta[stack[top]]=false; to } + } -}
View Code
Examples:
Poj2186-popular cows
The main idea is: In a herd, there are n cows, given m pairs of relationships (a, a, a) to show a admire B, and the admiration of the relationship is transitive, asked by all cows (except themselves) admire the number of cows
Problem-solving ideas: Find out all the connected components, if only one connected component of the output is 0, then the number of points of the outputs that connected components can, if not the only output 0
Since there are two of the connectivity components, there must be at least one cow that does not admire the other, so we guarantee at least 0 of the connected component is unique
Problem Solving Ideas:
1. Use Tarjan to find the double connected component and then indent it into points. These points form a tree.
2, the number of nodes in the tree to find out the degree of zero, if there is a output that point contains all the points (because it is a shrinking point out of the tree).
Attention:
1, the given diagram will have the possibility of non-connectivity, if so sure output zero. Because it's not connected, there's definitely not going to be a situation where all the other cows think a cow is bull.
2, if there are many points after the contraction point, then output zero. Because this picture is connected, but there is still no other cows think that a cow is very cow (you know it by painting).
The strong connected component is mainly to simplify the structure of the graph, if a point outside the component can reach one of the points within the component, then it must be able to reach all points within the component, so in some degree, the strong connected component can be reduced to a point.
#include <stdio.h>#include<string.h>Const intMAXN =10005;Const intMAXM =100005;structnode{intTo,next;} EDGE[MAXM];intN,m,head[maxn],dfn[maxn],low[maxn],stack1[maxn],num[maxn],du[maxn],vis[maxn],cnt,time,top,cut;voidinit () {memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (Head,-1,sizeof(head)); memset (Vis,0,sizeof(VIS)); memset (num,0,sizeof(num)); Memset (Du,0,sizeof(du)); CNT=0; time=1; Top=0; Cut=0;}voidAddedge (intUintv) {edge[cnt].to=v; Edge[cnt].next=Head[u]; Head[u]=CNT; CNT++;}intMinintAintb) { if(a>b) a=b; returnA;}voidDfsintUintFA) {Dfn[u]=Time ; Low[u]=Time ; time++; Vis[u]=1; Stack1[top]=u; Top++; for(intI=head[u]; i!=-1; I=Edge[i].next) { intv=edge[i].to; if(!Vis[v]) {DFS (V,U); Low[u]=min (low[u],low[v]); } Else if(Vis[v]) {Low[u]=min (low[u],dfn[v]); } } if(low[u]==Dfn[u]) {Cut++; while(top>0&&stack1[top]!=u) {top--; Vis[stack1[top]]=2; Num[stack1[top]]=cut; } }}intMain () {inti,u,v; while(SCANF ("%d%d", &n,&m)! =EOF) {init (); for(i=0; i<m; i++) {scanf ("%d%d",&u,&v); Addedge (U,V); } for(intI=1; i<=n; i++) { if(!Vis[i]) {DFS (I,0); } } for(i=1; i<=n; i++) { for(intJ=head[i]; j!=-1; j=Edge[j].next) { if(num[i]!=Num[edge[j].to]) {Du[num[i]]++; } } } intsum=0, X; for(i=1; i<=cut; i++) { if(!Du[i]) {Sum++; X=i; } } if(sum==1) {sum=0; for(i=1; i<=n; i++) { if(num[i]==x) {sum++; }} printf ("%d\n", sum); } Else{puts ("0"); } } return 0;}
View Code
Tarjan algorithm, a magical algorithm for the connectivity of graphs