-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
752 lines (607 loc) · 19.6 KB
/
graph.h
File metadata and controls
752 lines (607 loc) · 19.6 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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#ifndef GRAPH_H
#define GRAPH_H
#include <limits>
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <queue>
#include <stack>
/*****************************************/
/* GLOABL CONSTANTS */
/*****************************************/
constexpr const int MAX_NODES = 25; // global max to limit the size of the adj.-matrix
constexpr const double NO_ARC = std::numeric_limits<double>::max();
constexpr const double INFINITY = std::numeric_limits<double>::max();
const std::string out_of_range_error = "The index must be between 0 and" + MAX_NODES;
/*****************************************/
/* GRAPH */
/*****************************************/
/* Adj. Matrix implementation of a directed Graph
* uses string as data, could(should) be replaced by a template argument
*/
class Graph{
protected:
/* Vertex helper-class
* Represents a single node of the graph
*/
class Vertex{
public:
Vertex(){
living = visited = false;
indegree = outdegree = ord = 0;
data = "";
}
bool living, visited;
int indegree, outdegree, ord;
std::string data;
};
/*****************************************/
/* ATTRIBUTES */
/*****************************************/
Vertex vertex[MAX_NODES]; // stores the nodes
double arc[MAX_NODES][MAX_NODES]; // the adj-matrix
size_t size;
size_t num_arcs;
public:
/*****************************************/
/* GETTER & SETTER */
/*****************************************/
/*
*
*/
size_t Size(){
return this->size;
}
/*
*
*/
size_t numArcs(){
return this->num_arcs;
}
/*
*
*/
std::string getData(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].data;
} // end getData
/*
*
*/
int getIndegree(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].indegree;
} // end getIndegree
/*
*
*/
int getOutdegree(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].outdegree;
} // end getOurdegree
/*
*
*/
int getOrd(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].ord;
} // end getOrd
/*
*
*/
bool getVisited(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].visited;
} // end getVisited
/*
*
*/
void setVisited(const int& n, const bool& value){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
vertex[n].visited = value;
} // end setVisited
/* sets all nodes visited-attr. to the given value
*
*/
void setAllVisited(const bool& value){
for(int i=firstVertex(); i!=-1; i=nextVertex(i)){
setVisited(i,value);
}
} // end setAllVisited
bool getAlive(const int& n){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return vertex[n].living;
}
double getArc(const int& from, const int& to){
if(from < 0 || from > MAX_NODES)
throw std::out_of_range(out_of_range_error);
if(to < 0 || to > MAX_NODES)
throw std::out_of_range(out_of_range_error);
return arc[from][to];
}
/*****************************************/
/* TRAVERSAL */
/*****************************************/
/* returns the index of the first living node
* -1 if empty
*/
int firstVertex(){
for(int i=0; i<MAX_NODES; ++i){
if(vertex[i].living)
return i;
}
return -1;
} // end firstVertex
/* returns the index of the next living node after the given one
* -1 if there isn't one
*/
int nextVertex(const int& from){
if(from < 0 || from > MAX_NODES)
return -1;
for(int i=from+1; i<MAX_NODES; i++){
if(vertex[i].living)
return i;
}
return -1;
} // end nextVertex
/* returns the index of the first outgoing arc from the given node
* -1 if there isn't one
*/
int firstArc(const int& from){
if(from < 0 || from > MAX_NODES)
return -1;
for(int to=0; to<MAX_NODES; to++){
if(arc[from][to] < NO_ARC)
return to;
}
return -1;
} // end firstArc
/* returns the index of the next outgoing arc after the given one
* -1 if there isn't one
*/
int nextArc(const int& from,int to){
if(from < 0 || from > MAX_NODES)
return -1;
if(to < 0 || to > MAX_NODES)
return -1;
for(to++; to<MAX_NODES; to++){
if(arc[from][to] < NO_ARC)
return to;
}
return -1;
} // end nextArc
/*****************************************/
/* CONSTRUCTORS */
/*****************************************/
/* Constructor
*
*/
Graph(){
size = 0;
// init the nodes and matrix to 0
for(size_t i=0; i<MAX_NODES; i++){
vertex[i] = Vertex();
for(size_t j=0; j<MAX_NODES; j++){
arc[i][j] = NO_ARC;
}
}
} // end constructor
/* copy-constructor
*
*/
Graph(const Graph& other){
size = other.size;
// init the nodes and matrix to 0
for(size_t i=0; i<MAX_NODES; i++){
vertex[i] = other.vertex[i];
for(size_t j=0; j<MAX_NODES; j++){
arc[i][j] = other.arc[i][j];
}
}
} // end copy constructor
/* Destructor
*
*/
~Graph(){
// nothign to delete here
} // end destructor
/*****************************************/
/* MEMBER FUNCTIONS */
/*****************************************/
/* inserts a new vertex to the graph
* retunrs -1 if node(n) already is alive
*/
int insertVertex(const int& n, const std::string& data){
if(n < 0 || n > MAX_NODES)
throw std::out_of_range(out_of_range_error);
if(vertex[n].living)
return -1;
vertex[n].living = true;
vertex[n].data = data;
size++;
return 0;
} // end insertVertex
/* inserts a new Arc between the given nodes
* return -1 if one of the arcs isnt alive
*/
int insertArc(const int& from,const int& to, const double& weight = 0){
if(from < 0 || from > MAX_NODES)
throw std::out_of_range(out_of_range_error);
if(from < 0 || from > MAX_NODES)
throw std::out_of_range(out_of_range_error);
if(!vertex[from].living || !vertex[to].living)
return -1;
arc[from][to] = weight;
vertex[from].ord++;
vertex[from].outdegree++;
vertex[to].ord++;
vertex[to].indegree++;
return 0;
} // end insertArc
/* inserts an arc from a->b and b->a, thus acting like an undirected arc
*
*/
int insertBiArc(const int& from, const int& to, const double weight=0){
insertArc(from,to,weight);
return insertArc(to,from,weight);
} // end insertBiArc
/*****************************************/
/* PRINT FUNCITONS */
/*****************************************/
/* prints a single node to the screen
* n | data | indegree | outdegree | \n
* string_limit limits the length of the data-string to the given value
*/
void printVertex(const int& n, const size_t string_limit = 15){
if(n < 0 || n > MAX_NODES)
return;
std::cout << n << ": " << vertex[n].data.substr(0,string_limit);
std::cout << "\tIndegree: " << vertex[n].indegree;
std::cout << "\tOutdegree: " << vertex[n].outdegree << '\n';
} // end printVertex
/* calls the printVertex function for all living nodes
* string_limit limits the length of the data-strings to the given value
*/
void printVertecies(const size_t string_limit = 15){
std::cout << "All vertecies: \n";
for(int i=firstVertex(); i!=-1; i=nextVertex(i)){
printVertex(i,string_limit);
}
} // end printVertecies
/* prints the adj-matrix to the console
*
*/
void printAdj(){
std::cout << "Adj.-Matrix:\n";
// first row of indices
std::cout << " \t";
for(int i=firstVertex(); i!=-1; i=nextVertex(i)){
std::cout << i << '\t';
}
std::cout << '\n';
for(int i=firstVertex(); i!=-1; i=nextVertex(i)){
std::cout << std::setw(2) << i << ": " << '\t';
for(int j=firstVertex(); j!=-1; j=nextVertex(j)){
if(arc[i][j] < NO_ARC){
std::cout << std::setprecision(3) << arc[i][j] << '\t';
}
else{
std::cout << " \t";
}
}
std::cout << '\n';
}
} // end printAdj
};
/*****************************************/
/* DIJKSTRA ALGORITHM */
/*****************************************/
typedef std::pair<int,double> pair;
/* Compare class to sort the std::priority_queue
*
*/
class ComparePair{
public:
bool operator()(pair p1,pair p2){
return p1.second < p2.second;
}
};
/* implementation of the Dijkstra shortest path algorithm
*
*/
void Dijkstra(Graph* g, const int& start){
if(start < 0 || start > MAX_NODES)
throw std::out_of_range(out_of_range_error);
if(!g->getAlive(start))
return;
std::cout << "Dijkstra starting from: " << g->getData(start).substr(0,15)<<'(' << start << ')' << "\n\n";
double* dist = new double[MAX_NODES]; //stores the calced distance to all nodes
int* prev = new int[MAX_NODES]; // stores the previos node to get the shortest path
// init distances & prev-nodes & visited
for(size_t i=0; i<MAX_NODES; i++){
dist[i] = INFINITY;
prev[i] = -1;
}
g->setAllVisited(false);
std::priority_queue<pair,std::vector<pair>,ComparePair> q;
// set values for the first node
dist[start] = 0;
prev[start] = -2;
g->setVisited(start,true);
q.push(pair(start,0));
// main loop
while(!q.empty()){
// get the arc with the shortest distance nad remove it from the q
pair current = q.top();
q.pop();
// go trough every outgoing connection
for(int i=g->firstArc(current.first); i!=-1; i=g->nextArc(current.first,i)){
// calc the distance
double distance = g->getArc(current.first,i) + dist[current.first];
// update the distance
if(distance < dist[i] && g->getVisited(i)){
dist[i] = distance;
prev[i] = current.first;
}
// set the first distance and push to the q
else if(!g->getVisited(i)){
g->setVisited(i,true);
dist[i] = distance;
prev[i] = current.first;
q.push(pair(i,distance));
}
}
} // end main loop
// output for every node
for(int i=g->firstVertex(); i!=-1; i=g->nextVertex(i)){
std::cout << g->getData(i) << ": " << dist[i] << '\n';
int j=i;
// put the path onto the stack
std::stack<int> path;
while(prev[j]!=-2){
path.push(j);
j = prev[j];
}
path.push(start);
// print the path in order
while(!path.empty()){
std::cout << "->" << g->getData(path.top());
path.pop();
}
std::cout << "\n\n";
}
// freeing up memory
delete[] dist;
delete [] prev;
} // end Dijkstra
/*****************************************/
/* DFS TRAVERSAL */
/*****************************************/
/* recursive helper method for dfs
*
*/
void _dfs(Graph& g,const int& s, const size_t& string_limit=15){
std::cout << g.getData(s).substr(0,string_limit) << ' ';
g.setVisited(s,true);
for(int i=g.firstArc(s);i!=-1; i=g.nextArc(s,i)){
if(!g.getVisited(i)){
_dfs(g,i,string_limit);
}
}
} // end _dfs
/* Depth-First-Search Traversal of the graph
* doesnt include segment checking, just the nodes
* that can be reached from the start-node
*/
void DFS(Graph& g,const int& start, const size_t& string_limit=15){
if(start < 0 || start > MAX_NODES)
throw std::out_of_range(out_of_range_error);
std::cout << "DFS starting from: " << start << '\n';
// init visited value
g.setAllVisited(false);
_dfs(g,start,string_limit);
std::cout << '\n';
} // end DFS
/*****************************************/
/* BFS TRAVERSAL */
/*****************************************/
/* Breadth-First-Search Traversal of the graph
*
*/
void BFS(Graph& g,const int& start){
if(start < 0 || start > MAX_NODES)
throw std::out_of_range(out_of_range_error);
std::cout << "BFS starting from: " << start << '\n';
// init visited values
g.setAllVisited(false);
std::queue<int> q;
q.push(start);
// main loop
while(!q.empty()){
// get first node & visit (set visited & print)
int current = q.front();
g.setVisited(current,true);
std::cout << g.getData(current) << ' ';
q.pop();
// push every adjacent node that hasn't been visited to the q
for(int i=g.firstArc(current); i!=-1; i=g.nextArc(current,i)){
if(!g.getVisited(i)){
g.setVisited(i,true);
q.push(i);
}
}
}
std::cout << '\n';
} // end BFS
/*****************************************/
/* TOPSORT (KAHN ALGORITHM) */
/*****************************************/
/* topological sorting using the indegree of each node
*
*/
void topsort(Graph& g){
std::vector<int> order;
std::queue<int> q;
int indegree[MAX_NODES];
int count{0};
for(int i=0; i<MAX_NODES; i++){
indegree[i] = g.getIndegree(i);
}
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
if(g.getIndegree(i) == 0){
q.push(i);
}
}
while(!q.empty()){
int current = q.front();
q.pop();
order.push_back(current);
for(int i=g.firstArc(current); i!=-1; i=g.nextArc(current,i)){
--indegree[i];
if(indegree[i] == 0){
q.push(i);
}
}
count++;
if(count > g.Size()){
std::cout << "The graph is cyclic! \n";
return;
}
}
std::cout << "Topsort: \n";
for(auto it=order.begin(); it!=order.end(); it++){
std::cout << g.getData(*it) << '\n';
}
}
/*****************************************/
/* MINIMAL SPANNING TREE */
/*****************************************/
struct arc{
arc(int from_,int to_,double weight_){
from = from_;
to = to_;
weight = weight_;
}
int from,to;
double weight;
};
class CompareArc{
public:
bool operator()(arc a1, arc a2){
return a1.weight > a2.weight;
}
};
/* creates a minimal spanning tree from the given graph
*
*/
Graph* MST(Graph& g){
Graph* ret = new Graph();
g.setAllVisited(false);
std::priority_queue<arc,std::vector<arc>,CompareArc> q;
// copy vertecies from old graph
// and put all arcs into the queue
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
ret->insertVertex(i,g.getData(i));
for(int j=g.firstArc(i); j!=-1; j=g.nextArc(i,j)){
q.push(arc(i,j,g.getArc(i,j)));
}
}
// main loop
while(!q.empty()){
// take the smallest arc
arc current = q.top();
q.pop();
// if one of the both nodes hasnt been visited
// add it to the new graph
if(!g.getVisited(current.from)){
ret->insertBiArc(current.from,current.to,current.weight);
g.setVisited(current.from,true);
g.setVisited(current.to,true);
}
else if(!g.getVisited(current.to)){
ret->insertBiArc(current.from,current.to,current.weight);
g.setVisited(current.from,true);
g.setVisited(current.to,true);
}
}
return ret;
}
/* returns the sum of all arc weights
*
*/
double totalGraphWeight(Graph& g){
double sum{0};
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
for(int j=g.firstArc(i); j!=-1; j=g.nextArc(i,j)){
sum+=g.getArc(i,j);
}
}
return sum;
}
/*****************************************/
/* Floyd-Warshall Shortest Path */
/*****************************************/
/* calculates the shortest path for all nodes
* for this implementation to work, all nodes have
* to be inserted in sequence
*/
void FloydWarshall(Graph& g){
std::cout << "Floyd-Warshall: \n";
// create distance matrix
double** dist = new double*[g.Size()];
for(int i=0; i<g.Size(); i++){
dist[i] = new double[g.Size()];
}
for(int i=0; i<g.Size(); i++){
for(int j=0; j<g.Size(); j++){
dist[i][j] = INFINITY;
}
}
// copy distances from graph
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
for(int j=g.firstVertex(); j!=-1; j=g.nextVertex(j)){
dist[i][j] = g.getArc(i,j);
}
}
// set distances to self to 0
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
dist[i][i] = 0;
}
// calc the min distances
for(int k=g.firstVertex(); k!=-1; k=g.nextVertex(k)){
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
for(int j=g.firstVertex(); j!=-1; j=g.nextVertex(j)){
if(dist[i][j] > dist[i][k] + dist[k][j]){
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
// first row of indices
std::cout << " \t";
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
std::cout << i << '\t';
}
std::cout << '\n';
for(int i=g.firstVertex(); i!=-1; i=g.nextVertex(i)){
std::cout << std::setw(2) << i << ": " << '\t';
for(int j=g.firstVertex(); j!=-1; j=g.nextVertex(j)){
if(dist[i][j] < NO_ARC){
std::cout << std::setprecision(3) << dist[i][j] << '\t';
}
else{
std::cout << " \t";
}
}
std::cout << '\n';
}
}
#endif // GRAPH_H