-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCC_in_Directed_graph_Kosaraju's_Algo_.cpp
152 lines (133 loc) · 2.07 KB
/
SCC_in_Directed_graph_Kosaraju's_Algo_.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
maneesh(maik)
*/
// Kosaraju's Algorithm
// Strongly connected components in Directed Graph
#include<iostream>
#include<algorithm>
#include<string>
#include<bitset>
#include<deque>
#include<iterator>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define mp make_pair
#define M 1000000007
vector<ll>G1[1000000],G2[1000000];
ll cnt=0;
struct data
{
ll u,v;
};
void DFS1(stack<ll> &S, ll s, ll vis[], ll start[], ll end[])
{
vis[s]=1;
for(ll i=0; i<G1[s].size(); i++)
{
ll it=G1[s][i];
if(vis[it]==0)
{
start[it]=cnt; cnt++;
DFS1(S, it, vis, start, end);
}
}
end[s]=cnt; cnt++;
S.push(s);
}
void DFS2(ll s, ll vis[] )
{
vis[s]=1;
cout<<s<<" ";
for(ll i=0; i<G2[s].size(); i++)
{
ll it=G2[s][i];
if(vis[it]==0)
{
DFS2(it, vis);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
ll start[n+1],end[n+1];
ll i,x,y;
vector<data>edge;
data z;
for(i=0; i<m; i++)
{
cin>>x>>y;
z.u=x; z.v=y;
edge.pb(z);
G1[x].pb(y);
G2[y].pb(x);
}
/* for(i=1; i<=n; i++)
{
cout<<i<<"-->";
for(ll j=0; j<G1[i].size(); j++)
{
cout<<G1[i][j]<<" ";
}
cout<<endl;
} */
ll vis[n+1];
for(i=1; i<=n; i++)
{
vis[i]=0;
}
cnt=0;
stack<ll>S;
for(i=1; i<=n; i++)
{
if(vis[i]==0)
{
start[i]=cnt; cnt++;
DFS1(S, i, vis, start, end);
}
}
for(i=1; i<=n; i++)
vis[i]=0;
ll count_SCC=0,c=1;
while(!S.empty())
{
x=S.top();
if(vis[x]==0)
{
count_SCC++;
cout<<"component no. "<<c<<"--> ";
c++;
DFS2(x, vis); cout<<endl;
}
S.pop();
}
// Print number of SCC's in graph
cout<<"\nTotal components are :: ";
cout<<count_SCC<<endl;
if(count_SCC==1)
{
cout<<"\nOver all Graph is Strongly connected.\n";
}
// pre(start) and post(end) time of DFS
/* for(i=1; i<=n; i++)
cout<<start[i]<<" ";
cout<<endl;
for(i=1; i<=n; i++)
cout<<end[i]<<" ";
cout<<"\n\n";
*/
return 0;
}