Last active
September 7, 2017 05:57
-
-
Save lsiddiqsunny/a9d2d0e3528838802b14fd79d4215a25 to your computer and use it in GitHub Desktop.
Implementation for finding strongly connected components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
#define mx 50005 | |
using namespace std; | |
vector<int>g[mx],rg[mx],path,newg[mx]; | |
int visited[mx],scc[mx]; | |
int numscc; | |
void dfs1(int s) | |
{ | |
visited[s]=1; | |
for(int i=0; i<g[s].size(); i++) | |
{ | |
int v=g[s][i]; | |
if(visited[v]==0) dfs1(v); | |
} | |
path.push_back(s); | |
} | |
void dfs2(int s) | |
{ | |
visited[s]=1; | |
scc[s]=numscc; | |
for(int i=0; i<rg[s].size(); i++) | |
{ | |
int v=rg[s][i]; | |
if(visited[v]==0) dfs2(v); | |
} | |
} | |
int main() | |
{ | |
int n,m; | |
cin>>n>>m;//taking input number of edges and vertexes | |
int u,v; | |
while(m--) | |
{ | |
cin>>u>>v; | |
g[u].push_back(v);//making orginal graph | |
rg[v].push_back(u); //making reverse graph | |
} | |
for(int i=1; i<=n; i++) | |
{ | |
if(!visited[i]) | |
{ | |
dfs1(i); | |
} | |
} | |
for(int i=0; i<mx; i++) | |
{ | |
visited[i]=0; | |
} | |
int co=0; | |
for(int i=0; i<path.size(); i++) | |
{ | |
if(visited[path[i]]==0) | |
{ co++; | |
numscc=path[i]; | |
dfs2(path[i]); | |
} | |
} | |
cout<<co<<endl; //number of strongly connected components | |
for(int i=1; i<=n; i++) | |
{ | |
for(int j=0; j<g[i].size(); j++) | |
{ | |
int v=g[i][j]; | |
if(scc[i]!=scc[v]) | |
{ | |
newg[scc[i]].push_back(scc[v]); //making scc graph | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment