Compare commits

..

No commits in common. "dev" and "master" have entirely different histories.
dev ... master

9 changed files with 58 additions and 143 deletions

3
.gitmodules vendored
View File

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

View File

@ -9,6 +9,8 @@ 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,6 +6,7 @@
#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;
@ -13,17 +14,14 @@ Graph ReadInGraph(std::string fileLocation) {
} }
Graph newGraph; Graph newGraph;
graphFile >> newGraph.nodeCount; graphFile >> newGraph.nodeCount;
newGraph.vertices.resize(newGraph.nodeCount); newGraph.vertices.resize(newGraph.nodeCount + 1);
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());
@ -33,7 +31,8 @@ Graph ReadInGraph(std::string fileLocation) {
} }
Graph::Graph(void) { Graph::Graph(void) {
nodeCount = 0; nodeCount = -1;
} }
Edge::Edge(Vertex* destination, int weight) { Edge::Edge(Vertex* destination, int weight) {
@ -46,40 +45,26 @@ void Graph::AddEdge(int source, int destination, int weight) {
} }
void Graph::PrintGraph(void) { void Graph::PrintGraph(void) {
for (int i = 0; i < vertices.size(); i++) { for (int i = 1; i < vertices.size(); i++) {
std::cout << "Source " << vertices.at(0).nodeNumber + 1 << " has "; std::cout << "Source " << vertices.at(i).nodeNumber << " has ";
for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber + 1; } for (Edge j : vertices.at(i).edges) { std::cout << " " << j.destination->nodeNumber; }
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: " << solution.at(solution.size() - 1)->nodeNumber; std::cout << "Path: ";
for (int i = solution.size() - 2; i > 0; --i) { Vertex* lastVertex = solution.front();
std::cout << "<-" << solution.at(i)->nodeNumber; for (Vertex* i : solution) {
cost += solution.at(i + 1)->edges.at(solution.at(i)->nodeNumber).weight; std::cout << i->nodeNumber << ' ';
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;
@ -144,28 +129,22 @@ 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::vector<bool> visited(graph.nodeCount, false); std::stack<int> nodeStack;
std::stack<Vertex*> vertexStack; std::vector<bool> explored(graph.nodeCount + 1, 0);
Vertex* vertexCurrent; nodeStack.push(start);
solution.emplace_back(&graph.vertices.at(start));
vertexStack.push(&graph.vertices.at(start));
// Search // Search
while (!vertexStack.empty()) { while (!nodeStack.empty()) {
vertexCurrent = vertexStack.top(); int currentNode = nodeStack.top();
solution.push_back(vertexCurrent); nodeStack.pop();
vertexStack.pop(); solution.push_back(&(graph.vertices.at(currentNode)));
if (vertexCurrent->nodeNumber == end) { return solution; } if (currentNode == end) { break; }
for (Edge e : vertexCurrent->edges) { if (!explored.at(currentNode)) {
if (visited[e.destination->nodeNumber]) { continue; } // If visited explored.at(currentNode) = 1;
visited[e.destination->nodeNumber] = true; for (Edge i : graph.vertices.at(currentNode).edges) {
for (auto v = e.destination->edges.rbegin(); v != e.destination->edges.rend(); ++v) { nodeStack.push(i.destination->nodeNumber);
if (!visited[v->destination->nodeNumber]) {
vertexStack.push(&graph.vertices.at(v->destination->nodeNumber));
}
} }
} }
} }
return solution; return solution;
} }

View File

@ -28,7 +28,6 @@ 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,25 +8,23 @@ 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, 0, 0)); PrintSolution(BFS(newGraph, 1, 1));
auto t_end = std::chrono::high_resolution_clock::now(); auto t_end = std::chrono::high_resolution_clock::now();
for (int i = 1; i < newGraph.nodeCount; i++) { for (int i = 2; i < newGraph.nodeCount + 1; i++) {
PrintSolution(BFS(newGraph, 0, i)); PrintSolution(BFS(newGraph, 1, 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();
solution = DFS(newGraph, 0, 0); PrintSolution(DFS(newGraph, 1, 1));
PrintSolution(solution);
t_end = std::chrono::high_resolution_clock::now(); t_end = std::chrono::high_resolution_clock::now();
for (int i = 1; i < newGraph.nodeCount; i++) { for (int i = 2; i < newGraph.nodeCount + 1; i++) {
PrintSolution(CleanSolution(DFS(newGraph, 0, i))); PrintSolution(DFS(newGraph, 1, 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,8 +1,7 @@
find_package(unity REQUIRED) add_subdirectory(Unity)
include_directories(${CMAKE_SOURCE_DIR}/src) include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
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
test/Unity Submodule

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

View File

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