Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
f9b7ff0349 | |||
259e26c257 | |||
9dcfef9dd5 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "test/Unity"]
|
||||
path = test/Unity
|
||||
url = https://github.com/ThrowTheSwitch/Unity.git
|
@ -9,8 +9,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
|
||||
include_directories(${search-algorithms_SOURCE_DIR}/src)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include <queue>
|
||||
|
||||
Graph ReadInGraph(std::string fileLocation) {
|
||||
std::cout << fileLocation << std::endl;
|
||||
std::ifstream graphFile(fileLocation);
|
||||
if (!graphFile.is_open()) {
|
||||
std::cerr << "Error opening file, check filename" << std::endl;
|
||||
@ -14,14 +13,17 @@ Graph ReadInGraph(std::string fileLocation) {
|
||||
}
|
||||
Graph newGraph;
|
||||
graphFile >> newGraph.nodeCount;
|
||||
newGraph.vertices.resize(newGraph.nodeCount + 1);
|
||||
newGraph.vertices.resize(newGraph.nodeCount);
|
||||
std::string line;
|
||||
int source, destination, weight;
|
||||
std::getline(graphFile, line);
|
||||
while (std::getline(graphFile, line)) {
|
||||
std::istringstream iss(line);
|
||||
iss >> source;
|
||||
source--;
|
||||
do {
|
||||
iss >> destination >> weight;
|
||||
destination--;
|
||||
newGraph.vertices.at(source).nodeNumber = source;
|
||||
newGraph.AddEdge(source, destination, weight);
|
||||
} while (!iss.eof());
|
||||
@ -31,8 +33,7 @@ Graph ReadInGraph(std::string fileLocation) {
|
||||
}
|
||||
|
||||
Graph::Graph(void) {
|
||||
nodeCount = -1;
|
||||
|
||||
nodeCount = 0;
|
||||
}
|
||||
|
||||
Edge::Edge(Vertex* destination, int weight) {
|
||||
@ -45,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<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) {
|
||||
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;
|
||||
@ -129,22 +144,28 @@ std::vector<Vertex*> BFS(Graph graph, int start, int end) {
|
||||
std::vector<Vertex*> DFS(Graph graph, int start, int end) {
|
||||
// Setup
|
||||
std::vector<Vertex*> solution;
|
||||
std::stack<int> nodeStack;
|
||||
std::vector<bool> explored(graph.nodeCount + 1, 0);
|
||||
nodeStack.push(start);
|
||||
std::vector<bool> visited(graph.nodeCount, false);
|
||||
std::stack<Vertex*> 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;
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ struct Graph {
|
||||
};
|
||||
|
||||
Graph ReadInGraph(std::string fileLocation);
|
||||
std::vector<Vertex*> CleanSolution(std::vector<Vertex*> solution);
|
||||
void PrintSolution(std::vector<Vertex*> solution);
|
||||
|
||||
void UCBFS(Graph graph, int start);
|
||||
|
14
src/main.cpp
14
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<Vertex*> 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<double, std::milli>(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<double, std::milli>(t_end-t_start).count() << std::endl;
|
||||
std::cout << "UCBFS" << std::endl;
|
||||
|
@ -1,7 +1,8 @@
|
||||
add_subdirectory(Unity)
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
find_package(unity REQUIRED)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||
add_executable(testing
|
||||
./test.cpp
|
||||
test.cpp
|
||||
../src/algorithm.cpp
|
||||
)
|
||||
set_target_properties(testing PROPERTIES LINKER_LANGUAGE CXX)
|
||||
target_link_libraries(testing unity)
|
||||
|
@ -1 +0,0 @@
|
||||
Subproject commit 2775e1b05875cf45afce7153e36af76ddbfdba26
|
11
test/input/graphGenerated
Normal file
11
test/input/graphGenerated
Normal 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
|
@ -1,24 +1,79 @@
|
||||
#include "unity.h"
|
||||
#include "unity_internals.h"
|
||||
#include <unity/unity.h>
|
||||
#include <unity/unity_internals.h>
|
||||
#include "../src/algorithm.hpp"
|
||||
|
||||
void setUp() { }
|
||||
void tearDown() { }
|
||||
void setUp() { ; }
|
||||
void tearDown() { ; }
|
||||
|
||||
void test_SUCCESS(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(4, 2+2);
|
||||
Graph testGraph;
|
||||
|
||||
void test_GraphReadsIn(void) {
|
||||
testGraph = ReadInGraph("test/input/graphPosLittle");
|
||||
TEST_ASSERT_EQUAL_INT(10, testGraph.nodeCount);
|
||||
}
|
||||
|
||||
void test_FAILURE(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(4, 2+3);
|
||||
void test_AllVerticesAdded(void) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
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();
|
||||
RUN_TEST(test_SUCCESS);
|
||||
RUN_TEST(test_FAILURE);
|
||||
RUN_TEST(test_GraphReadsIn);
|
||||
RUN_TEST(test_AllVerticesAdded);
|
||||
RUN_TEST(test_AllEdgesAdded);
|
||||
RUN_TEST(test_DFS);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user