Kosaraju algorithm for finding strongly connected branches of a graph

Source: Internet
Author: User

Partially reprinted from: http://www.tuicool.com/articles/uQBz2y 1. Related Concepts

Given a graph G = (V, E), for any pair of vertices u and V, there are U-V and V---u, i.e., vertex u and V are mutually accessible, then the graph G is strongly connected (strongly Connected). As shown in the following figure, any two vertices are mutually accessible.

For undirected graphs, whether the graph is strongly connected, you can directly use depth-first search (DFS) or breadth-first search (BFS), from any vertex, if the result of the traversal contains all the vertices, then the diagram is strongly connected.

For directed graphs, it is not possible to use DFS or BFS for direct traversal to determine. As shown in the following figure, if you are traversing from vertex 0, you can tell that it is strongly connected and that you cannot reach all nodes if you start traversing from another vertex.

So, how to judge the strong connectivity of the graph.

In fact, the better way to solve this problem is to use the strongly connected branching algorithm (scc:strongly Connected components) to find all the SCC in O (v+e) time. If the number of SCC is 1, then the entire graph is strongly connected.

A strongly connected branch of the graph G = (V, E) is one of the largest vertex sets C,c is a subset of V, and for each pair of vertices in C u and V, there are U-V and V-u, i.e., the vertices u and V are mutually accessible.

An algorithm for implementing SCC is the Kosaraju algorithm. The Kosaraju algorithm is based on depth-first search (DFS) and takes two DFS traversal of the graph, with the following steps: initialization sets all vertices to be inaccessible, DFS traversal from any vertex V, and if the traversal results do not have access to all vertices, the diagram is not strongly connected; Replace the entire graph (Reverse graph), set all vertices in the displacement graph to be not visited, and start the DFS traversal from the same vertex v as in step 2, if the traversal does not have access to all vertices, the diagram is not strongly connected, otherwise the illustration is strongly connected.

The idea of the Kosaraju algorithm is that if all vertices can be reached from Vertex V, and all vertices can reach V, the diagram is strongly connected. 2. Java Source Code

/** find the strong connected branch of the graph, forward traversal, according to the post-root sequence pressure stack, according to the reverse graph to see if the node can reach */import java.util.*;
	   public class Kosaraju {static class Graph {int n;
	   list<integer>[] adj;
		   Graph (int n) {this.n=n;
		   This.adj=new List[n];
		   for (int i=0;i<n;i++) {this.adj[i]=new arraylist<integer> ();
	   }} public void Addedge (int v,int w) {This.adj[v].add (w); }/** forward traversal, next root sequence stack, guaranteed root first out stack */public void Fillorder (int v,boolean[] visited,stack<integer> s) {VI
		   Sited[v]=true;
			   For (Integer I:this.adj[v]) {if (!visited[i]) {fillorder (i,visited,s);
	   }} s.push (v);
		  }/** Get Reverse graph */public graph Gettranspose () {graph gv=new graph (THIS.N);
			  for (int i=0;i<n;i++) {for (Integer J:this.adj[i]) {gv.adj[j].add (i);
	   }} return GV;
		   }/**DFS Print Connected branch */public void DFSUtil (int v,boolean[] visited) {visited[v]=true; SystEm.out.print (v+ "");
			   For (Integer I:adj[v]) {if (!visited[i]) {DFSUtil (i,visited); }}}/** follow the steps of the KOSARAJU algorithm */public void Printsccs () {stack<integer> s=new stack<
		   Integer> ();
		   Boolean[] Visited=new BOOLEAN[THIS.N];
		   for (int i=0;i<n;i++) {visited[i]=false;
			   }/** after the root sequence stack */for (int i=0;i<n;i++) {if (!visited[i]) {fillorder (i,visited,s);
		   }}/** Get reverse graph */graph gr=this.gettranspose ();
		   for (int i=0;i<n;i++) {visited[i]=false;
			   The/** is based on the inverse graph to calculate the accessibility */while (!s.empty ()) {int v=s.pop (); if (visited[v]==false) {gr.
				   DFSUtil (V, visited);
			   System.out.println ();
	   }}}} public static void Main (String args[]) {Graph g=new graph (5);
	   G.addedge (1, 0);
	   G.addedge (0, 2);
	   G.addedge (2, 1);
	   G.addedge (3, 0);
	   G.addedge (3, 4); G.Printsccs ();
 }
   
  
}

3. Proof of correctness

1. The first time DFS has a graph G, the last recorded node must be the root node of the last spanning tree.
Proof: Assuming that the last node is not the root of the tree, there must be one node as the root, and the root node must be the ancestor of this node, and the latter root sequence access to know the ancestor node than this node later access, contradiction; The original proposition is established

2. For the first time in the DFS build forest, take two nodes A, B, meet: B than a later record, and B is not an ancestor (that is, in the first DFS, A, b in a different spanning tree); In the second DFS build forest, B is not an ancestor of a, and a is not an ancestor of B (that is, in the second DFS , A, B are in different spanning trees).
Proof: Assume that in the second DFS build forest, B is the ancestor of a, the inverse of the GT in the B to a path, that is, the first DFS generation forest, A is the ancestor of B, then a must be more late than B to record the contradiction; assume that a is an ancestor of B in the second DFS build forest, the A-to-B path exists in the inverse-graph GT , that is, the first DFS-generated forest, B is the ancestor of a, contradiction; The original proposition is established

3. The required strong connected component is obtained by the above steps
Proof: First, proof 2 ensures that each tree in the second DFS is the first tree in DFS or a subtree of a tree. Second, for each tree in the second DFS, the first DFS guarantees connectivity from the root to its descendants, and the second DFS guarantees the reverse connectivity of the root to the descendants (that is, the descendant-to-root connectivity), whereby each node in the tree is interconnected through its root. 4. Algorithm complexity The time complexity of the DFS traversal is O (v+e), and the graph transpose is O (v+e). So the total time complexity is O (v+e).

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.