-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
583 lines (542 loc) · 11.2 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
Going deeper:1.When the cache miss happened
2.how large the size is the algorithm become ineffictive
3.why using deque is faster
4.the performance in HPC Cluster against the performance in desktop
5.
*/
#include <iostream>
#include <fstream>
#include <stack>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cmath>
#include <deque>
#include <queue>
#include <string>
#include <sstream>
//#include "Vertice.h"
//#include "Graph.h";
using namespace std;
using std::vector;
using std::pair;
using std::stack;//neccessary
using std::queue;
struct vertice {
size_t postOrder = 0;
size_t preOrder = 0;
size_t ccNum = 0;
size_t index = 0;
bool visited = false;
};
size_t cc = 1;//count isolated components
size_t preCounter,postCounter;
vertice* v;//record the status
vector<vector<int> > adj;//record the neighbours
//void printV(vertice v) {
// size_t i = v.index;
// size_t n = adj[i].size();
// if (n == 0) {
// puts("");
// return;
// }
// else {
// for (size_t j = 0; j < n; j++) {
// cout << adj[i][j] << " ";
// }
// puts("");
// }
//}
void Print_data() {
int n = adj.size();
for (int i = 0; i < n; i++) {
int m = adj[i].size();
cout << i << ": ";
for (int j = 0; j < m; j++) { cout << adj[i][j] << " ,"; }
cout << endl;
}
}
size_t random(int r, int b) {
size_t random;
random=(rand() % (b-r+1)) +r;//represents [r,b]
// cout << random << endl;
return random;
}
bool isExplored() {
size_t n = adj.size();
bool eFlag =true;
for (size_t i = 0; i < n; i++)
{
if (!v[i].visited) {
eFlag = false;
cout << "Traversal incomplete"<<endl;
break;
}
}
return eFlag;
}
bool equal(vector<int> &temp,size_t x) {
size_t n = temp.size();
bool equal = false;
if (n == 0) {
return false;
}
else {
for (size_t c = 0; c < n; c++)
{
if (c >= n || c < 0) {
cout << "vetcor out of range" << endl;
break; }
if (temp[c] == x)
{
equal = true;
return equal;
}
}
return equal;
}
}
void generateD(size_t n, size_t m)
{
cout << "Directed graph generated:" << endl;
cout << "n=" << n << " m=" << m << endl;
adj = vector<vector<int> >(n, vector<int>());
v = new vertice[n];
for (size_t i = 0; i < n; i++) {
v[i].index = i;
}
for (size_t i = 0; i < m; i++)//push m lines in template vector
{
size_t j, k;
j = random(1, n); //[1,n]
while (true) {
if (adj[j - 1].size() == n - 1)
{
j = random(1, n);
}
else
break;
}
k = random(1, n);
while (true) {
if (j == k) {
k = random(1, n);
}
else break;
}
if (!equal(adj[j - 1], k - 1)) // if elements are not repeatd or are empty, return false, push element to the back
{
adj[j - 1].push_back(k - 1);
}
else {
//make sure there are exactly m edges in the graph, in case the vector has the same elements.
while (true)
{
k = random(1, n);
if (!equal(adj[j - 1], k - 1)&&k!=j)//
{
adj[j - 1].push_back(k - 1);
break;
}
};
}
}
//if (n < 25) {
// for (size_t i = 0; i < n; i++)
// {
// cout << i << ": ";
// printV(v[i]);
// }
//}
}
void generateU(size_t n, size_t m) {
cout << "Undirected graph generated:" << endl;
cout << "n=" << n << " m=" << m << endl;
adj = vector<vector<int> >(n, vector<int>());
v = new vertice[n];
for (size_t i = 0; i < n; i++) {
v[i].index = i;
}
for (size_t i = 0; i < m; i++)//push m lines in template vector
{
size_t j, k;
j = random(1, n); //[1,n]
while (true) {
if (adj[j - 1].size() == n - 1)
{
j = random(1, n);
}
else
break;
}
k = random(1, n);
while (true) {
if (j == k) {
k = random(1, n);
}
else break;
}
if (!equal(adj[j - 1], k - 1) && !equal(adj[k - 1], j - 1)) // if elements are not repeatd or are empty, return false, push element to the back
{
adj[j - 1].push_back(k - 1);
adj[k - 1].push_back(j - 1);
}
else {
//make sure there are exactly m edges in the graph, in case the vector has the same elements.
while (true)
{
k = random(1, n);
if (!equal(adj[j - 1], k - 1) && !equal(adj[k - 1], j - 1)&&k != j)
{
adj[j - 1].push_back(k - 1);
adj[k - 1].push_back(j - 1);
break;
}
};
}
}
//if (n < 25) {
// for (size_t i = 0; i < n; i++)
// {
// cout << i << ": ";
// printV(v[i]);
// }
//}
}
void userInterface()
{
size_t a;
size_t n=0, m=0;
cout << "Welcome to algorithms test, please select the type of the graph:" << "\n 1.small 2.large\n" << endl;
cin >> a;
switch (a)
{
case 1:
cout << "You selected small.\n" << endl;
n = random(5, 20);
break;
case 2:
cout << "You selected small.\n" << endl;
n = random(1000, 100000);
break;
default:
cout << "Input illegal.\n" << endl;
break;
}
cout << " 1.sparse 2.dense\n" << endl;
cin >> a;
switch (a)
{
case 1:
cout << "You selected sparse.\n"<<endl;
m = n+2;
break;
case 2:
cout << "You selected dense.\n" << endl;
m= (size_t)(pow(n,2)-n-2)/2;//undirected
break;
default:
cout << "Input illegal.\n" << endl;
break;
}
cout << " 1.directed 2.undirected\n" << endl;
size_t b;
cin >> b;
switch (b) {
case 1:
cout << "You selected directed.\n" << endl;
generateD(n, m);
break;
case 2:
cout << "You selected undirected.\n" << endl;
generateU(n, m);
break;
default:
cout << "Input illegal.\n" << endl;
break;
}
cout << "Print the graph?(Recommended when small graph is selected)" << "\n 1.Yes 2.No\n" << endl;
cin >> a;
switch (a)
{
case 1:
//for (size_t i = 0; i < n; i++)
//{
// cout << i << ": ";
// printV(v[i]);
//}
break;
case 2:
break;
default:
cout << "Input illegal.\n" << endl;
break;
}
};
void explore(int i)
{
if (!v[i].visited)
{
v[i].visited = true;
v[i].ccNum = cc;
int n = adj[i].size();
if (!adj[i].empty())
{
for (int j = 0; j < n; j++)
{int w = adj[i][j];//acutal index in the vector
explore(w);
}
v[i].postOrder = postCounter++;
}
else {
v[i].postOrder = postCounter++;
return; }//if the vertice is sink, return the upper level
}
else {
return;} //if visited, return immediately
}
void dfsRecursive() //the naive implementation of directed dfs
{
postCounter = 0;
int n = adj.size();
for (int w = 0; w < n; w++) {
if (!v[w].visited) {
explore(w);
cc++;
}
}
}
void dfsStack() {//using naive c++ stl stack
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
stack<vertice> S;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;//start point v[0]
S.push(v[k]);
while (!S.empty())
{
vertice u = S.top();
S.pop();//these two steps represent the operation pop
size_t m = adj[u.index].size();
int index = -1;
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
index = i;
break;
}
}
if (index != -1)
{
S.push(u);
v[index].visited = true;
S.push(v[index]);
}
}
}
S.~stack<vertice>();//release memory
}
void dfsDeque() {
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
deque<vertice> D;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;//start point v[0]
D.push_front(v[k]);
while (!D.empty())
{
vertice u = D.front();
D.pop_front();
size_t m = adj[u.index].size();
int index = -1;
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
index = i;
break;
}
}
if (index != -1)
{
D.push_front(u);
v[index].visited = true;
D.push_front(v[index]);
}
}
}
D.~deque<vertice>();//release memory
}
void showOrder() {
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
cout << i << ": " << v[i].postOrder << endl;
}
}
void bfsQueue()
{
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
queue<vertice> Q;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) {
continue;
}
else
v[k].visited = true;
Q.push(v[k]);
while (!Q.empty())
{
vertice u = Q.front();
Q.pop();//these two steps represent the operation dequeue()
size_t m = adj[u.index].size();
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
v[i].visited = true;
Q.push(v[i]);
}
}
}
}
Q.~queue<vertice>();
}
void bfsDeque() {
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
deque<vertice> D;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;
D.push_back(v[k]);
while (!D.empty())
{
vertice u = D.front();
D.pop_front();//these two steps represent the operation dequeue()
size_t m = adj[u.index].size();
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
v[i].visited = true;
D.push_back(v[i]);
}
}
}
}
D.~deque<vertice>();
}
void test() {
clock_t t1;//get current time
t1 = clock();
dfsStack();
clock_t t2;//get current time
t2 = clock();
double dif1;
dif1 = difftime(t2, t1);
cout << dif1 << endl;
}
/*The function inputD is used to manually input the graph
input format:
* n m(first line)
* x1 y1
* x2 y2
* ¡
* xm ym
*/
int Max(size_t a, size_t b, size_t c)
{
size_t max;
if (a >= b)
{
if (a >= c) {
max = a;
}
else
max = c;
}
else if (b >= c) { max = b; }
else max = c;
return max;
}
void compareDFS() {
clock_t t1,t2,t3;
t1 = clock();//get current time
dfsStack();
t2 = clock();
double dif1,dif2;
dif1 = difftime(t2, t1);
dfsDeque();
t3 = clock();
dif2 = difftime(t3, t2);
cout << "Naive stack takes " <<dif1<<" ms" <<endl;
cout << "Deque takes " << dif2 << " ms" << endl;
}
void Input_data(const string& filename) {
std::fstream in(filename.c_str());
cout << "reading file:" << filename << endl;
string s;
size_t n = 0, m = 0;
string data1, data2;
while (true)
{
std::getline(in, s);
istringstream is(s);
is >> data1 >> data2;
int d1 = stoi(data1);
int d2 = stoi(data2);
n = Max(n, d2, d1);
m += 1;
if (in.eof()) { break; }
}
//this block will count the number of lines and calculate the maximun number appeared in the file, which are the parameters n, m(vertice, edge)
in.seekg(0, ios::beg);
n += 1;
adj = vector<vector<int> >(n, vector<int>());
for (size_t i = 0; i < m; i++)
{
int x, y;
std::getline(in, s);
istringstream is(s);
is >> data1 >> data2;
x = stoi(data1);
y = stoi(data2);
adj[x].push_back(y);
}
in.close();
//this block will assign data into the vertice template in terms of the adjancancy list
}
int main(){
Input_data("out.txt");
//srand((int)time(NULL)); // generate random seeds, use and only use once
//generateD(1000,5000);
Print_data();
// compareDFS();
// isExplored();
return 0;
}