diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index b9744af..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "configurations": [ - { - "name": "macos-gcc-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "macos-gcc-x64", - "compilerArgs": [ - "-Wall", - "-Wextra", - "-Wpedantic" - ] - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 8e496e8..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "cppdbg", - "request": "launch", - "args": [], - "stopAtEntry": false, - "cwd": "/Users/elijahopoku-nyarko/Dropbox/My Mac (Elijah Macbook Air)/Desktop/cps222-2022-p5-opokunyarko-colbert", - "environment": [], - "program": "/Users/elijahopoku-nyarko/Dropbox/My Mac (Elijah Macbook Air)/Desktop/cps222-2022-p5-opokunyarko-colbert/build/Debug/outDebug", - "internalConsoleOptions": "openOnSessionStart", - "MIMode": "gdb", - "miDebuggerPath": "/usr/bin/lldb", - "externalConsole": false - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 99f2e92..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,109 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "/usr/bin/clang", - "C_Cpp_Runner.cppCompilerPath": "/usr/bin/clang++", - "C_Cpp_Runner.debuggerPath": "/usr/bin/lldb", - "C_Cpp_Runner.makePath": "/usr/bin/make", - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic" - ], - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.excludeSearch": [], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "files.associations": { - "iostream": "cpp", - "map": "cpp", - "__string": "cpp", - "ios": "cpp", - "__bit_reference": "cpp", - "__bits": "cpp", - "__config": "cpp", - "__debug": "cpp", - "__errc": "cpp", - "__hash_table": "cpp", - "__locale": "cpp", - "__mutex_base": "cpp", - "__node_handle": "cpp", - "__nullptr": "cpp", - "__split_buffer": "cpp", - "__threading_support": "cpp", - "__tree": "cpp", - "__tuple": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "cinttypes": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "csignal": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "exception": "cpp", - "forward_list": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "istream": "cpp", - "limits": "cpp", - "list": "cpp", - "locale": "cpp", - "memory": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "numeric": "cpp", - "optional": "cpp", - "ostream": "cpp", - "queue": "cpp", - "random": "cpp", - "ratio": "cpp", - "semaphore": "cpp", - "sstream": "cpp", - "stack": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "thread": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "variant": "cpp", - "vector": "cpp", - "__functional_base": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "utility": "cpp", - "*.tcc": "cpp", - "memory_resource": "cpp", - "stop_token": "cpp" - } -} \ No newline at end of file diff --git a/Makefile b/Makefile index e05576d..d3e4ddd 100644 --- a/Makefile +++ b/Makefile @@ -19,15 +19,12 @@ project5: project5.cc province.cc province.h g++ -std=c++11 -o project5 project5.cc province.cc # test all the code -test-all: project5 test-nowhere test-one-road test-simple test-local test-combo - @echo "" - @echo "$(GREEN)Passed all tests!$(RESET)" - @echo "" +test-all: project5 test-requirement1 test-nowhere test-one-road test-simple test-local test-combo + @rm test.diff && rm test-data/project5.out -test: project5 - ./project5 < test-data/t0502-combo.in > test-data/project5.out - diff test-data/project5.out test-data/shortest.out > test.diff - rm test.diff && rm project5.out +test-requirement1: project5 + ./project5 < test-data/requirement1.in > test-data/project5.out + -@diff test-data/project5.out test-data/requirement1.out > test.diff && echo "$(GREEN)Passed!$(RESET)" # test the code against an expected output file test-nowhere: project5 @@ -35,9 +32,7 @@ test-nowhere: project5 @echo "$(RED)--- Nowhere Test ---$(RESET)" @echo "" ./project5 < test-data/t01-nowhere.in > test-data/project5.out - diff test-data/project5.out test-data/t01-nowhere.out > test.diff - rm test.diff && rm project5.out - @echo "$(GREEN)Passed!$(RESET)" + -@diff test-data/project5.out test-data/t01-nowhere.out > test.diff && echo "$(GREEN)Passed!$(RESET)" # test the code against an expected output file test-one-road: project5 @@ -45,9 +40,7 @@ test-one-road: project5 @echo "$(RED)--- One Road Test ---$(RESET)" @echo "" ./project5 < test-data/t02-one-road.in > test-data/project5.out - diff test-data/project5.out test-data/t02-one-road.out > test.diff - rm test.diff && rm project5.out - @echo "$(GREEN)Passed!$(RESET)" + -@diff test-data/project5.out test-data/t02-one-road.out > test.diff && echo "$(GREEN)Passed!$(RESET)" # test the code against an expected output file test-simple: project5 @@ -55,9 +48,7 @@ test-simple: project5 @echo "$(RED)--- Simple Test ---$(RESET)" @echo "" ./project5 < test-data/t03-simple.in > test-data/project5.out - diff test-data/project5.out test-data/t03-simple.out > test.diff - rm test.diff && rm project5.out - @echo "$(GREEN)Passed!$(RESET)" + -@diff test-data/project5.out test-data/t03-simple.out > test.diff && echo "$(GREEN)Passed!$(RESET)" # test the code against an expected output file test-local: project5 @@ -65,9 +56,7 @@ test-local: project5 @echo "$(RED)--- Local Test ---$(RESET)" @echo "" ./project5 < test-data/t08-local.in > test-data/project5.out - diff test-data/project5.out test-data/t08-local.out > test.diff - rm test.diff && rm project5.out - @echo "$(GREEN)Passed!$(RESET)" + -@diff test-data/project5.out test-data/t08-local.out > test.diff && echo "$(GREEN)Passed!$(RESET)" # test the code against an expected output file test-combo: project5 @@ -75,6 +64,4 @@ test-combo: project5 @echo "$(RED)--- Combo Test ---$(RESET)" @echo "" ./project5 < test-data/t0502-combo.in > test-data/project5.out - diff test-data/project5.out test-data/t0502-combo.out > test.diff - rm test.diff && rm project5.out - @echo "$(GREEN)Passed!$(RESET)" \ No newline at end of file + -@diff test-data/project5.out test-data/t0502-combo.out > test.diff && echo "$(GREEN)Passed!$(RESET)" \ No newline at end of file diff --git a/province.cc b/province.cc index d0f3f5f..d17e273 100644 --- a/province.cc +++ b/province.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include "province.h" @@ -179,18 +180,10 @@ void Province::printShortestPath(std::ostream & output) const { cout << endl; } -/** - * < operator used to compare two roads - * @param road2 A road - * @return True if road1 is shorter in length than road 2 - */ bool Province::Road::operator < (Road road2) const { return this->_length < road2._length; } -/** - * Find minimum cost spanning tree of the province - */ void Province::minSpan(std::ostream & output) const { // if only one town if (_numberOfTowns == 1) { @@ -313,6 +306,10 @@ std::vector Province::bfs(int start) const { return results; } +void ap(int dfsTree[]) { + +} + /** * Remove bridges and print the list of towns that remain connected */ @@ -360,6 +357,40 @@ void Province::removeBridges(ostream &output) const { } } +void Province::dfs(vector & dfsTowns) const { + stack toExplore; + int town = 0; + bool visited[_numberOfTowns]; + memset(visited, false, sizeof visited); + while(true) { + cerr << "while" << endl; + visited[town] = true; + cerr << "town " << _towns[town]._name << " is " << visited[town] << endl; + // find an unvisited road and continue while loop + for (std::list::const_iterator it = _towns[town]._roads.begin(); it != _towns[town]._roads.end(); it++) { + cerr << _towns[town]._name << "'s " << _towns[it->_head]._name << " " << _towns[it->_tail]._name << " " << _towns[town]._roads.size() << endl; + if (!visited[it->_head]) { + cerr << "New visit" << endl; + toExplore.push(town); + dfsTowns.push_back(town); + town = it->_head; + break; + } + } + if (toExplore.empty()) { + cerr << "empty" << endl; + break; + } else { + cerr << "leaf" << endl; + town = toExplore.top(); + toExplore.pop(); + } + } + for(std::vector::iterator it = std::begin(dfsTowns); it != std::end(dfsTowns); ++it) { + std::cout << *it << "\n"; + } +} + void Province::APUtil(int u, bool visited[], int disc[], int low[], int& time, int parent, bool isAP[]) const { @@ -402,6 +433,7 @@ void Province::APUtil(int u, bool visited[], void Province::articulationPoints(std::ostream & output) const { + int disc[_numberOfTowns]; memset(disc, 0, sizeof disc); int low[_numberOfTowns]; @@ -410,6 +442,8 @@ void Province::articulationPoints(std::ostream & output) const bool isAP[_numberOfTowns]; memset(isAP, false, sizeof isAP); int time = 0, par = -1; + vector towns; + dfs(towns); // Adding this loop so that the // code works even if we are given @@ -433,8 +467,4 @@ void Province::articulationPoints(std::ostream & output) const output << " (None)" << endl; } output << endl; - - for (bool i : isAP) { - cerr << i << endl; - } } \ No newline at end of file diff --git a/province.h b/province.h index 9724b94..bb40cc7 100644 --- a/province.h +++ b/province.h @@ -69,6 +69,7 @@ class Province { int smallest(double dist [], std::list toVisit, int numTowns) const; + void dfs(std::vector & dfsTowns) const; /** * Conduct a breadth-first traversal on the province, ignoring bridges * @param start Index of town to start traversal at diff --git a/tes.out b/tes.out new file mode 100644 index 0000000..de2041a --- /dev/null +++ b/tes.out @@ -0,0 +1,5 @@ +g++ -std=c++11 -o project5 project5.cc province.cc + +--- Local Test ---(B + +./project5 < test-data/t08-local.in > test-data/project5.out diff --git a/test-data/project5.out b/test-data/project5.out deleted file mode 100644 index a0c981d..0000000 --- a/test-data/project5.out +++ /dev/null @@ -1,69 +0,0 @@ -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 - -Connected components in event of a major storm are: - If all bridges fail, the following towns would form an isolated group: - Hamilton - Wenham - -Destruction of any of the following would result in the province becoming -disconnected: - (None) - - ------------------------------------------------------------------- - -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 - -Connected components in event of a major storm are: - If all bridges fail, the following towns would form an isolated group: - C - - If all bridges fail, the following towns would form an isolated group: - B - - If all bridges fail, the following towns would form an isolated group: - A - -Destruction of any of the following would result in the province becoming -disconnected: - (None) - - ------------------------------------------------------------------- - diff --git a/test-data/requirement1.out b/test-data/requirement1.out index d5d431a..3452ab7 100644 --- a/test-data/requirement1.out +++ b/test-data/requirement1.out @@ -1,39 +1,87 @@ +The input data is: ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- +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 input data is : +The shortest paths from Salem 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 path from Salem to Wenham is 7.6 mi: + Salem + Beverly + Wenham + The shortest path from Salem to Beverly is 2.4 mi: + Salem + Beverly + The shortest path from Salem to Danvers is 3.7 mi: + Salem + Danvers + The shortest path from Salem to Lynn is 4.9 mi: + Salem + Lynn +The road upgrading goal can be achieved at minimal cost by upgrading: + Salem to Beverly + Danvers to Beverly + Wenham to Danvers + Salem to Lynn +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + Lynn + Salem ------------------------------------------------- ----------------- New DataSet: ------------------ ------------------------------------------------- + If all bridges fail, the following towns would form an isolated group: + Danvers + Beverly + Wenham -The input data is : +Destruction of any of the following would result in the province becoming +disconnected: + (None) - Wenham - Hamilton 1 mi - Hamilton - Wenham 1 mi +------------------------------------------------------------------ + +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 + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + Hamilton + Wenham + +Destruction of any of the following would result in the province becoming +disconnected: + (None) + + +------------------------------------------------------------------