-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder.cpp
428 lines (357 loc) · 12.4 KB
/
pathfinder.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
//
// pathfinder.cpp
// pathfinder
//
// Created by Vince Mansel on 10/27/11.
// Copyright (c) 2011 Wave Ocean Software. All rights reserved.
//
/*
* ---------------
* From the user's point of view, the program draws a graph
* and provides two operations: finding the shortest path
* between two locations and constructing the minimal cost network.
* The user chooses the graph data file for the program to read and display.
*
* This is the main file with some starting constants and handy utility routines.
*/
#include "common.h"
#include "shortestPathFirst.h"
#include "minimumSpanningTree.h"
// Moved to common.h
///* Constants
// * --------
// * A few program-wide constants concerning the graphical display.
// */
//const double CircleRadius =.05; // the radius of a node
//const int LabelFontSize = 9; // for node name labels
// Moved to common.h
///* Type: coordT
// * ------------
// * Just a simple struct to handle a pair of x,y coordinate values.
// */
//struct coordT {
// double x, y;
//};
// Moved to common.h
///* Function: DrawFilledCircleWithLabel
// * Usage: DrawFilledCircleWithLabel(center, "Green", "You are here");
// * -------------------------------------------------------------------
// * Uses facilities from extgraph to draw a circle filled with
// * color specified. The circle is centered at the given coord has the
// * specified radius. A label is drawn to the right of the circle.
// * You can leave off the last argument if no label is desired.
// */
//void DrawFilledCircleWithLabel(coordT center, string color, string label = "")
//{
// MovePen(center.x + CircleRadius, center.y);
// SetPenColor(color);
// StartFilledRegion(1.0);
// DrawArc(CircleRadius, 0, 360);
// EndFilledRegion();
// if (!label.empty()) {
// MovePen(center.x + CircleRadius, center.y);
// SetFont("Helvetica");
// SetPointSize(LabelFontSize);
// DrawTextString(label);
// }
//}
// Moved to common.h
///* Function: DrawLineBetween
// * Usage: DrawLineBetween(coord1, coord2, "Black");
// * -------------------------------------------------
// * Uses facilities from extgraph to draw a line of the
// * specified color between the two given coordinates.
// */
//void DrawLineBetween(coordT start, coordT end, string color)
//{
// SetPenColor(color);
// MovePen(start.x, start.y);
// DrawLine(end.x - start.x, end.y - start.y);
//}
/* Function: GetMouseClick
* Usage: loc = GetMouseClick();
* ------------------------------
* Waits for the user to click somewhere on the graphics window
* and returns the coordinate of where the click took place.
*/
coordT GetMouseClick()
{
coordT where;
WaitForMouseDown();
WaitForMouseUp();
where.x = GetMouseX();
where.y = GetMouseY();
return where;
}
/* Function: WithinDistance
* Usage: if (WithinDistance(click, pt))...
* -----------------------------------------
* Returns true if the Cartesian distance between the two coordinates
* is <= the specified distance, false otherwise. If not specified,
* the distance is assumed to be size of the node diameter on screen.
*/
bool WithinDistance(coordT pt1, coordT pt2, double maxDistance = CircleRadius*2)
{
double dx = pt2.x - pt1.x;
double dy = pt2.y - pt1.y;
double distance = sqrt(dx*dx + dy*dy);
return (distance <= maxDistance);
}
// Moved to common.h
//struct nodeT;
//
//struct arcT {
// nodeT *start, *end;
// double distance;
//};
//
//struct nodeT {
// string name;
// double x;
// double y;
// Vector<arcT *> links;
//};
struct graphT {
Set<nodeT *> allNodes;
};
string ReadGraph(Set<nodeT *> &graph, Map<nodeT *> &graphMap, string datafile);
void PrintGraphMap(Map<nodeT *> &graphMap);
void PrintGraph(Set<nodeT *> &graph);
void DrawNodesAndArcs(Set<nodeT *> graph);
void HandleShortestPath(Set<nodeT *> &graph);
bool IsCyclicGraph(graphT & graph);
int main()
{
Set<nodeT *> graph;
Map<nodeT *> graphMap;
InitGraphics();
SetWindowTitle("Pathfinder");
string backgroundFile;
bool timeToQuit = false;
cout << "This masterful piece of work is a graph extravaganza!" << endl;
while (true) {
cout << "Your options are:" << endl;
cout << "(1) Choose a new graph data file" << endl;
cout << "(2) Find shortest path using Dijkstra's algorithm" << endl;
cout << "(3) Compute the minimal spanning tree using Kruskal's algorithm" << endl;
cout << "(4) Determine if the graph has cycles" << endl;
cout << "(5) Quit" << endl;
cout << "Enter choice: ";
int choice = GetInteger();
string datafile;
void UpdateDisplay();
switch (choice) {
case 1:
cout << "Enter the name of the data file: ";
datafile = GetLine();
//datafile = "Small.txt";
//datafile = "USA.txt";
//datafile = "Stanford.txt";
graph.clear();
graphMap.clear();
backgroundFile = ReadGraph(graph, graphMap, datafile);
break;
case 2:
if (backgroundFile == "") {
cout << "No file is specified" << endl;
}
else {
UpdateDisplay();
MovePen(0,0);
DrawNamedPicture(backgroundFile);
DrawNodesAndArcs(graph);
cout << "(2) Finding shortest path using Dijkstra's algorithm" << endl;
HandleShortestPath(graph);
}
break;
case 3:
if (backgroundFile == "") {
cout << "No file is specified" << endl;
}
else {
cout << "(3) Computing the minimal spanning tree using Kruskal's algorithm" << endl;
UpdateDisplay();
MovePen(0,0);
DrawNamedPicture(backgroundFile);
//DrawNodesAndArcs(graph);
minimumSpanningTree(graph, graphMap);
}
break;
case 4:
if (backgroundFile == "") {
cout << "No file is specified" << endl;
}
else {
graphT graph2;
graph2.allNodes = graph;
cout << "(4) Determining if the graph has cycles" << endl;
if (IsCyclicGraph(graph2))
cout << "Yes, it is cyclic" << endl;
else
cout << "No, it is not cyclic" << endl;
}
break;
case 5:
cout << "Thanks for using Pathfinder. Bye!" << endl;
timeToQuit = true;
break;
default:
cout << "Invalid choice" << endl;
break;
}
if (timeToQuit) break;
}
return (0);
}
// Reads the graph file and returns the background file string
string ReadGraph(Set<nodeT *> &graph, Map<nodeT *> &graphMap, string datafile) {
ifstream inner;
inner.open(datafile.c_str());
if (inner.fail()) Error("Can't open file");
string background;
inner >> background;
string name;
inner >> name;
//cout << "Background file: " << background << endl;
//cout << name << endl;
while (true) {
nodeT *n = new nodeT;
if (inner.fail()) break; // no more lines to read
//double x, y;
inner >> n->name;
if (n->name == "ARCS")
break;
else
inner >> n->x >> n->y;
graphMap.add(n->name,n);
graph.add(n);
//cout << n->name << " " << n->x << ":" << n->y << endl;
}
// cout << endl << endl;
// cout << "THE GRAPH MAP" << endl;
// cout << endl;
// PrintGraphMap(graphMap);
while (true) {
if (inner.fail()) break; // no more lines to read
string name1, name2;
double distance;
inner >> name1 >> name2 >> distance;
if (name1 == "") break;
//cout << name1 << " <-> " << name2 << " :" << distance << endl;
arcT *arc1 = new arcT;
arcT *arc2 = new arcT;
arc1->start = graphMap.get(name1);
arc1->end = graphMap.get(name2);
arc1->distance = distance;
arc2->end = graphMap.get(name1);
arc2->start = graphMap.get(name2);
arc2->distance = distance;
nodeT *node1 = graphMap.get(name1);
nodeT *node2 = graphMap.get(name2);
node1->links.add(arc1);
node2->links.add(arc2);
}
inner.close();
// cout << endl << endl;
// cout << "THE GRAPH" << endl;
// PrintGraphMap(graphMap);
// PrintGraph(graph);
MovePen(0,0);
DrawNamedPicture(background);
DrawNodesAndArcs(graph);
return background;
}
void PrintGraphMap(Map<nodeT *> &graphMap) {
Map<nodeT *>::Iterator itr = graphMap.iterator();
while (itr.hasNext()) {
string city = itr.next();
nodeT *n = graphMap.get(city);
cout << n->name << ":" << n->x << ":" << n->y << endl;
}
}
void PrintGraph(Set<nodeT *> &graph) {
cout << endl;
cout << endl;
Set<nodeT *>::Iterator itr = graph.iterator();
while (itr.hasNext()) {
nodeT *n = itr.next();
cout << n->name << ":" << n->x << ":" << n->y << endl;
cout << "ARCS:" << endl;
Vector<arcT *>::Iterator itrArc = n->links.iterator();
while (itrArc.hasNext()) {
arcT *arc = itrArc.next();
cout << arc->distance << ":" << arc->start->name << ":"
<< arc->end->name << endl;
}
cout << endl;
}
}
void DrawNodesAndArcs(Set<nodeT *> graph) {
Set<nodeT *>::Iterator itr = graph.iterator();
while (itr.hasNext()) {
nodeT *n = itr.next();
coordT center = GetCoords(n);
DrawFilledCircleWithLabel(center, "Black", n->name);
Vector<arcT *>::Iterator itrArc = n->links.iterator();
while (itrArc.hasNext()) {
arcT *arc = itrArc.next();
DrawLineBetween(GetCoords(arc->start), GetCoords(arc->end), "Black");
}
}
}
nodeT * GetPoint(Set<nodeT *> graph, string message, string color) {
nodeT *node;
while (true) {
cout << message << endl;
coordT point1 = GetMouseClick();
bool pointFound = false;
Set<nodeT *>::Iterator itr = graph.iterator();
while (itr.hasNext()) { // Test if click was close enough to a known location
node = itr.next();
if (WithinDistance(GetCoords(node),point1)) {
cout << node->name << endl;
DrawFilledCircleWithLabel(GetCoords(node), color);
pointFound = true;
break;
}
}
if (pointFound)
break;
else
cout << "Try Again" << endl;
}
return node;
}
void HandleShortestPath(Set<nodeT *> &graph) {
nodeT *node1 = GetPoint(graph, "Click on the starting point in the Map", "Green");
nodeT *node2 = GetPoint(graph, "Click on the ending point in the Map", "Red");
ShortestPathFirst(node1, node2);
}
bool RecIsCyclic(nodeT * currentNode, nodeT * targetNode, int hops, Set<nodeT *> &visited) {
if (hops > 2 && currentNode == targetNode)
return true;
if (currentNode != targetNode)
visited.add(currentNode);
Vector<arcT *>::Iterator itr = currentNode->links.iterator();
while (itr.hasNext()) {
arcT * link = itr.next();
cout << " Link is (" << hops+1 << ") : " << link->end->name << endl;
if (!visited.contains(link->end) && !(hops+1 < 3 && link->end == targetNode)) {
cout << " Checking: " << link->end->name << endl;
if (RecIsCyclic(link->end, targetNode, hops+1, visited))
return true;
}
}
return false;
}
bool IsCyclicGraph(graphT & graph) {
Set<nodeT *>::Iterator itr = graph.allNodes.iterator();
while (itr.hasNext()) {
nodeT * currentNode = itr.next();
Set<nodeT *> visited;
cout << "Node is: " << currentNode->name << endl;
if (RecIsCyclic(currentNode, currentNode, 0, visited))
return true;
}
return false;
}