Tarjan Algorithm Application (cut point/bridge/contraction point/strongly connected component/dual connected component/LCA (recent common ancestor) Problem)

Source: Internet
Author: User

Reprinted from: http://hi.baidu.com/lydrainbowcat/blog/item/2194090a96bbed2db1351de8.html

 

 

Basic concepts:

1. cut point: if a vertex is deleted and the original connected graph is split into multiple subgraphs, the vertex is calledCut Point.

2. cut Point Set: In an undirected connected graph, if there is a vertex set, after deleting this vertex set and the edge associated with all vertices in this set, when the source image is changed to multiple connected blocks, This vertex set is calledCut Point Set.

3. Point connectivity: Number of vertices in the minimum cut point set.

4. Cut Edge (BRIDGE): After deleting it, the graph will be split into two or more subgraphs.

5. cut edge set: if there is an edge set, after this edge set is deleted, the source image becomes multiple connected blocks, and this vertex set is calledCut edge set.

6. Edge Connectivity: the connectivity of a graphEdge ConnectivityIs defined as the number of edges in the minimum cut edge set.

7. Point Reduction: The connected subgraph without cut edge is reduced to a point, so that two paths can be reached between any two points.

Note: Calculate the block <> contraction point. After the point is reduced, it becomes a K-point K-1 tree connected by cut edge. The cut point can exist in multiple blocks.

8. Dual-connectivity component: this component is divided into two parts: Point dual-connection and edge dual-connection. Its standard definition is: a graph with a point connectivity greater than 1 is calledPoint-to-Point dual-connection DiagramA graph with edge connectivity greater than 1 is calledEdges and hyphens. In layman's terms, a diagram that satisfies any two points and can reach through two or more roads without duplicate edges is called a dual connection diagram. Undirected graph GExtremely LargeThe connected subgraph is calledConnected Component.

Application of the Tarjan algorithm:

1. Require strongly connected components, cut points, bridges, and shrinkage points:

In the Tarjan algorithm, we obtain two Arrays: dfn and low,

Low [u]: = min (low [u], dfn [v]) -- (u, v) is the backward edge, and V is not the child tree of U;

Low [u]: = min (low [u], low [v]) -- (u, v) is a subtree with the branches and edges and V is the U;

We will discuss it below:

If low [v]> = dfn [u], U is the cut point., U and its children form a block. This indicates that the descendant of U cannot reach the ancestor of u through other edges. After removing the U, the graph is split into two subgraphs.

If low [v]> dfn [u], (u, v) is the cut edge.. The reason is similar to the previous situation.

Tarjan requires the code of Directed Graph strongly connected component, cut point, and cut edge:

VaR
N, m, I, j, x, y, z: longint;
A, B: array [0 .. 1000, 0 .. 1000] of longint; // figure
Dfn, low, S: array [0 .. 1000] of longint; // dfn indicates the timestamp, low indicates the ancestor, and s indicates the stack.
VIS, INS: array [0 .. 1000] of Boolean; // If vis is accessed or INS is in the stack
Num, P: longint;

Function min (X, Y: longint): longint;
Begin
If x <Y then exit (x) else exit (y );
End;

