Tarjan algorithm and its application introduction
The Tarjan algorithm can solve various problems such as LCA, strong connected component, double-link component (point double, side double), cutting point, cutting edge, etc.
Here is a simple collation of several applications of the Tarjan algorithm.
LCA
Http://www.cnblogs.com/mjtcn/p/6852646.html
Strong Unicom Component
The direction of a graph
Strong unicom: In a graph G, set two points a B found that a road from a can go to B, from B and a way to go to a, we call these two vertices (a, B) strong connectivity.
Strong Connect graph: If in a graph G, every two points are strong connectivity, we call this graph, strong connected graph.
Strongly connected components: in a directed graph G, there is a sub-graph, which satisfies strong connectivity every 2 points, we call this sub-graph called a strongly connected component [component: A vector that breaks a vector into several directions, and a vector in those directions is called the component of the vector (the pre-decomposed vector).
Http://www.cnblogs.com/mjtcn/p/7599217.html
Side Double Unicom component
The non-direction graph
Side Double Unicom diagram: If in an undirected graph, any two points have at least two edges not repeating the path, it is said that the graph is the edge of the two-connected .
Side Dual-Link component: The maximal sub-graph of the side-double connectivity is called the edge-connected component .
The principle is similar to the method of strong Unicom component.
1 voidTarjan (intUintFA) {2Dfn[u] = Low[u] = + +tn;3St[++top] =u;4Vis[u] =true;5 for(intI=head[u]; I I=e[i].nxt) {6 intv =e[i].to;7 if(!Dfn[v]) {8 Tarjan (v);9Low[u] =min (low[u],low[v]);Ten } One Else if(Vis[v] && v!=FA) ALow[u] =min (low[u],dfn[v]); - } - if(Dfn[u] = =Low[u]) { the++CNT; - Do { -Vis[st[top]] =false; -Bel[st[top]] =CNT; +top--; -} while(st[top+1] !=u); + } A}
View Code
Simple points can be written so that the low array can have the function of the BEL array
1 voidTarjan (intUintFA) {2Dfn[u] = Low[u] = + +tn;3 for(intI=head[u]; I I=e[i].nxt) {4 intv =e[i].to;5 if(!Dfn[v]) {6 Tarjan (v,u);7Low[u] =min (low[u],low[v]);8 }9 Else if(Dfn[v] < Dfn[u] && v! =FA)TenLow[u] =min (low[u],dfn[v]); One } A}
View CodePoint Double Unicom component
The non-direction graph
Cut the dots.
The cutting point of the graph
In an undirected connectivity graph, if a vertex is deleted, the number of connected components increases, and the point is called a cut point (or cut-top).
Naïve seeking O (n (n+m)), try to delete each point, DFS judge whether Unicom.
Tarjan algorithm complexity O (n+m), Linear!!!
Conditions for cutting points:
- Root node: There are multiple child nodes.
- Non-root node: The ancestor of the point U and its descendants without dots connecting to u (you can connect back to u)
So just let Low[v] >= Dfn[u] can.
1 voidTarjan (intUintFA) {2Low[u] = Dfn[u] = + +tn;3 intCnt_son =0;4 for(intI=head[u]; I I=e[i].nxt) {5 intv =e[i].to;6cnt_son++;7 if(!Dfn[v]) {8 Tarjan (v,u);9Low[u] =min (low[u],low[v]);Ten if(Low[v] >=Dfn[u]) OneIscut[u] =true; A } - Else if(Dfn[v] < Dfn[u] && v! =FA) -Low[u] =min (low[u],dfn[v]); the } - if(fa==-1&& cnt_son==1) Iscut[u] =false; -}
View Code
Cutting Edge
Graph without direction
In an undirected graph, if an edge is removed, the graph is no longer connected, and the edge is the cut Edge (bridge).
If you have a child node V, all of its child nodes and their own can not be connected back to the ancestors of U, here including also cannot connect to u, then u-v, is a cut edge
Code just change one place, Low[v] > Dfn[u]
1 voidTarjan (intUintFA) {2Dfn[u] = Low[u] = + +tn;3 for(intI=head[u]; I I=e[i].nxt) {4 intv =e[i].to;5 if(!Dfn[v]) {6 Tarjan (v,u);7Low[u] =min (low[u],low[v]);8 if(Low[v] >Dfn[u]) {9printf"%d%d\n", u,v);Ten } One } A Else if(Dfn[v] < Dfn[u] && v! =FA) -Low[u] =min (low[u],dfn[v]); - } the}
View Code
Tarjan algorithm and its application