-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovince.cc
409 lines (345 loc) · 11.8 KB
/
province.cc
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
/*
* Implentation of Province.h
* Authors: Elijah Opoku-Nyarko and Jake Colbert
*/
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <cfloat>
#include <array>
#include "province.h"
using namespace std;
Province::Province(std::istream & source) {
// Read first line of input
source >> _numberOfTowns >> _numberOfRoads;
_towns = new Town[_numberOfTowns];
std::map<std::string, int> townMap; // maps town names to their indexes in _towns
// Read town names
for (int i = 0; i < _numberOfTowns; i++) {
source >> _towns[i]._name;
townMap[_towns[i]._name] = i;
}
// Read roads
for (int i = 0; i < _numberOfRoads; i++) {
string tail, head;
char bridgeFlag;
double length;
source >> tail >> head >> bridgeFlag >> length;
bool isBridge = (bridgeFlag == 'B');
_roads.push_back(Road(townMap.at(head), townMap.at(tail), isBridge, length));
_towns[townMap.at(tail)]._roads.push_back(
Road(townMap.at(head), townMap.at(tail), isBridge, length));
_towns[townMap.at(head)]._roads.push_back(
Road(townMap.at(tail), townMap.at(head), isBridge, length));
}
}
void Province::printAll(int start, std::ostream & output) {
// keep track of whether a town(vertex) has been scheduled to be visited
bool scheduled[_numberOfTowns];
memset(scheduled, 0, sizeof scheduled); // initialize to 0/false
// Keep track of which towns have been visited
queue <int> toVisit; // use queue to keep track of which town to visit next
toVisit.push(start);
scheduled[start] = true;
output << "The input data is:" << endl << endl;
// Visit every town in the queue
while (!toVisit.empty()) {
int current = toVisit.front();
toVisit.pop();
output << _towns[current]._name << endl;
// Add current town's(vertex's) unscheduled neighbors to the queue
for (Town::RoadList::iterator neighbor = _towns[current]._roads.begin();
neighbor != _towns[current]._roads.end(); neighbor++) {
std::string neighborName = _towns[neighbor->_head]._name;
output << " " << neighborName << " " << neighbor->_length << " mi";
// if the type is bridge, then add to output
if (neighbor ->_isBridge) {
output << " via bridge";
}
output << endl;
int head = neighbor ->_head;
// If neighbour is not scheduled, add neighbor to the queue
if (!scheduled[head]) {
toVisit.push(head);
scheduled[head] = true;
}
}
}
output << endl;
}
int Province::smallest(double dist[], std::list <int> toVisit,
int numTowns) const {
int smallest = toVisit.front();
if (toVisit.size() > 1) {
for (int i = 0; i < numTowns; i++) {
if (dist[i] < dist[smallest]) {
bool found = (std::find(toVisit.begin(), toVisit.end(), i)
!= toVisit.end());
if (found) {
smallest = i;
}
}
}
}
return smallest;
}
void Province::printShortestPath(std::ostream & output) const {
// if there is only one town only one town
if (_numberOfTowns == 1) {
output << "There is only one town, so the provincial "
<< "officials have no need of efficient routes!";
return;
}
output << "The shortest paths from " + _towns[0]._name;
output << " are:" << std::endl << std::endl;
// keeps track of the index of the predecessor to each
// town(vertex) n on the shortest path to n
int prev[_numberOfTowns];
// queue to keep track of which town(vertex) to visit next
list <int> toVisit;
// keeps track of the distance from the capital to each town
// following the shortest path
double dist[_numberOfTowns];
// set defaults for dist, prev, and add all vertices to toVisit
for (int i = 0; i < _numberOfTowns; i++) {
dist[i] = DBL_MAX;
toVisit.push_back(i);
}
// distance from the capital to the capital is zero
dist[0] = 0.0;
while (!toVisit.empty()) {
int smallestIndex = smallest(dist, toVisit, _numberOfTowns);
toVisit.remove(smallestIndex);
// Add current vertex's neighbors to the queue
for (Town::RoadList::iterator neighbor =
_towns[smallestIndex]._roads.begin();
neighbor != _towns[smallestIndex]._roads.end(); neighbor++) {
// new distance needed for testing
double newDist = dist[smallestIndex] + neighbor->_length;
// if new dist is smaller, replace the old one, and
// update the corresponding entry in prev
if (newDist < dist[neighbor->_head]) {
dist[neighbor->_head] = newDist;
prev[neighbor->_head] = smallestIndex;
}
}
}
// print out the data for each non capital town
for (int i = 1; i < _numberOfTowns; i++) {
output << " " << "The shortest path from " + _towns[0]._name;
output << " to " + _towns[i]._name + " is " << dist[i];
output << " mi:" << std::endl;
// stack to hold the path to the town at index i
std::stack <int> predecessors;
// add town at i to stack
int predecessor = i;
predecessors.push(i);
// follow the links in prev until we get to the capital,
// adding each town to the predecessor stack
while (predecessor != 0) {
predecessor = prev[predecessor];
predecessors.push(predecessor);
}
// print out the names for each entry in the stack
while (!predecessors.empty()) {
output << " " << _towns[predecessors.top()]._name;
output << std::endl;
predecessors.pop();
}
}
cout << endl;
}
bool Province::Road::operator < (Road road2) const {
return this->_length < road2._length;
}
void Province::minSpan(std::ostream & output) const {
// if only one town
if (_numberOfTowns == 1) {
output << "There is only one town, so the province "
<< "does not need to upgrade any roads!";
return;
}
list<Road> roads;
vector<Road> minSpanTree;
vector<int> higher;
// Initialize a numComponent value for each town to 0
int numComponent[_numberOfTowns];
for (int i = 0; i < _numberOfTowns; i++) {
numComponent[i] = 0;
}
// Add all roads to list of roads
for (int i = 0; i < _numberOfRoads; i++) {
roads.push_back(_roads[i]);
}
// Sort list of roads by length
roads.sort();
int compNum = 0; // Used to determine if edge forms a cycle
while (minSpanTree.size() < _numberOfTowns - 1) {
Road minRoad = roads.front();
roads.pop_front();
// Both towns have component number 0
if (numComponent[minRoad._head] == 0 &&
numComponent[minRoad._tail] == 0) {
minSpanTree.push_back(minRoad);
compNum++;
numComponent[minRoad._head] = compNum;
numComponent[minRoad._tail] = compNum;
// Only one town has component number 0
} else if (numComponent[minRoad._head] == 0) {
minSpanTree.push_back(minRoad);
numComponent[minRoad._head] = numComponent[minRoad._tail];
// Other town has component number 0
} else if (numComponent[minRoad._tail] == 0) {
minSpanTree.push_back(minRoad);
numComponent[minRoad._tail] = numComponent[minRoad._head];
// If component number of one town is less than other town
} else if (numComponent[minRoad._head] <
numComponent[minRoad._tail]) {
minSpanTree.push_back(minRoad);
higher.push_back(minRoad._tail);
// Set all higher road components to value of lower
for (int i = 0; i < higher.size(); i++) {
higher[i] = numComponent[minRoad._head];
}
// If component number of other town is less than other town
} else if (numComponent[minRoad._head] >
numComponent[minRoad._tail]) {
minSpanTree.push_back(minRoad);
higher.push_back(minRoad._head);
// Set all higher road components to value of lower
for (int i = 0; i < higher.size(); i++) {
higher[i] = numComponent[minRoad._tail];
}
}
}
output << "The road upgrading goal can be achieved at minimal cost by upgrading:";
output << std::endl;
// Print names of towns in minimum spanning tree of province
for (int i = 0; i < minSpanTree.size(); i++) {
output << " ";
output << _towns[minSpanTree[i]._head]._name;
output << " to ";
output << _towns[minSpanTree[i]._tail]._name << std::endl;
}
output << endl;
}
std::vector<int> Province::bfs(int start) const {
// Initialize list of towns scheduled to visit
bool scheduled[_numberOfTowns];
memset(scheduled, 0, sizeof scheduled); // initialize to 0/false
// Initialize list of towns to visit with starting town
std::queue<int> toVisit;
toVisit.push(start);
scheduled[start] = true;
std::vector<int> results;
// While all towns have not been visited
while (!toVisit.empty()) {
// Remove current town from queue and add to results
int current = toVisit.front();
toVisit.pop();
results.push_back(current);
// Iterate over neighbors to current town
for (Town::RoadList::iterator neighbor =
_towns[current]._roads.begin();
neighbor != _towns[current]._roads.end();
neighbor ++) {
// If neighbor is not bridge and is not scheduled,
// add to results and schedule
if (!neighbor->_isBridge && !scheduled[neighbor->_head]) {
toVisit.push(neighbor->_head);
scheduled[neighbor->_head] = true;
}
}
}
return results;
}
/**
* Remove bridges and print the list of towns that remain connected
*/
void Province::removeBridges(ostream &output) const {
// Look for a bridge
bool hasBridge = false;
for ( int roadNum = 0; roadNum < _roads.size(); roadNum++) {
if (_roads[roadNum]._isBridge) {
hasBridge = true;
break;
}
}
// Mark all towns as unvisited
list<int> toVisit;
for (int i = 0; i < _numberOfTowns; i++) {
toVisit.push_back(i);
}
output << "Connected components in event of a major storm are:";
output << endl;
while (!toVisit.empty()) {
// Mark current town as visited
int curr = toVisit.back();
toVisit.pop_back();
// Run BFS from current town
vector<int> bfsResult = bfs(curr);
// Mark all town in BFS result as visited
for (int i = 0; i < bfsResult.size(); i++) {
toVisit.remove(bfsResult[i]);
}
output << " ";
output << "If all bridges fail, the following towns would form ";
output << "an isolated group:" << endl;
// Print names of all towns in connected component
for (int i = 0; i < bfsResult.size(); i++) {
output << " ";
output << _towns[bfsResult[i]]._name << endl;
}
output << endl;
}
}
void Province::dfs(int v,
bool visited[],
int low[],
int tin[],
int & timer,
std::vector<int> & ap,
int p = -1)
{
visited[v] = true;
tin[v] = low[v] = timer++;
int children=0;
for (Road to : _towns[v]._roads) {
if (to._tail == p) continue;
if (visited[to._head]) {
low[v] = min(low[v], tin[to._head]);
} else {
dfs(to._head, visited, low, tin, timer, ap, v);
low[v] = min(low[v], low[to._head]);
if (low[to._head] >= tin[v] && p!=-1) {
ap.push_back(v);
}
++children;
}
}
if (p == -1 && children > 1) {
ap.push_back(v);
}
}
void Province::articulationPoints(std::ostream & output) {
std::vector<int> ap;
bool visited[_numberOfTowns]; memset(visited, 0, sizeof visited);
int tin[_numberOfTowns]; memset(tin, 0, sizeof tin);
int low[_numberOfTowns]; memset(low, 0, sizeof low);
int timer = 0;
for (int i = 0; i < _numberOfTowns; ++i) {
if (!visited[i]) {
dfs(i, visited, low, tin, timer, ap);
}
}
output << "Destruction of any of the following would result in the province becoming";
output << "\ndisconnected:" << endl;
int apCount = 0;
for (int i : ap) {
output << " " << _towns[i]._name << endl;
apCount++;
}
if (apCount == 0) { output << " (None)" << endl; }
output << endl;
}