-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dijkstra.cpp
210 lines (168 loc) · 6.78 KB
/
test_dijkstra.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
/*
@file test_dijkstra.cpp
Test file for directed graph class
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <limits>
#include <vector>
#include "Digraph.hpp"
#include "test_suite.hpp"
// Read data from either `nqmq.dat` or `nqmqBig.dat` into `directedGraph`
// @param directedGraph [out] The adjacency matrix storing the nodes
// @param bigData [in] Flag to determine wether to use `nqmq.dat` or `nqmqBig.dat`
void readData(Digraph& directedGraph, bool bigData);
// Runs a test suite on dijkstra's algorithm by trying all combinations of nodes in `directedGraph`
// @parm directedGraph [in] The adjacency matrix storing the nodes
// @param bigData [in] Flag to determine wether to use `nqmq.dat` or `nqmqBig.dat`
// @param fullReport [in] Flag to control output
void runTest(const Digraph& directedGraph, bool bigData, bool fullReport);
// Runs a menu-based UI for testing dijkstra's algorithm
// @parm directedGraph [in] The adjacency matrix storing the nodes
void runMenu(const Digraph& directedGraph);
int main(int argc, char* argv[])
{
// flag for nqmqBig.dat
bool bigData = false;
// flags for testing
bool testSuite = false;
bool fullReport = true;
// get command line arguments
for (int i = 0; i < argc; ++i) {
if (strcmp(argv[i], "big") == 0)
bigData = true;
if (strcmp(argv[i], "test") == 0)
testSuite = true;
if (strcmp(argv[i], "summary") == 0)
fullReport = false;
}
// the adjacency matrix
Digraph directedGraph;
// read data into graph
readData(directedGraph, bigData);
// testing
if (testSuite) {
runTest(directedGraph, bigData, fullReport);
}
// menu gui
else {
runMenu(directedGraph);
}
return 0;
}
// Read data from either `nqmq.dat` or `nqmqBig.dat` into `directedGraph`
// @param directedGraph [out] The adjacency matrix storing the nodes
// @param bigData [in] Flag to determine wether to use `nqmq.dat` or `nqmqBig.dat`
void readData(Digraph& directedGraph, bool bigData) {
// open the appropriate input file
std::ifstream dataFile;
if (bigData)
dataFile.open("nqmqBig.dat");
else
dataFile.open("nqmq.dat");
// the number of cities in the graph, each city is a node
int numberOfNodes;
dataFile >> numberOfNodes;
// the name of each city
std::string city;
// directed graph to store the cities in
for (int i = 0; i < numberOfNodes; i++) {
dataFile >> city;
directedGraph.addVertex(city);
}
// read in the first route
// read in the source
int sourceIndex;
dataFile >> sourceIndex;
// read in the destination
int destinationIndex;
dataFile >> destinationIndex;
// read in the distance between the source and the destination
int distance;
dataFile >> distance;
// -1 delimits the list
while (sourceIndex > -1) {
// from source to destination
directedGraph.addEdge(sourceIndex, destinationIndex, distance);
// from destination back to source
directedGraph.addEdge(destinationIndex, sourceIndex, distance);
// read in the next route
dataFile >> sourceIndex;
dataFile >> destinationIndex;
dataFile >> distance;
}
// close the input file once done reading
dataFile.close();
}
// Runs a test suite on dijkstra's algorithm by trying all combinations of nodes in `directedGraph`
// @parm directedGraph [in] The adjacency matrix storing the nodes
// @param bigData [in] Flag to determine wether to use `nqmq.dat` or `nqmqBig.dat`
// @param fullReport [in] Flag to control output
void runTest(const Digraph& directedGraph, bool bigData, bool fullReport) {
// run the appropriate test suite
if(bigData)
testSuite(directedGraph, fullReport, bigData);
else
testSuite(directedGraph, fullReport);
}
// Runs a menu-based UI for testing dijkstra's algorithm
// @parm directedGraph [in] The adjacency matrix storing the nodes
void runMenu(const Digraph& directedGraph) {
// allows for repeated use of the menu system
char again = 'Y';
// main loop
while (again == 'Y' || again == 'y') {
// values from the displayed menu
std::vector<int> cities = {-1, -1};
// display available cities
for (int i = 0; i < directedGraph.getNumberOfVertices(); ++i)
std::cout << i + 1 << ( i + 1 < 10 ? ") ":") ") << directedGraph.getVertex(i)->getName() << '\n';
// pick starting and ending cities
for (int i = 0; i < cities.size(); ++i) {
// select city
std::cout << "Select " << (!i ? "Starting":"Ending") << " City: ";
do {
// input city index
std::cin >> cities[i];
// validate input
if(std::cin.fail() || (cities[i] < 1 || cities[i] > directedGraph.getNumberOfVertices())) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Invalid Input, enter a number from the table above: ";
cities[i] = -1;
}
}
while(cities[i] < 1 || cities[i] > directedGraph.getNumberOfVertices()); // range: [1, numberOfVertices]
}
// output distance and path between the two cities
int sourceIndex = cities[0] - 1;
int destinationIndex = cities[1] - 1;
std::cout << "\nThe distance between " << directedGraph.getVertex(sourceIndex)->getName();
std::cout << " and " << directedGraph.getVertex(destinationIndex)->getName();
std::string path;
std::cout << " is: " << directedGraph.dijkstra(sourceIndex, destinationIndex, path) << '\n';
// output path
if (path.find("->") == std::string::npos) {
std::cout << "PATH: You didn't go anywhere.";
}
else {
std::cout << path;
}
std::cout << "\n\n";
// prompt user if they want to go again
std::cout << "Again?[Y/N]:";
do {
// input choice
std::cin >> again;
// validate choice
if(std::cin.fail() || again != 'N' && again != 'Y' && again != 'n' && again != 'y') {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout<<"Invalid Input, enter 'Y' or 'N':";
}
}
while(again != 'N' && again != 'Y' && again != 'n' && again != 'y'); // only accept 'y', 'n', 'Y', and 'N'
}
}