diff --git a/project5 b/project5 index bcd3caf..e061e86 100755 Binary files a/project5 and b/project5 differ diff --git a/project5.cc b/project5.cc index b29a904..c19a073 100644 --- a/project5.cc +++ b/project5.cc @@ -46,6 +46,6 @@ int main(int argc, char *argv[]) { theProvince.printAll(0, cout); - + } } diff --git a/province.cc b/province.cc index 7b1025b..95a96bd 100644 --- a/province.cc +++ b/province.cc @@ -117,6 +117,82 @@ void Province::printAll(int start, std::ostream & output) { output << endl << endl; } - + +int Province::smallest(double dist[], std::list 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; +} + +/** +* Print the shortest route from the capital of the +* province to each of the other towns +*/ +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 routes 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 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; + } + } + } +} diff --git a/test-data/project5.out b/test-data/project5.out index d5d431a..8caeb26 100644 --- a/test-data/project5.out +++ b/test-data/project5.out @@ -24,6 +24,25 @@ The input data is : Danvers 4.2 mi +The shortest routes from Salem are: + + The shortest route from Salem to Wenham is 7.6 mi: + Salem + Beverly + Wenham + The shortest route from Salem to Beverly is 2.4 mi: + Salem + Beverly + The shortest route from Salem to Danvers is 3.7 mi: + Salem + Danvers + The shortest route from Salem to Lynn is 4.9 mi: + Salem + Lynn + +------------------------------------------------ +------------------------------------------------ + ------------------------------------------------ ---------------- New DataSet: ------------------ @@ -37,3 +56,12 @@ The input data is : Wenham 1 mi +The shortest routes from Wenham are: + + The shortest route from Wenham to Hamilton is 1 mi: + Wenham + Hamilton + +------------------------------------------------ +------------------------------------------------ +