-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph Analyzer.cpp
More file actions
712 lines (674 loc) · 19.7 KB
/
Graph Analyzer.cpp
File metadata and controls
712 lines (674 loc) · 19.7 KB
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;
struct node
{
vector<string> adj; // cost of edge, destination vertex
string name;
node() {}
node(string s) : name(s) {}
};
class graph
{
public:
typedef pair<string, string> Pss;
typedef map<string, node *> vmap;
typedef map<Pss, double> emap;
stack <string> TPS;
unordered_map<string, int> Distance_Array_As_A_Map;
unordered_map<string, string> Parent_Array_for_BFSP_Map;
unordered_map<string, bool> inprogress;
unordered_map<string, bool> finished;
unordered_map<string, string> parent;
bool cycle;
emap edges;
vmap vertices;
graph() {}
void insertNode(const string &name)
{
bool found = false;
vmap::iterator itr = vertices.find(name);
if (itr == vertices.end())
{
node *v;
v = new node(name);
vertices[name] = v;
return;
}
cout << "There is already such a node!" << endl;
;
}
void removeNode(string &name)
{
node *f = (vertices.find(name)->second);
for (int i = 0; i < f->adj.size(); i++)
{
Pss edge_name = make_pair(name,f->adj[i]);
Pss edge1_name = make_pair(f->adj[i],name);
edges.erase(edge_name);
edges.erase(edge1_name);
}
for (auto const &pair : vertices)
{
if (pair.first != name)
{
for (auto i = pair.second->adj.begin(); i != pair.second->adj.end(); ++i)
{
if (*i == name)
{
pair.second->adj.erase(i);
break;
}
}
}
}
vertices.erase(name);
delete f;
}
void Diaddedge(const string &u, const string &v, double w)
{
node *f = (vertices.find(u)->second);
Pss edge_name = make_pair(u, v);
edges[edge_name] = w;
f->adj.push_back(v);
}
void Undiaddedge(const string &u, const string &v, double w)
{
node *f = (vertices.find(u)->second);
node *t = (vertices.find(v)->second);
Pss edge_name = make_pair(u, v);
Pss edge2_name = make_pair(v, u);
edges[edge2_name] = w;
edges[edge_name] = w;
f->adj.push_back(v);
t->adj.push_back(u);
}
void Diremoveedge(string &u, string &v)
{
Pss edge_name = make_pair(u, v);
edges.erase(edge_name);
for (auto const &pair : vertices)
{
if (pair.first == u)
{
for (auto i = pair.second->adj.begin(); i != pair.second->adj.end(); ++i)
{
if (*i == v)
{
pair.second->adj.erase(i);
break;
}
}
}
}
}
void Undiremoveedge(string &u, string &v)
{
Pss edge_name = make_pair(u, v);
Pss edge2_name = make_pair(v, u);
edges.erase(edge_name);
edges.erase(edge2_name);
for (auto const &pair : vertices)
{
if (pair.first == u)
{
for (auto i = pair.second->adj.begin(); i != pair.second->adj.end(); ++i)
{
if (*i == v)
{
pair.second->adj.erase(i);
break;
}
}
}
}
for (auto const &pair : vertices)
{
if (pair.first == v)
{
for (auto i = pair.second->adj.begin(); i != pair.second->adj.end(); ++i)
{
if (*i == u)
{
pair.second->adj.erase(i);
break;
}
}
}
}
}
void DiadjustWeight(string &u, string &v, double w)
{
Pss edge_name = make_pair(u, v);
edges[edge_name] = w;
}
void UndiadjustWeight(string &u, string &v, double w)
{
Pss edge_name = make_pair(u, v);
Pss edge1_name = make_pair(v, u);
edges[edge_name] = w;
edges[edge1_name] = w;
}
};
void DiDFSearch2(graph &G, string v, node *q)
{
G.inprogress[v] = true;
cout<<v<<" ";
vector<string> p = q->adj;
for (int i = 0; i < p.size(); i++)
{
string u = p[i]; // The edge is (v,u)
if (G.inprogress[u] == false)
{
G.parent[u] = v;
DiDFSearch2(G, u, G.vertices[u]); // Recursive DFSearch call
}
else if (G.finished[u]==false)
{
G.cycle = true;
}
}
G.finished[v] = true;
}
void DiDFSearch(graph &G)
{
for (auto const &pair : G.vertices)
{
G.inprogress[pair.first] = false;
G.finished[pair.first] = false;
G.parent[pair.first] = "";
}
for (auto const &pair : G.vertices)
{
if (G.inprogress[pair.first] == false)
{
DiDFSearch2(G, pair.first, pair.second);
}
}
}
void UndiDFSearch2(graph &G, string v, node *q)
{
G.inprogress[v] = true;
cout << v << " ";
vector<string> p = q->adj;
for (int i = 0; i < p.size(); i++)
{
string u = p[i]; // The edge is (v,u)
if (G.inprogress[u] == false)
{
G.parent[u] = v;
UndiDFSearch2(G, u, G.vertices[u]); // Recursive DFSearch call
}
else if (G.finished[u] == false && G.parent[v]!=u)
{
G.cycle = true;
}
}
G.finished[v] = true;
}
void UndiDFSearch(graph &G)
{
for (auto const &pair : G.vertices)
{
G.inprogress[pair.first] = false;
G.finished[pair.first] = false;
G.parent[pair.first] = "";
}
for (auto const &pair : G.vertices)
{
if (G.inprogress[pair.first] == false)
{
UndiDFSearch2(G, pair.first, pair.second);
}
}
}
void DiDFSearch2_no_output(graph &G, string v, node *q)
{
G.inprogress[v] = true;
vector<string> p = q->adj;
for (int i = 0; i < p.size(); i++)
{
string u = p[i]; // The edge is (v,u)
if (G.inprogress[u] == false)
{
G.parent[u] = v;
DiDFSearch2_no_output(G, u, G.vertices[u]); // Recursive DFSearch call
}
else if (G.finished[u] == false)
{
G.cycle = true;
}
}
G.finished[v] = true;
}
void DiDFSearch_no_output(graph &G)
{
for (auto const &pair : G.vertices)
{
G.inprogress[pair.first] = false;
G.finished[pair.first] = false;
G.parent[pair.first] = "";
}
for (auto const &pair : G.vertices)
{
if (G.inprogress[pair.first] == false)
{
DiDFSearch2_no_output(G, pair.first, pair.second);
}
}
}
void Dicheck(graph &G)
{
G.cycle = false;
DiDFSearch_no_output(G);
if (G.cycle == true)
{
cout << "There is a cycle";
}
else
{
cout << "There is no cycle";
}
}
void UndiDFSearch2_no_output(graph &G, string v, node *q)
{
G.inprogress[v] = true;
vector<string> p = q->adj;
for (int i = 0; i < p.size(); i++)
{
string u = p[i]; // The edge is (v,u)
if (G.inprogress[u] == false)
{
G.parent[u] = v;
UndiDFSearch2_no_output(G, u, G.vertices[u]); // Recursive DFSearch call
}
else if (G.finished[u] == false && G.parent[v] != u)
{
G.cycle = true;
}
}
G.finished[v] = true;
}
void UndiDFSearch_no_output(graph &G)
{
for (auto const &pair : G.vertices)
{
G.inprogress[pair.first] = false;
G.finished[pair.first] = false;
G.parent[pair.first] = "";
}
for (auto const &pair : G.vertices)
{
if (G.inprogress[pair.first] == false)
{
UndiDFSearch2_no_output(G, pair.first, pair.second);
}
}
}
void Undicheck(graph &G)
{
G.cycle = false;
UndiDFSearch_no_output(G);
if (G.cycle == true)
{
cout << "There is a cycle";
}
else
{
cout << "There is no cycle";
}
}
void DiDFSearch2_TPS(graph &G, string v, node *q)
{
G.inprogress[v] = true;
vector<string> p = q->adj;
for (int i = 0; i < p.size(); i++)
{
string u = p[i]; // The edge is (v,u)
if (G.inprogress[u] == false)
{
G.parent[u] = v;
DiDFSearch2_TPS(G, u, G.vertices[u]); // Recursive DFSearch call
}
else
{
if (G.finished[u] == false)
{
G.cycle = true;
}
}
}
G.finished[v] = true;
G.TPS.push(v);
}
void DiDFSearch_TPS(graph &G)
{
for (auto const &pair : G.vertices)
{
G.inprogress[pair.first] = false;
G.finished[pair.first] = false;
G.parent[pair.first] = "";
}
for (auto const &pair : G.vertices)
{
if (G.inprogress[pair.first] == false)
{
DiDFSearch2_TPS(G, pair.first, pair.second);
}
}
}
void Topological_Sort(graph &G)
{
while (G.TPS.empty() == false)
{
cout << G.TPS.top() << " ";
G.TPS.pop();
}
return;
}
void FindPath2(string s, string t, unordered_map<string, string> parent)
{
if ((s == t) || (t == ""))
{
cout << s<<" ";
}
else
{
FindPath2(s, parent[t], parent);
cout << t<<" ";
}
}
void FindPath(graph &G, string s, string t)
{
for (auto const &pair : G.vertices)
{
G.Distance_Array_As_A_Map[pair.first] = INT_MAX;
G.Parent_Array_for_BFSP_Map[pair.first] = "";
}
G.Distance_Array_As_A_Map[s] = 0;
for( int i=0;i<G.vertices.size()-1;i++)
{
for (auto const &pair : G.edges)
{
if (G.Distance_Array_As_A_Map[pair.first.first] + pair.second < G.Distance_Array_As_A_Map[pair.first.second])
{
G.Parent_Array_for_BFSP_Map[pair.first.second] = pair.first.first;
G.Distance_Array_As_A_Map[pair.first.second] = G.Distance_Array_As_A_Map[pair.first.first] + pair.second;
}
}
}
for (auto const &pair : G.edges)
{
if (G.Distance_Array_As_A_Map[pair.first.second] > G.Distance_Array_As_A_Map[pair.first.first] + pair.second)
{
cout << "There is no such path, there is a negative weight cycle!";
return;
}
}
return FindPath2(s, t, G.Parent_Array_for_BFSP_Map);
return;
}
// from here//
// Function to generate the DOT file
// Function to generate the DOT file
void generateDotFile(graph &G)
{
ofstream dotFile("graph.dot", ofstream::trunc);
dotFile << "digraph G {\n";
// Write node declarations
for (auto const &pair : G.vertices)
{
dotFile << " " << pair.first << ";\n";
}
// Write edge declarations
for (auto const &pair : G.edges)
{
dotFile << " " << pair.first.first << " -> " << pair.first.second
<< " [label=\"" << pair.second << "\"];\n";
}
dotFile << "}\n";
dotFile.close();
}
// Function to display the graph
void displayGraph()
{
system("dot -Tpng graph.dot -o graph.png");
system("graph.png");
}
// to here
int main()
{
cout << "Welcome to graph creator and analyser,by Maya Ali, Nael Haidar, Toufic Al Mabsout, Youri Klim." << endl;
cout << "What type of graph would you like to create?" << endl;
cout << "Enter the number 1 for a directed graph and the number 2 for an undirected graph." << endl;
int n;
cin >> n;
typedef pair<string, string> Pss;
typedef map<string, node *> vmap;
typedef map<Pss, double> emap;
graph G;
if (n == 1)
{
cout << "Noted. A directed graph has been created." << endl;
}
else if (n == 2)
{
cout << "Noted. An undirected graph has been created." << endl;
}
else
{
cout << "value of input must be either 1 or 2!";
return 0;
}
int m = 0;
while (m != 12)
{
cout << "Enter the number of the option you would like to choose" << endl;
cout << "Available options are: " << endl;
cout << "(1): insert node" << endl;
cout << "(2): remove node" << endl;
cout << "(3): insert edge with weight" << endl;
cout << "(4): remove an edge" << endl;
cout << "(5): adjust edge weight" << endl;
cout << "(6): Perform DFS on your graph" << endl;
cout << "(7): Check for cycles" << endl;
cout << "(8): Perform Topological sort" << endl;
cout << "(9): Find shortest path between two nodes of your choice" << endl;
cout << "(10): Display current graph nodes and connections" << endl;
cout << "(11): Display the graph visually" << endl;
cout << "(12): exit the program" << endl;
cin >> m;
if (m == 1)
{
cout << "Enter the name of the node you would like to insert: " << endl;
string name;
cin >> name;
bool find = false;
vmap::iterator itr = G.vertices.find(name);
if (itr != G.vertices.end())
{
find = true;
}
G.insertNode(name);
if (find == false)
{
cout << "Node inserted successfully!" << endl;
cout << endl;
}
}
else if (m == 2)
{
cout << "Specify the name of the node you would like to remove: " << endl;
string name;
cin >> name;
G.removeNode(name);
cout << "Node removed successfully!" << endl;
cout << endl;
}
else if (m == 3)
{
cout << "Enter the names of the nodes you would like to connect" << endl;
cout << "Enter the name of the first node: " << endl;
string N1;
cin >> N1;
vmap::iterator itr = G.vertices.find(N1);
if (itr == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
cout << "Enter the name of the second node: " << endl;
string N2;
cin >> N2;
vmap::iterator itr1 = G.vertices.find(N2);
if (itr1 == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
cout << "Enter the weight of the edge" << endl;
double w;
cin >> w;
if (n == 1)
{
G.Diaddedge(N1, N2, w);
}
else if (n == 2)
{
G.Undiaddedge(N1, N2, w);
}
cout << "Edge created successfully!" << endl;
cout << endl;
}
else if (m == 4)
{
cout << "Enter the names of the nodes you would like to disconnect" << endl;
cout << "Enter the name of the first node: " << endl;
string N1;
cin >> N1;
vmap::iterator itr = G.vertices.find(N1);
if (itr == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
cout << "Enter the name of the second node: " << endl;
string N2;
cin >> N2;
vmap::iterator itr2 = G.vertices.find(N2);
if (itr2 == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
if (n == 1)
{
G.Diremoveedge(N1, N2);
}
else if (n == 2)
{
G.Undiremoveedge(N1, N2);
}
cout << "Edge removed successfully!" << endl;
cout << endl;
}
else if (m == 5)
{
cout << "Enter the names of the nodes you would like to adjust the weight between them " << endl;
cout << "Enter the name of the first node: " << endl;
string N1;
cin >> N1;
vmap::iterator itr = G.vertices.find(N1);
if (itr == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
cout << "Enter the name of the second node: " << endl;
string N2;
cin >> N2;
vmap::iterator itr3 = G.vertices.find(N2);
if (itr3 == G.vertices.end())
{
cout << "There's no such node existing!" << endl;
continue;
}
cout << "Enter the weight of the edge" << endl;
int w;
cin >> w;
if (n == 1)
{
G.DiadjustWeight(N1, N2, w);
}
else if (n == 2)
{
G.UndiadjustWeight(N1, N2, w);
}
cout << "Edge weight adjusted successfully!" << endl;
cout << endl;
}
else if (m==6){
cout<<"The nodes in DFS order are: ";
DiDFSearch(G); // works anyways for any type of graph
cout<<endl;
}
else if (m==7){
if (n==1){
Dicheck(G);}
else
{ Undicheck(G);}
cout<<endl;
}
else if (m==8){
if (n==2){
cout<<"It is an undirected graph, there is no topological sort for it!"<<endl;
}
else if (n==1){
DiDFSearch_no_output(G);
if (G.cycle == true){
cout<<"This graph contains a cycle, so there is no topological sort!"<<endl;
}
else{
DiDFSearch_TPS(G);
cout<<"Topological order is: ";
Topological_Sort(G);
cout<<endl;
}
}
}
else if (m==9){
string source,destination;
cout<<"Enter your source node: "<<endl;
cin>>source;
cout<<"Enter your destination node: "<<endl;
cin>>destination;
cout<<endl;
FindPath(G,source,destination);
cout<<endl;
}
else if(m==10){
for (auto const &pair : G.vertices)
{
cout << "node " << pair.first << endl;
for (int i = 0; i < pair.second->adj.size(); i++)
{
Pss testo = make_pair(pair.first, pair.second->adj[i]);
cout << pair.first << "-->" << pair.second->adj[i] << " weight is " << G.edges[testo] << endl;
}
}
}
else if (m==11){
generateDotFile(G);
displayGraph();
}
}
cout << "Goodbye !" << endl;
return 0;
}