diff --git a/src/algorithm.cpp b/src/algorithm.cpp index 4e48fcf..caeb3e0 100644 --- a/src/algorithm.cpp +++ b/src/algorithm.cpp @@ -46,26 +46,40 @@ void Graph::AddEdge(int source, int destination, int weight) { } void Graph::PrintGraph(void) { - for (int i = 1; i < vertices.size(); i++) { - std::cout << "Source " << vertices.at(i).nodeNumber << " has "; - for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber; } + for (int i = 0; i < vertices.size(); i++) { + std::cout << "Source " << vertices.at(0).nodeNumber + 1 << " has "; + for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber + 1; } std::cout << std::endl; } std::cout << std::endl; } +std::vector CleanSolution(std::vector solution) { + std::vector solutionSanitized; + int i = solution.size() - 2; + Vertex* destination = solution.back(); + Vertex* source = destination; + while (destination != solution.front()) { + solutionSanitized.push_back(destination); + while (source == destination && i > 0) { + for (Edge e : solution.at(i)->edges) { + if (e.destination->nodeNumber == destination->nodeNumber) { + source = solution.at(i); + } + } + i--; + } + } + solutionSanitized.push_back(destination); + return solutionSanitized; +} + void PrintSolution(std::vector solution) { int cost = 0; - std::cout << "Path: "; - Vertex* lastVertex = solution.front(); - for (Vertex* i : solution) { - std::cout << i->nodeNumber << ' '; - for (Edge j : lastVertex->edges) { - if (j.destination->nodeNumber == i->nodeNumber) { - cost += j.weight; - } - } - lastVertex = i; + std::cout << "Path: " << solution.at(solution.size() - 1)->nodeNumber; + for (int i = solution.size() - 2; i > 0; --i) { + std::cout << "<-" << solution.at(i)->nodeNumber; + cost += solution.at(i + 1)->edges.at(solution.at(i)->nodeNumber).weight; } std::cout << std::endl; std::cout << "Cost: " << cost << std::endl; @@ -130,22 +144,28 @@ std::vector BFS(Graph graph, int start, int end) { std::vector DFS(Graph graph, int start, int end) { // Setup std::vector solution; - std::stack nodeStack; - std::vector explored(graph.nodeCount + 1, 0); - nodeStack.push(start); + std::vector visited(graph.nodeCount, false); + std::stack vertexStack; + Vertex* vertexCurrent; + solution.emplace_back(&graph.vertices.at(start)); + vertexStack.push(&graph.vertices.at(start)); // Search - while (!nodeStack.empty()) { - int currentNode = nodeStack.top(); - nodeStack.pop(); - solution.push_back(&(graph.vertices.at(currentNode))); - if (currentNode == end) { break; } - if (!explored.at(currentNode)) { - explored.at(currentNode) = 1; - for (Edge i : graph.vertices.at(currentNode).edges) { - nodeStack.push(i.destination->nodeNumber); + while (!vertexStack.empty()) { + vertexCurrent = vertexStack.top(); + solution.push_back(vertexCurrent); + vertexStack.pop(); + if (vertexCurrent->nodeNumber == end) { return solution; } + for (Edge e : vertexCurrent->edges) { + if (visited[e.destination->nodeNumber]) { continue; } // If visited + visited[e.destination->nodeNumber] = true; + for (auto v = e.destination->edges.rbegin(); v != e.destination->edges.rend(); ++v) { + if (!visited[v->destination->nodeNumber]) { + vertexStack.push(&graph.vertices.at(v->destination->nodeNumber)); + } } } } + return solution; } diff --git a/src/algorithm.hpp b/src/algorithm.hpp index 936dfa5..ce12c5d 100644 --- a/src/algorithm.hpp +++ b/src/algorithm.hpp @@ -28,6 +28,7 @@ struct Graph { }; Graph ReadInGraph(std::string fileLocation); +std::vector CleanSolution(std::vector solution); void PrintSolution(std::vector solution); void UCBFS(Graph graph, int start); diff --git a/src/main.cpp b/src/main.cpp index 9bb2081..08a8e0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,23 +8,25 @@ int main(int argc, char* argv[]) { std::cerr << "Usage: search-algorithms graphFile.txt" << std::endl; return 1; } + std::vector solution; Graph newGraph = ReadInGraph(argv[1]); std::cout << "BFS" << std::endl; std::cout << "---" << std::endl; auto t_start = std::chrono::high_resolution_clock::now(); - PrintSolution(BFS(newGraph, 1, 1)); + PrintSolution(BFS(newGraph, 0, 0)); auto t_end = std::chrono::high_resolution_clock::now(); - for (int i = 2; i < newGraph.nodeCount + 1; i++) { - PrintSolution(BFS(newGraph, 1, i)); + for (int i = 1; i < newGraph.nodeCount; i++) { + PrintSolution(BFS(newGraph, 0, i)); } std::cout << "BFS Time: " << std::chrono::duration(t_end-t_start).count() << std::endl; std::cout << "DFS" << std::endl; std::cout << "---" << std::endl; t_start = std::chrono::high_resolution_clock::now(); - PrintSolution(DFS(newGraph, 1, 1)); + solution = DFS(newGraph, 0, 0); + PrintSolution(solution); t_end = std::chrono::high_resolution_clock::now(); - for (int i = 2; i < newGraph.nodeCount + 1; i++) { - PrintSolution(DFS(newGraph, 1, i)); + for (int i = 1; i < newGraph.nodeCount; i++) { + PrintSolution(CleanSolution(DFS(newGraph, 0, i))); } std::cout << "DFS Time: " << std::chrono::duration(t_end-t_start).count() << std::endl; std::cout << "UCBFS" << std::endl; diff --git a/test/input/graphGenerated b/test/input/graphGenerated new file mode 100644 index 0000000..80a2084 --- /dev/null +++ b/test/input/graphGenerated @@ -0,0 +1,11 @@ +10 +1 2 2 3 3 +2 4 1 5 4 +3 6 2 +4 8 2 +5 9 3 +6 8 1 +7 9 2 +8 10 3 +9 10 2 +10 diff --git a/test/test.cpp b/test/test.cpp index b648774..35ff3ad 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -55,11 +55,25 @@ void test_AllEdgesAdded(void) { TEST_ASSERT_EQUAL_INT(9, testGraph.vertices.at(9).edges.at(1).destination->nodeNumber + 1); } +void test_BFS(void) { +} + +void test_DFS(void) { + testGraph = ReadInGraph("test/input/graphGenerated"); + std::vector solution = CleanSolution(DFS(testGraph, 0, 9)); + TEST_ASSERT_EQUAL_INT(9, solution.at(4)); + TEST_ASSERT_EQUAL_INT(7, solution.at(3)); + TEST_ASSERT_EQUAL_INT(3, solution.at(2)); + TEST_ASSERT_EQUAL_INT(1, solution.at(1)); + TEST_ASSERT_EQUAL_INT(0, solution.at(0)); +} + int main(void) { UNITY_BEGIN(); RUN_TEST(test_GraphReadsIn); RUN_TEST(test_AllVerticesAdded); RUN_TEST(test_AllEdgesAdded); + RUN_TEST(test_DFS); return UNITY_END(); }