Kosaraju algorithm details, kosaraju Algorithm

Source: Internet
Author: User

Kosaraju algorithm details, kosaraju Algorithm
 The Kosaraju algorithm can calculate the number of strongly connected components in a directed graph and mark points belonging to different strongly connected components. Its Algorithm DescriptionRelatively simple:(1)First pair of GraphsGProceedDFSAnd record the exit sequence of each vertex during the traversal process. Example: If1For the start point traversal, the order of access nodes is as follows: 

 

When the node is accessed for the second time, we can get the exit order of the node:  (2) reverse the direction of each edge to construct an inverse graph. G ' . Then, the reverse graph is processed for the second time in the reverse order of exit. DFS Traverse. We press 1 , 4 , 2 , 3 , 5 The second time in reverse order DFS Traversal: 

 

The access process is as follows:  The vertices obtained by each traversal belong to the same strongly connected component. 1 , 4 Belong to the same strongly connected component, 2 , 3 , 5 It belongs to another strongly connected component.

Code:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int map[101][101]; 6 int nmap[101][101]; 7 int pass[101]; 8 int vis[101]; 9 int now=1;10 int n,m;11 int num=0;12 void dfs(int p)13 {14     vis[p]=1;15     for(int i=1;i<=n;i++)16     {17         if(vis[i]==0&&map[p][i]==1)18         {19             dfs(i);20         21         }22     }23     pass[now]=p;24     now++;25 }26 void ndfs(int p)27 {28     vis[p]=0;29     for(int i=1;i<=n;i++)30     {31         if(vis[i]==1&&nmap[p][i]==1)32         ndfs(i);33     }34 }35 int main()36 {37     38     scanf("%d%d",&n,&m);39     for(int i=1;i<=m;i++)40     {41         int x,y;42         scanf("%d%d",&x,&y);43         map[x][y]=1;44         nmap[y][x]=1;45     }46     for(int i=1;i<=n;i++)47     {48         if(vis[i]==0)49         dfs(i);50     }51     pass[now]=1;52     for(int i=n;i>=1;i--)53     {54         if(vis[pass[i]]==1)55         {56             ndfs(pass[i]);57             num++;58         }59     }60     cout<<num;61     return 0;62 }

 

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.