Algorithm Introduction--kosaraju algorithm (strong connectivity)

Source: Internet
Author: User

Package Org.loda.graph;import org.loda.util.in;/** * * @ClassName: KOSARAJUSCC * @Description: Kosaraju Strong Connectivity Algorithm * * Understanding: Original G, inverse If a->b in the order order, then the inverse figure RG if there is also a->b, indicating that this is strongly connected. * In reverse diagram in accordance with the original sequence of the reverse order DFS, if you can search from a to B, then there must be a->b, which is bound to strong connectivity. and * Expand, a Dfs can find how much, it means that the strong connectivity component is how big, a total number of searches on behalf of a total of how many strong connectivity components * * @author minjun* @date May 24, 2015 PM 11:31:05 * */public class K OSARAJUSCC {//strong connect number private int count;//is accessed private boolean[] visited;//each element belongs to the strong connected component Idprivate int[] Id;public KOSARAJUSCC (Digraph g) {//Initialize int v=g.v (); visited=new boolean[v];id=new int[v];//get reverse post Deptfirstorder d=new Deptfirstorder (g);//Get the inverse graph of G digraph rg=g.reverse ();//dfsfor (int i:d.reversepost ()) {if (!visited[i]) {if (+)) according to the topological order of the reverse graph { DFS (I,RG);//DFS is completed once, indicating a strong connectivity component, count+1count++;}}} /** * * @Title: DFS * @Description: Depth First search * @param @param v* @param @param g settings file * @return void return type * @throws */p rivate void Dfs (int v, Digraph g) {visited[v]=true;id[v]=count;for (int W:g.adj (v)) {if (!visited[w]) {DFS (w, g);}}} /** * * @Title: Count * @Description: Number of strongly connected componentsVolume * @param @return Set file * @return int return type * @throws */public int count () {return count;} /** * * @Title: strongconnected * @Description: Determine whether two points are strongly connected * @param @param a * @param @param b* @param @return Setting file * @re Turn Boolean return type * @throws */public boolean strongconnected (int a,int b) {return id[a]==id[b];} /** * * @Title: ID * @Description: Identity of the strong connected component * @param @param a * @param @return set file * @return int return type * @throws */ public int ID (int a) {return id[a];} /** * * @Title: PRINTSCC * @Description: Print all strong connected components * @param settings file * @return void return type * @throws */public void print SCC () {stringbuilder[] sb=new stringbuilder[count];for (int i=0;i<sb.length;i++) {sb[i]=new StringBuilder ();} for (int i=0;i<id.length;i++) {sb[id[i]].append ("\ t" +i);} for (StringBuilder s:sb) {System.out.println ("Connected component:" +s.tostring ());}} public static void Main (string[] args) {Digraph d=new Digraph (New in ("F:\\ algorithm \\attach\\tinyDG.txt")); KOSARAJUSCC k=new KOSARAJUSCC (d); System.out.println ("Number of strongly connected components:" +k.count ());//print strong all connected components K.PRINTSCC ();}} 

The text data introduced are:

13
22
4 2
2 3
3 2
6 0
0 1
2 0
11 12
12 9
9 10
9 11
7 9
10 12
11 4
4 3
3 5
6 8
8 6
5 4
0 5
6 4
6 9
7 6

---------------------Gorgeous split-line------------------

Output content:

The number of strongly connected components is: 5 Connected components: 7 Connected components: 68 Connected Components: 9101112 Connected components: 02345 Connected components: 1




Algorithm Introduction--kosaraju algorithm (strong connectivity)

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.