From 4ddd36a63abd60579080bd4a224f4d444c10e39a Mon Sep 17 00:00:00 2001 From: Elijah Opoku-Nyarko Date: Thu, 28 Apr 2022 09:11:57 -0400 Subject: [PATCH] Put something togeter for removeBridges function --- .DS_Store | Bin 6148 -> 6148 bytes province.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/.DS_Store b/.DS_Store index 6fc45215b53f821730c8bae577358abe8aa11f88..0f6eac7acd89355b7ea1090f7e074fb351ec77e3 100644 GIT binary patch delta 149 zcmZoMXfc@J&&ahgU^g=(*JK_RV?_>z0)`@ne1E(Vhk1TM301R? Province::bfs(int start) const { 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; + } + } + // If only one town + if (_numberOfTowns == 1) { + output << "There is only one town, so the province " + << "will not be affected by a major storm"; + return; + + // If province has no bridge + } else if (!hasBridge) { + output << "The province has no bridges, so it " + << "will not be affected by a major storm"; + return; + } + + // Mark all towns as unvisited + list toVisit; + for (int i = 0; i < _numberOfTowns; i++) { + toVisit.push_back(i); + } + output << "Connected components in event of a major storm are: "; + output << endl << endl; + + while (!toVisit.empty()) { + // Mark current town as visited + int curr = toVisit.back(); + toVisit.pop_back(); + + // Run BFS from current town + vector 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; + } + } + +} +