/
graph_shortest_path_algos.cpp
executable file
·157 lines (148 loc) · 3.88 KB
/
graph_shortest_path_algos.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
#include "graph.h"
template <typename T>
void graph<T>::findDAGShortestPaths(node<T> *start)
{
cout << "For DAG with no negative weight edges:\n";
const int LARGENUMBER = 10000000;
stack<node <T> *> *st = new stack<node <T> *>();
topologicalSort(st);
if (hasCycle)
{
cout << "has cycle\n";
return;
}
stack<node<T> *> *st1 = st->cloneStack();
for (auto elem : nodelist)
{
node2parent[elem] = NULL;
node2shortest[elem] = LARGENUMBER;
}
node2shortest[start] = 0;
bool flag = false;
while (!st1->isEmpty())
{
node<T> *src = st1->dequeue();
if (src == start)
flag = true;
if (flag)
{
auto t_adjlist = src->get_adjlist();
for (auto elem : t_adjlist)
{
node<T> *dst = elem.first;
int w = elem.second;
cout << "src = " << src->get_data() << " and dst = " << dst->get_data() << "and w = " << w << "\n";
if (node2shortest[src] + w < node2shortest[dst])
{
node2parent[dst] = src;
node2shortest[dst] = node2shortest[src] + w;
}
}
}
}
cout << "Shortest path lengths from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
cout << elem->get_data() << ": " << node2shortest[elem] << "\n";
}
cout << "Shortest path parents from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
if (node2parent[elem])
cout << elem->get_data() << ": " << node2parent[elem]->get_data() << "\n";
}
cout << "***********************\n";
}
template <typename T>
void graph<T>::djikstra(node<T> *start)
{
cout << "DJIKSTRA:\n";
const int LARGENUMBER = 10000000;
for (auto elem : nodelist)
{
node2parent[elem] = NULL;
node2shortest[elem] = LARGENUMBER;
}
node2shortest[start] = 0;
set<node<T> *> readyset(nodelist.begin(),nodelist.end());
while (!readyset.empty())
{
int min = LARGENUMBER;
node<T> *min_elem = NULL;
for (auto elem : readyset)
{
if (node2shortest[elem] < min)
{
min = node2shortest[elem];
min_elem = elem;
}
}
readyset.erase(min_elem);
auto t_adjlist = min_elem->get_adjlist();
for (auto elem : t_adjlist)
{
node<T> *dst = elem.first;
int w = elem.second;
if (node2shortest[min_elem] + w < node2shortest[dst])
{
node2parent[dst] = min_elem;
node2shortest[dst] = node2shortest[min_elem] + w;
}
}
}
cout << "Shortest path lengths from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
cout << elem->get_data() << ": " << node2shortest[elem] << "\n";
}
cout << "Shortest path parents from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
if (node2parent[elem])
cout << elem->get_data() << ": " << node2parent[elem]->get_data() << "\n";
}
cout << "***********************\n";
}
template <typename T>
void graph<T>::bellmanford(node<T> *start)
{
cout << "BELLMAN-FORD:\n";
const int LARGENUMBER = 10000000;
for (auto elem : nodelist)
{
node2parent[elem] = NULL;
node2shortest[elem] = LARGENUMBER;
}
node2shortest[start] = 0;
for (int i = 0; i < num_nodes()-1; ++i)
{
for (auto src : nodelist)
{
auto t_adjlist = src->get_adjlist();
for (auto elem : t_adjlist)
{
node<T> *dst = elem.first;
int w = elem.second;
if (node2shortest[src] + w < node2shortest[dst])
{
node2parent[dst] = src;
node2shortest[dst] = node2shortest[src] + w;
}
}
}
}
cout << "Shortest path lengths from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
cout << elem->get_data() << ": " << node2shortest[elem] << "\n";
}
cout << "Shortest path parents from " << start->get_data() << " are:\n";
for (auto elem : nodelist)
{
if (node2parent[elem])
cout << elem->get_data() << ": " << node2parent[elem]->get_data() << "\n";
}
cout << "***********************\n";
}
template class node<int>;
template class graph<int>;