TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
kosaraju.cpp
1/* Implementation of Kosaraju's Algorithm to find out the strongly connected
2 components (SCCs) in a graph. Author:Anirban166
3*/
4
5#include <iostream>
6#include <stack>
7#include <vector>
8
15void print(const std::vector<std::vector<int> > &a, int V) {
16 for (int i = 0; i < V; i++) {
17 if (!a[i].empty()) {
18 std::cout << "i=" << i << "-->";
19 }
20 for (int j : a[i]) {
21 std::cout << j << " ";
22 }
23 if (!a[i].empty()) {
24 std::cout << std::endl;
25 }
26 }
27}
28
37void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis,
38 const std::vector<std::vector<int> > &adj) {
39 (*vis)[v] = true;
40 for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
41 if ((*vis)[*i] == false) {
42 push_vertex(*i, st, vis, adj);
43 }
44 }
45 st->push(v);
46}
47
55void dfs(int v, std::vector<bool> *vis,
56 const std::vector<std::vector<int> > &grev) {
57 (*vis)[v] = true;
58 // cout<<v<<" ";
59 for (auto i = grev[v].begin(); i != grev[v].end(); i++) {
60 if ((*vis)[*i] == false) {
61 dfs(*i, vis, grev);
62 }
63 }
64}
65
66// function/method to implement Kosaraju's Algorithm:
76int kosaraju(int V, const std::vector<std::vector<int> > &adj) {
77 std::vector<bool> vis(V, false);
78 std::stack<int> st;
79 for (int v = 0; v < V; v++) {
80 if (vis[v] == false) {
81 push_vertex(v, &st, &vis, adj);
82 }
83 }
84 // making new graph (grev) with reverse edges as in adj[]:
85 std::vector<std::vector<int> > grev(V);
86 for (int i = 0; i < V + 1; i++) {
87 for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
88 grev[*j].push_back(i);
89 }
90 }
91 // cout<<"grev="<<endl; ->debug statement
92 // print(grev,V); ->debug statement
93 // reinitialise visited to 0
94 for (int i = 0; i < V; i++) vis[i] = false;
95 int count_scc = 0;
96 while (!st.empty()) {
97 int t = st.top();
98 st.pop();
99 if (vis[t] == false) {
100 dfs(t, &vis, grev);
101 count_scc++;
102 }
103 }
104 // cout<<"count_scc="<<count_scc<<endl; //in case you want to print here
105 // itself, uncomment & change return type of function to void.
106 return count_scc;
107}
108
109// All critical/corner cases have been taken care of.
110// Input your required values: (not hardcoded)
111int main() {
112 int t = 0;
113 std::cin >> t;
114 while (t--) {
115 int a = 0, b = 0; // a->number of nodes, b->directed edges.
116 std::cin >> a >> b;
117 int m = 0, n = 0;
118 std::vector<std::vector<int> > adj(a + 1);
119 for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each
120 // required to form an edge.
121 {
122 std::cin >> m >> n; // take input m,n denoting edge from m->n.
123 adj[m].push_back(n);
124 }
125 // pass number of nodes and adjacency array as parameters to function:
126 std::cout << kosaraju(a, adj) << std::endl;
127 }
128 return 0;
129}
std::vector< size_t > dfs(const std::vector< std::vector< size_t > > &graph, size_t start)
Explores the given vertex, exploring a vertex means traversing over all the vertices which are connec...
int main()
Main function.