-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
367 lines (356 loc) · 9.94 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include<cstdio>
#include<algorithm>
#include<iostream>
#include <list>
#include <fstream>
#include <time.h>
#include <stack>
#include <string.h>
#define NIL -1
using namespace std;
#define MAX_VERTEX_SIZE 301
int j=0;//get the numbers of AP
int edge=0;// the sum of edges remain as m
int vertex=0;// the sum of vertices
int large=0;//use to store the largest SCC(s) in the remainder graph at last
// A class that represents a directed graph
int root[100],rankk[100];
//初始化n个元素
int num=0;
int timee=0;//dfs order
int loww[MAX_VERTEX_SIZE];
int dfnn[MAX_VERTEX_SIZE];
int visit[MAX_VERTEX_SIZE];
int inStack[MAX_VERTEX_SIZE];
bool valid[MAX_VERTEX_SIZE];
int belong[MAX_VERTEX_SIZE];//calculate the element belong to what size of SCC
int scc=0;//count for # of strong connected component in graph
stack<int> st;
stack<int> big;
ofstream out;
class Graph
{
int V; // No. of vertices from 0 to V(actually V+1 as 0 is always a dummy one)
int deletenum; //No. of deleted vertices
list<int> *adj; // A dynamic array of adjacency lists
void APUtil(int v, bool visited[], int disc[], int low[],
int parent[], bool ap[]);
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void deleteVertex(int v,bool* valid); //function to delete an edge in graph
void printGraph(int V,bool* valid); // prints the remainder giant graph
void tarjan(int u,int really);
Graph(const Graph &graph);
~Graph();//free me
//void UnionFind(bool* valid); // get the maximum connected subgraph(s)
};
Graph::Graph(int V)//default
{
this->V = V;
this->deletenum=0;
adj = new list<int>[V+1];
}
Graph::Graph(const Graph &graph)//deep copy
{
this->V=graph.V;
this->deletenum=graph.deletenum;
//cout<<"V is "<<V<<endl;
adj = new list<int>[V+1];
for (int i = 0; i <= V; i++)
{
if(valid[i]==true)
{
list<int>::iterator k;
for (k = graph.adj[i].begin(); k != graph.adj[i].end(); ++k)
{
addEdge(i,*k);
}
}
}
}
Graph::~Graph()
{
//cout<<"delete!"<<endl;
free(adj);
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
//adj[w].push_back(v); // Note: the graph is directed
}
void Graph::deleteVertex(int v,bool* valid)
{
if(valid[v]==false)
cout<<"delete again!"<<endl;
list<int>::iterator i;
int k;
for (k=0; k<=num; k++)
{
adj[k].remove(v);
}
adj[v].clear();//clear itself with other vertices
valid[v]=false;
deletenum++;
// Note: the graph is directed
}
void Graph::printGraph(int V,bool* valid)
{
edge=0;
vertex=0;
out<<"This is the output of the remainder graph"<<endl;
for (int i = 0; i <= V; i++)
{
if(valid[i]==true)
{
//cout<<"vertex "<<i<<" belongs to a SCC with "<<belong[i]<<" elements"<<endl;
vertex++;
//out<<"This is the point "<<i<<" along with its edges"<<endl;
list<int>::iterator k;
for (k = adj[i].begin(); k != adj[i].end(); ++k)
{
int w=*k;
edge++;
//out<<i<<" to "<<w<<endl;
out<<i<<' '<<w<<endl;
}
}
}
out<<"largest SCC has "<<large<<" vertices"<<endl;
out<<"delete "<<deletenum<<" vertices"<<endl;
}
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
void Graph::tarjan(int u,int really)//really 0->test; 1->regular; 2->keep largest SCCs
{
//cout<<"exploring "<<u<<endl;
int index=0;
dfnn[u] = loww[u] = timee++;
st.push(u);
visit[u] = 1;
inStack[u] = 1;
list<int>::iterator v;
if(!adj[u].empty())//no vertex out
{
// //cout<<"this node "<<u<<" doesn't has any edge out"<<endl;
// if(really)
// deleteVertex(u,valid);
// }
// else
// {
for(v = adj[u].begin(); v != adj[u].end(); ++v)
{
if(visit[*v] == 0)
{
tarjan(*v,really);
//cout<<"we're comparing"<<loww[u]<<" and "<<loww[*v]<<endl;
loww[u] = min(loww[u],loww[*v]);
}
else
{
if(inStack[*v]==1)
loww[u] = min(loww[u],dfnn[*v]);
}
}
}
if(dfnn[u] == loww[u])
{
int vtx;
index=0;//refresh?
int temp[num+1];//store scc elements
//cout<<"set is: ";
do
{
vtx = st.top();
st.pop();
inStack[vtx] = 0;//表示已经出栈
temp[index]=vtx;
index++;
}
while(vtx !=u );
//cout<<"total is "<<index<<endl;
if(index==1)//we don't need one point CC in real tarjan
{
//cout<<temp[0]<<' ';
if(really)//it's not attempting!
{
cout<<"isolated point "<<temp[0]<<endl;
deleteVertex(temp[0],valid);
//valid[temp[0]]=false;
}
//deleteVertex(temp[0],valid);
//graph.valid[temp[0]]=false;
}
else
{
if(really==2)//print SCC>=2 in the final graph
{
if(large<index)large=index;
for(int indexx=0; indexx<index; indexx++)
{
out<<temp[indexx]<<' ';
belong[temp[indexx]]=index;//very tricky!
}
out<<"end"<<endl;
}
scc++;//only take scc>=2 into account as good scc
}
//cout<<"end"<<endl;
}
if(really==0&&index==1)
scc++;//accept in attempting to detect any possible APs
//cout<<"vertex "<<u<<" dfn "<<dfn[u]<<' '<<" low "<<low[u]<<endl;
}
void print(int* A)
{
out<<"This is the output of AP points array"<<endl;
for(int i=0; i<j; i++)
out<<A[i]<<' ';
out<<endl;
}
int main()
{
//int max_scc=0;//store largest number of elements among all SCCs
clock_t start,off;
ifstream in;
start=clock();
//in.open("first.txt");
in.open("Edges_threshold.txt");
out.open("rslt.txt");
in>>num;
Graph g(num);
//initialization for valid, belong, and array
for(int i=0; i<=num; i++)
valid[i]=true;
for(int i=0;i<=num;i++)
{
belong[i]=0;
}
int* array=new int[num];//an array to store all the AP
int edge1,edge2;
int back1,back2=0;
while(!in.eof())
{
in>>edge1;
in>>edge2;
if (back1==edge1 &&back2==edge2)
{
break;
}
back1=edge1;//backup
back2=edge2;//backup, avoid repeating
g.addEdge(edge1,edge2);
}
int flag=0;// test case, set 1 to ignore
//g.printGraph(num,valid);//initial graph
while(j!=0||flag==0)//we want to get SCC first
{
cout<<"a new loop!"<<endl;
if(j==0)
flag=1;
timee=0;
while(!st.empty())//clear
{
st.pop();
}
for(int i=0; i<=num; i++)
{
visit[i]=0;
dfnn[i]=0;
loww[i]=0;
inStack[i]=0;
}
for(int i=0; i<j; i++)
{
g.deleteVertex(array[i],valid);
}
j=0;//renew
for(int i =0; i<=num; i++)
if(visit[i] == 0&&valid[i])
{
g.tarjan(i,1);
}
int current_scc=scc;
//cout<<"current scc is "<<current_scc<<endl;
for(int k =0; k<=num; k++)
{
scc=0;
if(valid[k])//test by deleting it first
{
//cout<<"We are trying to delete "<<k<<endl;
Graph gg(g);
gg.deleteVertex(k,valid);
//gg.printGraph(num,valid);
timee=0;
while(!st.empty())//clear
{
st.pop();
}
for(int i=0; i<=num; i++)
{
visit[i]=0;
dfnn[i]=0;
loww[i]=0;
inStack[i]=0;
}
for(int i =0; i<=num; i++)
if(visit[i] == 0&&valid[i])
{
gg.tarjan(i,0);
}
int temp_scc=scc;
cout<<"after trying to delete "<<k<<" we get "<<temp_scc<<" before "<<current_scc<<endl;
if(temp_scc>current_scc)
{
cout<<"find one "<<k<<endl;
array[j++]=k;
}
valid[k]=true;//resume it!
}
else//this vertex is not valid for some reason
{
continue;
}
}
print(array);
}
//The output is several SCCs without any articulation point
timee=0;
while(!st.empty())//clear
{
st.pop();
}
for(int i=0; i<=num; i++)
{
visit[i]=0;
dfnn[i]=0;
loww[i]=0;
inStack[i]=0;
}
for(int i=0; i<j; i++)
{
g.deleteVertex(array[i],valid);
}
j=0;//renew
for(int i =0; i<=num; i++)
if(visit[i] == 0&&valid[i])
{
g.tarjan(i,2);//only keep elements at least 2 of SCC(s)
}
for(int i=0;i<=num;i++)
if(valid[i]&&belong[i]<large)
{
cout<<"vertex "<<i<<" is not in the biggest SCC(s) because it belongs to a SCC whom has "<<belong[i]<<" elements"<<endl;
g.deleteVertex(i,valid);
}
g.printGraph(num,valid);
off=clock();
out<<"Runtime is "<<off-start<<" ms"<<endl;
out.close();
return 0;
}