-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
279 lines (223 loc) · 7.19 KB
/
Graph.cpp
File metadata and controls
279 lines (223 loc) · 7.19 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
/*---Graph.cpp-----------------------------------------------------------------
* Thomas Matlak
*----------------------------------------------------------------------------*/
#include "Graph.h"
#include <random>
#include <iostream>
#include <climits> // MAX_INT
Graph::Graph(int setNumNodes, int setMaxEdgeLength)
{
numNodes = setNumNodes;
maxEdgeLength = setMaxEdgeLength;
// randomly make connections between nodes
std::random_device connectNode;
std::mt19937 eng(connectNode());
std::uniform_int_distribution<> distr(1, maxEdgeLength);
std::random_device doConnectNode;
std::mt19937 eng2(doConnectNode());
std::uniform_int_distribution<> distr2(0, numNodes);
nodes.resize(numNodes, nullptr);
edges.resize(numNodes, std::vector<int>(numNodes, 0));
initialEdges.resize(numNodes, std::vector<int>(numNodes, 0));
for (size_t i = 0; i < edges.size(); i++)
{
for (size_t j = 0; j < edges[i].size(); j++)
{
if (i != j)
{
if ((distr2(eng2) % 3) == 0) // control the frequency of edges
{
int randLength = distr(eng);
edges[i][j] = randLength;
initialEdges[i][j] = randLength;
}
}
}
}
// set up node adjacency
for (size_t i = 0; i < edges.size(); i++)
{
Node* node = new Node;
nodes[i] = node;
for (size_t j = 0; j < edges[i].size(); j++)
{
if (edges[i][j] > 0) {
nodes[i]->adjacentNodes.push_back(j);
}
}
}
}
Graph::~Graph()
{
for (size_t i = 0; i < nodes.size(); i++)
{
delete nodes[i];
}
}
void Graph::printGraph()
{
std::cout << "NODES ADJACENT NODES" << std::endl;
for (int m = 0; m < numNodes; m++)
{
std::cout << m << ": ";
for (size_t n = 0; n < nodes[m]->adjacentNodes.size(); n++)
{
std::cout << nodes[m]->adjacentNodes[n] << " ";
}
std::cout << std::endl;
}
std::cout << "EDGES:" << std::endl << " ";
for (int f = 0; f < numNodes; f++)
{
std::cout << f << " ";
}
std::cout << std::endl << " ";
for (int f = 0; f < numNodes; f++)
{
std::cout << "- ";
}
std::cout << std::endl;
for (size_t i = 0; i < edges.size(); i++)
{
std::cout << i << "|";
for (size_t j = 0; j < edges[i].size(); j++)
{
std::cout << edges[i][j] << " ";
}
std::cout << std::endl;
}
}
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(std::vector<int> & dist, std::vector<bool> & sptSet, Graph & graph)
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < graph.numNodes; v++)
{
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
}
return min_index;
}
std::deque<int> Graph::traverseSelfish(int start, int dest)
{
std::vector<int> dist; // hold the distances from each node to start
dist.resize(numNodes, INT_MAX);
std::vector<bool> sptSet; // is the node included in the shortest path tree
sptSet.resize(numNodes, false);
std::vector<int> prev;
prev.resize(numNodes, -1);
std::deque<int> shortestPath;
dist[start] = 0;
// find the shortest path for each vertex
for (int i = 0; i < numNodes; i++) // for each node in the graph
{
int u = minDistance(dist, sptSet, *this);
if (u == dest) break;
sptSet[u] = true;
// for each node adjacent to u
for (size_t j = 0; j < nodes[u]->adjacentNodes.size(); j++)
{
int adjNode = nodes[u]->adjacentNodes[j];
// find travel time based solely on distance
if ((!sptSet[adjNode]) && (initialEdges[u][adjNode])
&& (dist[u] != INT_MAX)
&& ((dist[u] + initialEdges[u][adjNode]) < dist[adjNode]))
{
// actual travel distance is still based on congestion though
dist[adjNode] = dist[u] + edges[u][adjNode];
// each player adds congesting to the edges they traverse
edges[u][adjNode]++;
prev[adjNode] = u;
}
}
}
int u = dest;
while (prev[u] != -1)
{
shortestPath.push_front(u);
u = prev[u];
}
shortestPath.push_front(u);
return shortestPath;
}
std::deque<int> Graph::traverseOptimal(int start, int dest)
{
std::vector<int> dist; // hold the distances from each node to start
dist.resize(numNodes, INT_MAX);
std::vector<bool> sptSet; // is the node included in the shortest path tree
sptSet.resize(numNodes, false);
std::vector<int> prev;
prev.resize(numNodes, -1);
std::deque<int> shortestPath;
dist[start] = 0;
// find the shortest path for each vertex
for (int i = 0; i < numNodes; i++) // for each node in the graph
{
int u = minDistance(dist, sptSet, *this);
if (u == dest) break;
sptSet[u] = true;
// for each node adjacent to u
for (size_t j = 0; j < nodes[u]->adjacentNodes.size(); j++)
{
int adjNode = nodes[u]->adjacentNodes[j];
// find travel time based on distance + congestion
if ((!sptSet[adjNode]) && (edges[u][adjNode])
&& (dist[u] != INT_MAX)
&& ((dist[u] + edges[u][adjNode]) < dist[adjNode]))
{
dist[adjNode] = dist[u] + edges[u][adjNode];
// each player adds congesting to the edges they traverse
edges[u][adjNode]++;
prev[adjNode] = u;
}
}
}
int u = dest;
while (prev[u] != -1)
{
shortestPath.push_front(u);
u = prev[u];
}
shortestPath.push_front(u);
return shortestPath;
}
int Graph::traversalDistance(int start, int dest)
{
std::vector<int> dist; // hold the distances from each node to start
dist.resize(numNodes, INT_MAX);
std::vector<bool> sptSet; // is the node included in the shortest path tree
sptSet.resize(numNodes, false);
dist[start] = 0;
// find the shortest path for each vertex
for (int i = 0; i < numNodes; i++) // for each node in the graph
{
int u = minDistance(dist, sptSet, *this);
if (u == dest) break;
sptSet[u] = true;
// for each node adjacent to u
for (size_t j = 0; j < nodes[u]->adjacentNodes.size(); j++)
{
int adjNode = nodes[u]->adjacentNodes[j];
// find travel time based on distance + congestion
if ((!sptSet[adjNode]) && (edges[u][adjNode])
&& (dist[u] != INT_MAX)
&& ((dist[u] + edges[u][adjNode]) < dist[adjNode]))
{
dist[adjNode] = dist[u] + edges[u][adjNode];
}
}
}
return dist[dest];
}
void Graph::resetGraph()
{
for (size_t i = 0; i < edges.size(); i++)
for (size_t j = 0; j < edges.size(); j++)
edges[i][j] = initialEdges[i][j];
}
int Graph::getNumNodes()
{
return numNodes;
}