Repository navigation
Expand file tree
/
Copy pathflightmap.cpp
More file actions
180 lines (128 loc) · 4.99 KB
/
Copy pathflightmap.cpp
File metadata and controls
180 lines (128 loc) · 4.99 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
#include <fstream>
#include <sstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <utility>
#include <exception>
#include "flightmap.h"
using namespace std;
FlightMap::FlightMap(string file_path)
{
ifstream myfile (file_path.c_str());
string line;
//check to see if the file is opened:
if (myfile.is_open()) {
//while there are still lines in the
//file, keep reading:
for(auto index = 0; ! myfile.eof(); index++) {
//place the line from myfile into the
//line string variable:
getline (myfile,line);
if (line == "") { // last line of text file is empty.
break;
}
// read in the integers from the string
istringstream istr(line);
// the first city is the destination city
int originCity;
istr >> originCity;
// add the destination cities, which follow the origin
// city, to a temporary vector
vector<int> destinations;
copy(istream_iterator<int>(istr), istream_iterator<int>(),
back_inserter(destinations));
// insert the origin city into the map.
auto pr = adjacencyList.insert(pair<int, adjacencyStruct>(originCity, adjacencyStruct() ));
// swap it's value, an empty vector<adjacencyStruct>, with the vector we created.
(pr.first)->second.destinationCities.swap(destinations);
}
//close the stream:
myfile.close();
}
for(auto iter = adjacencyList.begin(); iter != adjacencyList.end(); iter++) {
cout << "Origin is: " << iter->first << iter->second << endl;
}
}
// Set the visited flag to false in each adjacencyStruct.
void FlightMap::unvisitAll()
{
for (map<int, adjacencyStruct>::iterator iter = adjacencyList.begin(); iter != adjacencyList.end(); iter++) {
iter->second.setVisited(false);
//TODO: reset all iterators for the destination cities.
vector<int>& vec = iter->second.destinationCities;
vector<int>::const_iterator iter_begin = vec.begin();
iter->second.iter = iter_begin;
}
}
// This method is called only once, each time a city is visited, immediately
// after it has been pushed onto the stack (of visited cities).
inline void FlightMap::markVisited(int city)
{
adjacencyStruct& s = adjacencyList[city];
// Set the initial value for the vector's iterator.
s.setVisited(true);
}
// Get the next yet-unvisited destination city.
bool FlightMap::getNextCity(int TopCity, int& nextCity)
{
// If an destination city has not yet been visited, return it.
adjacencyStruct& s = adjacencyList[TopCity];
while (s.iter != s.destinationCities.end()) {
int index = *(s.iter);
// advance iterator, so next time this method is called we
// already point to the next destination city.
++s.iter;
if (!adjacencyList[index].visited) {
nextCity = index;
return true;
}
}
return false;
}
bool FlightMap::isPath(int originCity, int destinationCity)
// -----------------------------------------------------
// Determines whether a sequence of flights between two
// cities exists. Nonrecursive stack version.
// Precondition: originCity and destinationCity are the city
// numbers of the origin and destination cities,
// respectively.
// Postcondition: Returns true if a sequence of flights
// exists from originCity to destinationCity; otherwise
// returns false. Cities visited during the search are
// marked as visited in the flight map.
// Implementation notes: Uses a stack for the city
// numbers of a potential path. Calls unvisitAll,
// markVisited, and getNextCity.
// -----------------------------------------------------
{
stack<int> aStack;
int topCity, nextCity;
bool success;
unvisitAll();
// push origin city onto aStack, mark it visited
aStack.push(originCity);
markVisited(originCity);
topCity = aStack.top();
while (!aStack.empty() && (topCity != destinationCity)) {
// Loop invariant: The stack contains a directed path
// from the origin city at the bottom of the stack to
// the city at the top of the stack
// find an unvisited city adjacent to the city on the
// top of the stack
success = getNextCity(topCity, nextCity);
if (!success) {
aStack.pop(); // no city found; backtrack
}
else { // visit city
aStack.push(nextCity);
markVisited(nextCity);
}
if (!aStack.empty()) {
topCity = aStack.top();
}
} // end while
// If stack empty, no path exists.
return !aStack.empty();
}