Note: Before reading this article, make sure that you understand and master the basic Tarjan algorithm. 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. Determine the strongly connected components (see the first line of this article with links), cut points, bridges, and shrink 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.Node V and node u form a block. This indicates that V's descendant cannot reach the U's ancestor through other edges. After removing the U, the graph is split into two subgraphs. In this way, when processing vertex u, first recursion the subnode V of U, and then backtracking from V to u. If the above inequality is found to be true, a cut point U is found, and the subtree of U and V forms a block. If low [v]> dfn [u], (u, v) is the cut edge.. However, we do not judge this in actual processing, because some graphs may have duplicate edges, which is hard to handle. We record the number of each edge (the number of the two undirected edges is the same), and record the number of the father of each vertex to its edge. If the edge (u, v) is the father side of V. Therefore, you cannot use dfn [u] to update low [v]. In this way, if we find that low [v] = dfn [v] After traversing all the subnodes of V, the father side of U (u, v) is the cut edge. Void Tarjan (int x)
{
Vis [x] = 1;
Dfn [x] = low [x] = ++ num;
For (INT I = head [X]; I; I = next [I])
If (! Vis [ver [I])
{
P [ver [I] = edge [I]; // record the parent edge
Tarjan (ver [I]);
Low [x] = min (low [X], low [ver [I]);
}
Else if (P [x]! = Edge [I]) // It is updated only when it is not the father side.
Low [x] = min (low [X], dfn [ver [I]);
If (P [x] & low [x] = dfn [x]) f [p [x] = 1; // cut edge
} 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. 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.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; Example:Poj 1523, 2942, 3694, 3352, 3177 Tyvj p1111 Note: Part of this article from Byvoid blog: http://www.byvoid.com/blog/biconnect/ |