Compare commits

..

3 Commits
master ... dev

Author SHA1 Message Date
f9b7ff0349 Added current progress of a better written solution 2023-09-28 14:21:04 -05:00
259e26c257 Fixed graph reading in wrong 2023-09-15 17:01:51 -05:00
9dcfef9dd5 Fixed Unity 2023-09-15 16:34:07 -05:00
9 changed files with 143 additions and 58 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "test/Unity"]
path = test/Unity
url = https://github.com/ThrowTheSwitch/Unity.git

View File

@ -9,8 +9,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
include_directories(${search-algorithms_SOURCE_DIR}/src)
add_subdirectory(src) add_subdirectory(src)
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")

View File

@ -6,7 +6,6 @@
#include <queue> #include <queue>
Graph ReadInGraph(std::string fileLocation) { Graph ReadInGraph(std::string fileLocation) {
std::cout << fileLocation << std::endl;
std::ifstream graphFile(fileLocation); std::ifstream graphFile(fileLocation);
if (!graphFile.is_open()) { if (!graphFile.is_open()) {
std::cerr << "Error opening file, check filename" << std::endl; std::cerr << "Error opening file, check filename" << std::endl;
@ -14,14 +13,17 @@ Graph ReadInGraph(std::string fileLocation) {
} }
Graph newGraph; Graph newGraph;
graphFile >> newGraph.nodeCount; graphFile >> newGraph.nodeCount;
newGraph.vertices.resize(newGraph.nodeCount + 1); newGraph.vertices.resize(newGraph.nodeCount);
std::string line; std::string line;
int source, destination, weight; int source, destination, weight;
std::getline(graphFile, line);
while (std::getline(graphFile, line)) { while (std::getline(graphFile, line)) {
std::istringstream iss(line); std::istringstream iss(line);
iss >> source; iss >> source;
source--;
do { do {
iss >> destination >> weight; iss >> destination >> weight;
destination--;
newGraph.vertices.at(source).nodeNumber = source; newGraph.vertices.at(source).nodeNumber = source;
newGraph.AddEdge(source, destination, weight); newGraph.AddEdge(source, destination, weight);
} while (!iss.eof()); } while (!iss.eof());
@ -31,8 +33,7 @@ Graph ReadInGraph(std::string fileLocation) {
} }
Graph::Graph(void) { Graph::Graph(void) {
nodeCount = -1; nodeCount = 0;
} }
Edge::Edge(Vertex* destination, int weight) { Edge::Edge(Vertex* destination, int weight) {
@ -45,26 +46,40 @@ void Graph::AddEdge(int source, int destination, int weight) {
} }
void Graph::PrintGraph(void) { void Graph::PrintGraph(void) {
for (int i = 1; i < vertices.size(); i++) { for (int i = 0; i < vertices.size(); i++) {
std::cout << "Source " << vertices.at(i).nodeNumber << " has "; std::cout << "Source " << vertices.at(0).nodeNumber + 1 << " has ";
for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber; } for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber + 1; }
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << std::endl; std::cout << std::endl;
} }
std::vector<Vertex*> CleanSolution(std::vector<Vertex*> solution) {
std::vector<Vertex*> 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<Vertex*> solution) { void PrintSolution(std::vector<Vertex*> solution) {
int cost = 0; int cost = 0;
std::cout << "Path: "; std::cout << "Path: " << solution.at(solution.size() - 1)->nodeNumber;
Vertex* lastVertex = solution.front(); for (int i = solution.size() - 2; i > 0; --i) {
for (Vertex* i : solution) { std::cout << "<-" << solution.at(i)->nodeNumber;
std::cout << i->nodeNumber << ' '; cost += solution.at(i + 1)->edges.at(solution.at(i)->nodeNumber).weight;
for (Edge j : lastVertex->edges) {
if (j.destination->nodeNumber == i->nodeNumber) {
cost += j.weight;
}
}
lastVertex = i;
} }
std::cout << std::endl; std::cout << std::endl;
std::cout << "Cost: " << cost << std::endl; std::cout << "Cost: " << cost << std::endl;
@ -129,22 +144,28 @@ std::vector<Vertex*> BFS(Graph graph, int start, int end) {
std::vector<Vertex*> DFS(Graph graph, int start, int end) { std::vector<Vertex*> DFS(Graph graph, int start, int end) {
// Setup // Setup
std::vector<Vertex*> solution; std::vector<Vertex*> solution;
std::stack<int> nodeStack; std::vector<bool> visited(graph.nodeCount, false);
std::vector<bool> explored(graph.nodeCount + 1, 0); std::stack<Vertex*> vertexStack;
nodeStack.push(start); Vertex* vertexCurrent;
solution.emplace_back(&graph.vertices.at(start));
vertexStack.push(&graph.vertices.at(start));
// Search // Search
while (!nodeStack.empty()) { while (!vertexStack.empty()) {
int currentNode = nodeStack.top(); vertexCurrent = vertexStack.top();
nodeStack.pop(); solution.push_back(vertexCurrent);
solution.push_back(&(graph.vertices.at(currentNode))); vertexStack.pop();
if (currentNode == end) { break; } if (vertexCurrent->nodeNumber == end) { return solution; }
if (!explored.at(currentNode)) { for (Edge e : vertexCurrent->edges) {
explored.at(currentNode) = 1; if (visited[e.destination->nodeNumber]) { continue; } // If visited
for (Edge i : graph.vertices.at(currentNode).edges) { visited[e.destination->nodeNumber] = true;
nodeStack.push(i.destination->nodeNumber); 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; return solution;
} }

View File

@ -28,6 +28,7 @@ struct Graph {
}; };
Graph ReadInGraph(std::string fileLocation); Graph ReadInGraph(std::string fileLocation);
std::vector<Vertex*> CleanSolution(std::vector<Vertex*> solution);
void PrintSolution(std::vector<Vertex*> solution); void PrintSolution(std::vector<Vertex*> solution);
void UCBFS(Graph graph, int start); void UCBFS(Graph graph, int start);

View File

@ -8,23 +8,25 @@ int main(int argc, char* argv[]) {
std::cerr << "Usage: search-algorithms graphFile.txt" << std::endl; std::cerr << "Usage: search-algorithms graphFile.txt" << std::endl;
return 1; return 1;
} }
std::vector<Vertex*> solution;
Graph newGraph = ReadInGraph(argv[1]); Graph newGraph = ReadInGraph(argv[1]);
std::cout << "BFS" << std::endl; std::cout << "BFS" << std::endl;
std::cout << "---" << std::endl; std::cout << "---" << std::endl;
auto t_start = std::chrono::high_resolution_clock::now(); 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(); auto t_end = std::chrono::high_resolution_clock::now();
for (int i = 2; i < newGraph.nodeCount + 1; i++) { for (int i = 1; i < newGraph.nodeCount; i++) {
PrintSolution(BFS(newGraph, 1, i)); PrintSolution(BFS(newGraph, 0, i));
} }
std::cout << "BFS Time: " << std::chrono::duration<double, std::milli>(t_end-t_start).count() << std::endl; std::cout << "BFS Time: " << std::chrono::duration<double, std::milli>(t_end-t_start).count() << std::endl;
std::cout << "DFS" << std::endl; std::cout << "DFS" << std::endl;
std::cout << "---" << std::endl; std::cout << "---" << std::endl;
t_start = std::chrono::high_resolution_clock::now(); 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(); t_end = std::chrono::high_resolution_clock::now();
for (int i = 2; i < newGraph.nodeCount + 1; i++) { for (int i = 1; i < newGraph.nodeCount; i++) {
PrintSolution(DFS(newGraph, 1, i)); PrintSolution(CleanSolution(DFS(newGraph, 0, i)));
} }
std::cout << "DFS Time: " << std::chrono::duration<double, std::milli>(t_end-t_start).count() << std::endl; std::cout << "DFS Time: " << std::chrono::duration<double, std::milli>(t_end-t_start).count() << std::endl;
std::cout << "UCBFS" << std::endl; std::cout << "UCBFS" << std::endl;

View File

@ -1,7 +1,8 @@
add_subdirectory(Unity) find_package(unity REQUIRED)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}") include_directories(${CMAKE_SOURCE_DIR}/src)
add_executable(testing add_executable(testing
./test.cpp test.cpp
../src/algorithm.cpp
) )
set_target_properties(testing PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(testing PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(testing unity) target_link_libraries(testing unity)

@ -1 +0,0 @@
Subproject commit 2775e1b05875cf45afce7153e36af76ddbfdba26

11
test/input/graphGenerated Normal file
View File

@ -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

View File

@ -1,24 +1,79 @@
#include "unity.h" #include <unity/unity.h>
#include "unity_internals.h" #include <unity/unity_internals.h>
#include "../src/algorithm.hpp"
void setUp() { } void setUp() { ; }
void tearDown() { } void tearDown() { ; }
void test_SUCCESS(void) Graph testGraph;
{
TEST_ASSERT_EQUAL_INT(4, 2+2); void test_GraphReadsIn(void) {
testGraph = ReadInGraph("test/input/graphPosLittle");
TEST_ASSERT_EQUAL_INT(10, testGraph.nodeCount);
} }
void test_FAILURE(void) void test_AllVerticesAdded(void) {
{ for (int i = 0; i < 10; i++) {
TEST_ASSERT_EQUAL_INT(4, 2+3); TEST_ASSERT_EQUAL_INT(i, testGraph.vertices.at(i).nodeNumber);
}
} }
int main(void) void test_AllEdgesAdded(void) {
{ // Edges for vertex 1
TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(0).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(2, testGraph.vertices.at(0).edges.at(1).destination->nodeNumber + 1);
// Edges for vertex 2
TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(1).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(1).edges.at(1).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(1).edges.at(2).destination->nodeNumber + 1);
// Edges for vertex 3
TEST_ASSERT_EQUAL_INT(6, testGraph.vertices.at(2).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(2).edges.at(1).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(2).edges.at(2).destination->nodeNumber + 1);
// Edges for vertex 4
TEST_ASSERT_EQUAL_INT(8, testGraph.vertices.at(3).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(10, testGraph.vertices.at(3).edges.at(1).destination->nodeNumber + 1);
// Edges for vertex 5
TEST_ASSERT_EQUAL_INT(3, testGraph.vertices.at(4).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(8, testGraph.vertices.at(4).edges.at(1).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(4).edges.at(2).destination->nodeNumber + 1);
// Edges for vertex 6
TEST_ASSERT_EQUAL_INT(3, testGraph.vertices.at(5).edges.at(0).destination->nodeNumber + 1);
// Edges for vertex 7
TEST_ASSERT_EQUAL_INT(2, testGraph.vertices.at(6).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(9, testGraph.vertices.at(6).edges.at(1).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(1, testGraph.vertices.at(6).edges.at(2).destination->nodeNumber + 1);
// Edges for vertex 8
TEST_ASSERT_EQUAL_INT(5, testGraph.vertices.at(7).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(4, testGraph.vertices.at(7).edges.at(1).destination->nodeNumber + 1);
// Edges for vertex 9
TEST_ASSERT_EQUAL_INT(6, testGraph.vertices.at(8).edges.at(0).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(10, testGraph.vertices.at(8).edges.at(1).destination->nodeNumber + 1);
TEST_ASSERT_EQUAL_INT(7, testGraph.vertices.at(8).edges.at(2).destination->nodeNumber + 1);
// Edges for vertex 10
TEST_ASSERT_EQUAL_INT(4, testGraph.vertices.at(9).edges.at(0).destination->nodeNumber + 1);
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<Vertex*> 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(); UNITY_BEGIN();
RUN_TEST(test_SUCCESS); RUN_TEST(test_GraphReadsIn);
RUN_TEST(test_FAILURE); RUN_TEST(test_AllVerticesAdded);
RUN_TEST(test_AllEdgesAdded);
RUN_TEST(test_DFS);
return UNITY_END(); return UNITY_END();
} }