Skip to content

Commit

Permalink
Merge pull request #4 from gordon-cs/worksession
Browse files Browse the repository at this point in the history
Worksession progress
  • Loading branch information
jakedcolbert authored May 3, 2022
2 parents a0fa16a + d82a12d commit dbbc889
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 177 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ project5: project5.cc province.cc province.h
# test the code against an expected output file
# this test should grow into a spaceship and end with blinkers
test-requirement1: project5
./project5 < test-data/requirement1.in > test-data/project5.out
diff test-data/project5.out test-data/requirement1.out > test.diff
wc -l test.diff
./project5 < test-data/t0502-combo.in > test-data/project5.out
diff test-data/project5.out test-data/shortest.out > test.diff
rm test-data/project5.out
Binary file modified project5
Binary file not shown.
42 changes: 12 additions & 30 deletions project5.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,16 @@ bool eof() {
}

int main(int argc, char *argv[]) {
// Repeatedly read input from standard input
while (!eof()) {
// create a new graph for each loop, which will read all of the
// corresponding data per graph
Province theProvince(cin);

std::cout << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << "---------------- New DataSet: ------------------" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << std::endl;

theProvince.printAll(0, cout);


theProvince.printShortestPath(std::cout);

std::cout << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << std::endl;

theProvince.minSpan(std::cout);

std::cout << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << std::endl;

}
// Repeatedly read input from standard input
while (!eof()) {
// create a new graph for each loop, which will read all of the
// corresponding data per graph
Province theProvince(cin);
theProvince.printAll(0, cout);
theProvince.printShortestPath(std::cout);
theProvince.minSpan(std::cout);
std::cout << std::endl;
std::cout << "------------------------------------------------------------------";
std::cout << endl;
}
}
68 changes: 35 additions & 33 deletions province.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,20 @@ void Province::printAll(int start, std::ostream & output) {
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;
output << "The input data is:" << endl << endl;

// Visit every town in the queue
while (!toVisit.empty()) {
int current = toVisit.front();
toVisit.pop();

output << " ";
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 << " ";
output << neighborName << " " << neighbor->_length << " mi";
std::string neighborName = _towns[neighbor->_head]._name;
output << " " << neighborName << " " << neighbor->_length << " mi";

// if the type is bridge, then add to output
if (neighbor ->_isBridge) {
Expand All @@ -113,7 +110,7 @@ void Province::printAll(int start, std::ostream & output) {
}
}
}
output << endl << endl;
output << endl;
}

int Province::smallest(double dist[], std::list <int> toVisit,
Expand Down Expand Up @@ -142,11 +139,18 @@ 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!";
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];

output << "The shortest routes from " + _towns[0]._name;
output << " are:" << std::endl << std::endl;

Expand Down Expand Up @@ -192,11 +196,11 @@ void Province::printShortestPath(std::ostream & output) const {
}
}

// print out the data for each non capital town
for (int i = 1; i < _numberOfTowns; i++) {
output << " " << "The shortest route from " + _towns[0]._name;
output << " to " + _towns[i]._name + " is " << dist[i];
output << " mi:" << std::endl;
// 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;
Expand All @@ -211,14 +215,14 @@ void Province::printShortestPath(std::ostream & output) const {
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();
// 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;
}

/**
Expand Down Expand Up @@ -307,18 +311,16 @@ void Province::minSpan(std::ostream & output) const {
}
}

output << "The road upgrading goal can be achieved at minimal cost by upgrading:";
output << std::endl << 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 << "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;

std::vector<int> Province::bfs(int start) const {
// Initialize list of towns scheduled to visit
bool scheduled[_numberOfTowns];
Expand Down
102 changes: 30 additions & 72 deletions test-data/project5.out
Original file line number Diff line number Diff line change
@@ -1,84 +1,42 @@
The input data is:

------------------------------------------------
---------------- New DataSet: ------------------
------------------------------------------------
Wenham
Hamilton 1 mi
Hamilton
Wenham 1 mi

The input data is :
The shortest paths from Wenham are:

Salem
Beverly 2.4 mi via bridge
Danvers 3.7 mi via bridge
Lynn 4.9 mi
Beverly
Danvers 2.9 mi
Salem 2.4 mi via bridge
Wenham 5.2 mi
Danvers
Beverly 2.9 mi
Wenham 4.2 mi
Salem 3.7 mi via bridge
Lynn
Salem 4.9 mi
Wenham
Beverly 5.2 mi
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

------------------------------------------------
------------------------------------------------
The shortest path from Wenham to Hamilton is 1 mi:
Wenham
Hamilton

The road upgrading goal can be achieved at minimal cost by upgrading:
Hamilton to Wenham

Salem to Beverly
Danvers to Beverly
Wenham to Danvers
Salem to Lynn

------------------------------------------------
------------------------------------------------

------------------------------------------------------------------
The input data is:

------------------------------------------------
---------------- New DataSet: ------------------
------------------------------------------------
A
B 1 mi via bridge
B
A 1 mi via bridge
C 1 mi via bridge
C
B 1 mi via bridge

The input data is :
The shortest paths from A are:

Wenham
Hamilton 1 mi
Hamilton
Wenham 1 mi


The shortest routes from Wenham are:

The shortest route from Wenham to Hamilton is 1 mi:
Wenham
Hamilton

------------------------------------------------
------------------------------------------------
The shortest path from A to B is 1 mi:
A
B
The shortest path from A to C is 2 mi:
A
B
C

The road upgrading goal can be achieved at minimal cost by upgrading:
B to A
C to B

Hamilton to Wenham

------------------------------------------------
------------------------------------------------

------------------------------------------------------------------
39 changes: 0 additions & 39 deletions test-data/requirement1.out

This file was deleted.

45 changes: 45 additions & 0 deletions test-data/shortest.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
The input data is:

Wenham
Hamilton 1 mi
Hamilton
Wenham 1 mi

The shortest paths from Wenham are:

The shortest path from Wenham to Hamilton is 1 mi:
Wenham
Hamilton

The road upgrading goal can be achieved at minimal cost by upgrading:
Hamilton to Wenham


------------------------------------------------------------------

The input data is:

A
B 1 mi via bridge
B
A 1 mi via bridge
C 1 mi via bridge
C
B 1 mi via bridge

The shortest paths from A are:

The shortest path from A to B is 1 mi:
A
B
The shortest path from A to C is 2 mi:
A
B
C

The road upgrading goal can be achieved at minimal cost by upgrading:
B to A
C to B


------------------------------------------------------------------

0 comments on commit dbbc889

Please sign in to comment.