From dbe3b0d89a6d8ea675779bfaa8cb49ce2683795d Mon Sep 17 00:00:00 2001 From: Elijah Opoku-Nyarko Date: Tue, 26 Apr 2022 12:37:55 -0400 Subject: [PATCH] BFS traversal on the province - Ignoring bridges --- province.cc | 41 +++++++++++++++++++++++++++++++++++++++++ province.h | 7 +++++++ 2 files changed, 48 insertions(+) diff --git a/province.cc b/province.cc index eb09948..793dcab 100644 --- a/province.cc +++ b/province.cc @@ -318,3 +318,44 @@ void Province::minSpan(std::ostream & output) const { output << _towns[minSpanTree[i]._tail]._name << std::endl; } } + +std::vector Province::bfs(int start) const { + // Initialize list of towns scheduled to visit + bool scheduled[_numberOfTowns]; + for (int i = 0; i < _numberOfTowns; i ++) { + scheduled[i] = false; + } + + // Initialize list of towns to visit with starting town + std::queue toVisit; + toVisit.push(start); + + scheduled[start] = true; + std::vector results; + + // While all towns have not been visited + while (!toVisit.empty()) { + + // Remove current town from queue, 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; +} + diff --git a/province.h b/province.h index a5ce1c7..fcd773f 100644 --- a/province.h +++ b/province.h @@ -54,7 +54,14 @@ class Province private: int smallest(double dist [], std::list toVisit, int numTowns) const; + + /** + * Conduct a breadth-first traversal on the province, ignoring bridges + * @param start Index of town to start traversal at + * @return - List of indices of towns in order of traversal + */ std::vector bfs(int start) const; + void dfsAux(int current, std::vector & dfsTowns, bool visited []) const; /**