Procedure Tarjan (U: longint );
VaR
I, V: longint;
Begin
INC (Num); // specifies a timestamp.
Dfn [u]: = num;
Low [u]: = num;
Vis [u]: = true;
INC (p); // inbound Stack
S [p]: = u;
INS [u]: = true;
For I: = 1 to B [U, 0] Do // note that the following operations are performed only when u is connected to I
If not vis [B [U, I] Then // not accessed
Begin
Tarjan (B [U, I]);
Low [u]: = min (low [u], low [B [U, I]); // It is a branch edge and takes two low min values.
{If you want to find a cut point or cut edge, judge the size of dfn [u] and low [v] here and roll up the stack .}
End
Else if INS [B [U, I] Then // in the stack
Low [u]: = min (low [u], dfn [B [U, I]); // non-branching edge, Min values of low and dfn are obtained.
If dfn [u] = low [u] Then // a strongly connected component has been found, and the stack is played.
Repeat
V: = s [p];
Write (v ,'');
INS [v]: = false;
Dec (P );
If u = V then writeln;
Until u = V;
End;

Begin
Readln (n, m );
For I: = 1 to M do // Diagram
Begin
Readln (x, y );
INC (B [x, 0]);
B [X, B [x, 0]: = y;
End;
Tarjan (1 );
End.

2. Calculate the dual-connected component and construct the dual-connected component:

ForPoint-connected branchIn factCut PointBy the way, we can find the dual-connected branches of each vertex. Create a stack to store the current dual-connected branches. When you search for a graph, add this edge to the stack every time you find a branch edge or backward edge (not a cross-cross edge. If the DFS (u) <= low (v) is met at a time, it indicates that u is a cut point, and the edges are extracted from the top of the stack one by one until the edges (u, v. A cut point can belong to multiple vertices in a dual-connected branch. The other vertices and each edge only belong to one vertex in a dual-connected branch.

ForEdge dual-connectivity BranchThe method is simpler. You only needFind all bridgesAfter the bridge edge is deleted and the source image is changed to multiple connected blocks, each connected block is an edge dual-connected branch. A bridge does not belong to any edge dual-connected branch. The other edges and each vertex belong to and only belongs to one edge dual-connected branch.

How to add an edge to a connected graph with a bridge and change it into an edge pair Connected Graph? The method is to first find all bridges and then delete these bridges. Each connected block is a double connected subgraph. Every connected subgraph is reduced to a vertex, and the bridge edge is added back. The final graph is a tree with the edge connectivity of 1.

Count the number of nodes with a moderate value of 1 in the tree, that is, the number of leaf nodes, which is recorded as leaf. Then, at least two (leaf + 1)/two sides are added to the tree to enable the tree to be connected by two sides. Therefore, the minimum number of edge added is (leaf + 1)/2. The specific method is to first connect an edge between the two most recent common ancestor's two leaf nodes, so that the two nodes can contract all the points on the path of the ancestor, because the formation of a ring must be dual-connected. Then we can find two leaf nodes with the farthest recent common ancestor. This one is exactly the same as (leaf + 1)/two times, and all the points are reduced together.

3. Find the nearest common ancestor (LCA)

When traversing to U, first Tarjan traverses the child tree of U, then the nearest common ancestor of nodes in the Child tree of U and U is U, and the closest common ancestor of U and [U's sibling node and Its subtree] is u's father. Note that because we traverse the data in the DFS order, we can use a color array tag, the dyeing being accessed is 1, and the unaccessed tag is 0, the accessed sibling node and Its subtree are marked as 2, in this way, we can continuously merge and update the query set and use find to achieve the above objectives.

 

Function find (X: longint): longint;
Begin
If f [x] <> X then f [x]: = find (F [x]);
Find: = f [x];
End;
Procedure Tarjan (U: longint );
Begin
F [u]: = u; color [u]: = 1;
For I: = 1 to n do
If (G [U, I]) and (color [I] = 0) Then // G [U, I] indicates that u is connected to I
Begin
Tarjan (I); F [I]: = u;
End;
For I: = 1 to n do
If (ask [U, I]) or (ask [I, u]) and (color [I] = 2) then // ask [U, i] indicates U, I
Begin
LCA [U, I]: = find (I); LCA [I, u]: = LCA [U, I];
End;
Color [u]: = 2;
End;

Note: Using linked list storage edge and problems can reduce the time complexity of this algorithm to O (N + M + q ), n, m, and q are the points, edges, and problem numbers respectively. This document uses the matrix storage method for ease of writing.

Example:Poj 1523, 2942, 3694, 3352, 3177 tyvj p1111

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.