Skip to content

Commit

Permalink
Merge pull request #159 from navaneet625/main
Browse files Browse the repository at this point in the history
strongly connected component graph
  • Loading branch information
aman-raza authored Oct 6, 2021
2 parents 0cb836a + 6a9f9be commit d8a4dce
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions strongly_connected_component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<bits/stdc++.h>
using namespace std;

void dfs(vector<vector<int>>&adj, vector<bool>&vis, int node, vector<int>&st)
{
vis[node] = true;
for (auto nbr : adj[node])
{
if (!vis[nbr])
dfs(adj, vis, nbr, st);
}
st.push_back(node);
}

void dfs_(vector<vector<int>>&adj, vector<bool>&vis, int node)
{
vis[node] = true;
cout << node << " ";
for (auto nbr : adj[node])
{
if (!vis[nbr])
dfs_(adj, vis, nbr);
}
}

int main()
{
int n, m;
cin >> n >> m;

vector<vector<int>> adj(n), rev_adj(n);
while (m--)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
rev_adj[b].push_back(a);
}
vector<bool>vis(n, false);
vector<int>st;//use stack

for (int i = 0; i < n; i++)
{
if (!vis[i])
dfs(adj, vis, i, st);
}
fill(vis.begin(), vis.end(), false);


char comp_name = 'A';

for (int i = st.size() - 1; i >= 0; i--)
{
int node = st[i];
if (!vis[node])
{
cout << "component " << comp_name << "-->";
dfs_(rev_adj, vis, node);
cout << endl;
comp_name++;
}
}

}

0 comments on commit d8a4dce

Please sign in to comment.