-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStronglyConnectedComponents.cpp
290 lines (257 loc) · 5.81 KB
/
StronglyConnectedComponents.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//Strongly connected components using Kosaraju's algorithm- 2 DFS passes, first with grid revered to get finishing times and second on original map
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node {
int data;
node* prev;
node* next;
};
class Stack {
private:
node* head, *tail;
public:
Stack() {
head = NULL;
tail = NULL;
}
void push(int value) {
node* temp = new node;
temp->data = value;
temp->next = NULL;
if (isEmpty()) {
temp->prev = NULL;
head = temp;
tail = temp;
}
else {
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
int pop() {
if (isEmpty()) {
cout << "Stack is empty, nothing to remove" << endl;
}
else if (head->next == NULL) {
int value=head->data;
head = NULL;
tail = head;
return value;
}
else {
int value=tail->data;
node* temp = new node;
temp = tail->prev;
tail = temp;
delete tail->next;
temp->next = NULL; //Just to make sure its not pointing to garbage value
return value;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return;
}
node* temp = new node;
temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
bool isEmpty() {
if (head == NULL) {
return 1;
}
return 0;
}
};
void DFS_Loop1(vector<vector<int>>&, vector<int>&, Stack&);
void DFS_inner(vector<vector<int>>&, vector<int>&, int);
void remap_gr(vector<vector<int>>& , vector<vector<int>>&) ;
void DFS_Loop2(vector<vector<int>>&, vector<int>&);
void DFS_inner2(vector<vector<int>>&, vector<int>&, int);
void quickSort(vector<int>& ,int,int);
void printVector2D(vector<vector<int>>&);
void Swap(vector<int>&, int, int);
void printVector1D(vector<int>& );
//Global variables
int t = 0, s = 0;
vector<int>finish_time = {};
vector<int>precedence = {};
int num=0;
void DFS_Loop1(vector<vector<int>>& arr1,vector<int>& E, Stack& list) {
while (!list.isEmpty()) {
int v1 = list.pop();
if (E[v1 - 1] == 0) {
DFS_inner(arr1, E, v1);
}
}
}
void DFS_inner(vector<vector<int>>& arr1,vector<int>& E, int v) {
//Find all findable vertices from vertex v
E[v - 1] = 1;
vector<int> v2_options = arr1[v - 1];
//v2_options.erase(v2_options.begin());
int v2 = 0;
for (int i = 1; i < v2_options.size(); i++) {
v2 = v2_options[i];
if (E[v2 - 1] == 0) {
E[v2 - 1] = 1;
DFS_inner(arr1, E, v2);
}
}
t++;
finish_time[v - 1] = t;
//precedence.push_back(v);
}
void remap_gr(vector<vector<int>>& r_gr, vector<vector<int>>& gr) {
//Remap graph according to finishing times
int n = r_gr.size();
int n2;
int row, val;
for (int i = 0; i < n; i++) {
n2 = r_gr[i].size();
for (int j = 1; j < n2; j++) {
//Element to add- arr1[i][0] in row arr1[i][j] of gr
row = finish_time[r_gr[i][j]-1]-1;
val = finish_time[r_gr[i][0]-1];
gr[row].push_back(val);
}
}
}
void DFS_Loop2(vector<vector<int>>& arr1, vector<int>& arr2) {
int n = arr1.size();
int count = 0;
vector<int> scc;
for (int i = n; i > 0; i--) {
int v1=i;
if (arr2[v1 - 1] == 0) {
num=1;
DFS_inner2(arr1, arr2, v1);
if(num>100){
scc.push_back(num);
}
//cout<<num<<endl;
count++;
}
}
// for(int i=0;i<scc.size();i++){
// cout<<scc[i]<<endl;
// }
int n2=scc.size();
quickSort(scc,0,n2);
for(int i=1;i<6;i++){
cout<<scc[n2-i]<<endl;
}
cout << "SCCs:" << count << endl;
}
void quickSort(vector<int>& arr,int s,int N){
//Get the largest 5 values
if(N-1>s){
int n=arr.size();
int p=arr[s];
int i=s+1;
for(int j=s+1;j<N;j++){
if(arr[j]<p){
Swap(arr,i,j);
i++;
}
}
Swap(arr,s,i-1);
if(i>n-6){
quickSort(arr,s,i);
}
else{
quickSort(arr,i,N);
}
}
}
void Swap(vector<int>& arr, int a, int b){
int k=arr[a];
arr[a]=arr[b];
arr[b]=k;
}
void DFS_inner2(vector<vector<int>>& arr1, vector<int>& E, int v) {
//Find all findable vertices from vertex v
E[v - 1] = 1;
vector<int> v2_options = arr1[v - 1];
v2_options.erase(v2_options.begin());
int v2 = 0;
for (int i = 0; i < v2_options.size(); i++) {
v2 = v2_options[i];
if (E[v2 - 1] == 0) {
num++;
E[v2 - 1] = 1;
DFS_inner2(arr1, E, v2);
}
}
}
int main() {
// vector<vector<int>>Graph = { {1,4},{2,8},{3,6},{4,7},{5,2},{6,8},{7,1},{8,5,6},{9,3,7} };
// vector<vector<int>>Graph_rev = { {1,7},{2,5},{3,9},{4,1},{5,8},{6,3,8},{7,4,9},{8,2},{9,6} };
// int n = Graph.size();
//Reading input file
int n;
cout << "Enter number of nodes:";
cin >> n;
cin.clear();
vector<vector<int>>Graph(n);
vector < vector<int>>Graph_rev(n);
for(int i=0;i<n;i++){
Graph[i].push_back(i+1);
Graph_rev[i].push_back(i+1);
}
//Using strtok
char line[1000]="";
FILE* fp= fopen("SCC.txt", "r");
while(fgets(line, 1000, fp)!=NULL){
char* pch = strtok(line, "\t \n");
int u = atoi(pch);
pch = strtok(NULL, "\t \n");
while (pch != NULL)
{
int v = atoi(pch);
//Enter here what you want to do with u and v
//Graph[u-1].push_back(v);
Graph_rev[v-1].push_back(u);
pch = strtok(NULL, "\t \n");
}
}
//Solve
finish_time.resize(n, 0);
vector<int> Explored = {};
Explored.resize(n, 0);
Stack list;
for (int i = 1; i<=n; i++) {
list.push(i);
}
DFS_Loop1(Graph_rev, Explored,list);
vector<int>E = {};
E.resize(n, 0);
remap_gr(Graph_rev,Graph);
DFS_Loop2(Graph, E);
}
void printVector2D(vector<vector<int>>& arr) {
int n1 = arr.size();
for (int i = 0; i < n1; i++) {
int n2 = arr[i].size();
for (int j = 0; j < n2; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void printVector1D(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}