Added current progress of a better written solution
This commit is contained in:
parent
259e26c257
commit
f9b7ff0349
@ -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<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;
|
||||
@ -130,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;
|
||||
|
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
|
@ -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<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_GraphReadsIn);
|
||||
RUN_TEST(test_AllVerticesAdded);
|
||||
RUN_TEST(test_AllEdgesAdded);
|
||||
RUN_TEST(test_DFS);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user