From ce424df021ac24a5fb446891ded232c49c2a728e Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sun, 10 Sep 2023 16:29:15 -0500 Subject: [PATCH] Changed graph struct, partially added BFS --- src/algorithm.cpp | 72 +++++++++++++++++++++++++++++++++++++++++------ src/algorithm.hpp | 32 ++++++++++++++++++++- src/main.cpp | 3 ++ 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/src/algorithm.cpp b/src/algorithm.cpp index 64f0fc2..bdcecc0 100644 --- a/src/algorithm.cpp +++ b/src/algorithm.cpp @@ -3,6 +3,7 @@ #include #include #include +#include Graph ReadInGraph(std::string fileLocation) { std::cout << fileLocation << std::endl; @@ -13,7 +14,7 @@ Graph ReadInGraph(std::string fileLocation) { } Graph newGraph; graphFile >> newGraph.nodeCount; - newGraph.nodes.resize(newGraph.nodeCount + 1); + newGraph.vertices.resize(newGraph.nodeCount + 1); std::string line; int source, destination, weight; while (std::getline(graphFile, line)) { @@ -21,32 +22,87 @@ Graph ReadInGraph(std::string fileLocation) { iss >> source; do { iss >> destination >> weight; - newGraph.AddChild(source, destination, weight); + newGraph.vertices.at(source).nodeNumber = source; + newGraph.AddEdge(source, destination, weight); } while (!iss.eof()); } graphFile.close(); return newGraph; } +/* Old Node Node::Node(int destination, int weight) { this->destination = destination; this->weight = weight; -} +} +*/ Graph::Graph(void) { nodeCount = -1; + } -void Graph::AddChild(int source, int destination, int weight) { - nodes.at(source).push_back(Node(destination, weight)); +Edge::Edge(Vertex* destination, int weight) { + this->destination = destination; + this->weight = weight; +} + +void Graph::AddEdge(int source, int destination, int weight) { + vertices.at(source).edges.emplace_back(&(vertices.at(destination)), weight); } void Graph::PrintGraph(void) { - for (int i = 1; i < nodes.size(); i++) { - std::cout << "Source " << i << " "; - for (auto j : nodes.at(i)) { std::cout << "-> " << j.destination; } + for (auto i : vertices) { + std::cout << "Source " << i.nodeNumber << " has "; + for (auto j : i.edges) { std::cout << " " << j.destination->nodeNumber; } std::cout << std::endl; } std::cout << std::endl; } +void PrintSolution(std::vector solution) { + std::cout << "Path: "; + for (auto i : solution) { std::cout << i->nodeNumber << ' '; } + std::cout << std::endl; +} + +std::vector UCBFS(Graph graph, int start, int end) { + std::vector solution; + + + + return solution; +} + +std::vector BFS(Graph graph, int start, int end) { + // Setup + std::vector solution; + std::queue nodeQueue; + nodeQueue.push(start); + + // Search + while (!nodeQueue.empty()) { + int currentNode = nodeQueue.front(); + solution.push_back(&graph.vertices.at(currentNode)); + nodeQueue.pop(); + for (auto i : graph.vertices.at(currentNode).edges) { + nodeQueue.push(i.destination->nodeNumber); + } + if (currentNode == end) { return solution; } // Solution found + if (solution.size() > graph.nodeCount) { + std::cout << "Path too long" << std::endl; + break; + } + } + solution.clear(); + solution.push_back(&graph.vertices.at(0)); + return solution; +} + +std::vector DFS(Graph graph, int start, int end) { + std::vector solution; + + + + return solution; +} diff --git a/src/algorithm.hpp b/src/algorithm.hpp index e2a3f37..964e0fc 100644 --- a/src/algorithm.hpp +++ b/src/algorithm.hpp @@ -4,6 +4,30 @@ #include #include +typedef struct Edge Edge; +typedef struct Vertex Vertex; + +struct Edge { + Vertex* destination; + int weight; + Edge(Vertex* destination, int weight); +}; + +struct Vertex { + int nodeNumber; + std::vector edges; + void AddEdge(Vertex* destination, int weight); +}; + +struct Graph { + int nodeCount; + std::vector vertices; + Graph(void); + void AddEdge(int source, int destination, int weight); + void PrintGraph(void); +}; + +/* OLD IMPLEMENTATION struct Node { int destination; int weight; @@ -14,10 +38,16 @@ struct Graph { int nodeCount; std::vector> nodes; Graph(void); - void AddChild(int source, int destination, int weight); + void AddEdge(int source, int destination, int weight); void PrintGraph(void); }; + */ Graph ReadInGraph(std::string fileLocation); +void PrintSolution(std::vector solution); + +std::vector UCBFS(Graph graph, int start, int end); +std::vector BFS(Graph graph, int start, int end); +std::vector DFS(Graph graph, int start, int end); #endif diff --git a/src/main.cpp b/src/main.cpp index d2eb6e2..ed0454b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,5 +8,8 @@ int main(int argc, char* argv[]) { } Graph newGraph = ReadInGraph(argv[1]); newGraph.PrintGraph(); + for (int i = 1; i < newGraph.nodeCount; i++) { + PrintSolution(BFS(newGraph, 1, i)); + } return 0; }