From 57cde52b2745662d716b2c0bfd155ad6ef142637 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Tue, 21 Jun 2022 21:24:50 +0200 Subject: [PATCH] Prim's algo --- Uebung 5/Uebung5_1/graph.cpp | 40 ++++++++++++++ Uebung 5/Uebung5_1/graph.h | 103 ++++++++++++++++++++++------------- 2 files changed, 104 insertions(+), 39 deletions(-) diff --git a/Uebung 5/Uebung5_1/graph.cpp b/Uebung 5/Uebung5_1/graph.cpp index 8962e9a..5b882eb 100644 --- a/Uebung 5/Uebung5_1/graph.cpp +++ b/Uebung 5/Uebung5_1/graph.cpp @@ -445,3 +445,43 @@ std::string Graph::getNearestNeighbour(const std::string &vertex, s << this->vertices[nearestNeighbour]; return s.str(); } + +// Prim's algorithm +std::vector> Graph::MST(const std::string &vertex) { + std::vector> mst; + mst.resize(this->vertices.size(), std::vector(this->vertices.size())); + std::vector visited; + visited.resize(this->vertices.size(), false); + int vertexIndex = this->resolveVertex(vertex); + visited[vertexIndex] = true; + + int neighbourIndex = + this->resolveVertex(this->getNearestNeighbour(vertex, visited)); + mst[vertexIndex][neighbourIndex] = + this->adjacencyMatrix[vertexIndex][neighbourIndex]; + while (std::count(visited.begin(), visited.end(), false) > 0) { + + int leastweight = INT_MAX; + std::string leastVertex; + std::string lastVisited; + for (int i = 0; i < this->vertices.size(); i++) { + if (visited[i]) { + std::vector theNeighbours = this->neighbours(vertex); + for (auto it = theNeighbours.begin(); it != theNeighbours.end(); it++) { + if (!visited[resolveVertex(*it)] && + this->adjacencyMatrix[this->resolveVertex(vertex)][i] < + leastweight) { + leastweight = this->adjacencyMatrix[this->resolveVertex(vertex)][i]; + leastVertex = *it; + lastVisited = vertices[i]; + } + } + } + } + visited[this->resolveVertex(lastVisited)] = true; + mst[this->resolveVertex(lastVisited)][this->resolveVertex(leastVertex)] = + this->adjacencyMatrix[this->resolveVertex(lastVisited)] + [this->resolveVertex(leastVertex)]; + } + return mst; +} diff --git a/Uebung 5/Uebung5_1/graph.h b/Uebung 5/Uebung5_1/graph.h index 988af47..63eec2f 100644 --- a/Uebung 5/Uebung5_1/graph.h +++ b/Uebung 5/Uebung5_1/graph.h @@ -1,9 +1,9 @@ -#include /* type vector */ -#include /* type string */ -#include /* cout, cerr */ -#include /* type stringstream to easily concat a result string */ -#include /* INT_MAX */ #include +#include /* cout, cerr */ +#include /* INT_MAX */ +#include /* type stringstream to easily concat a result string */ +#include /* type string */ +#include /* type vector */ #include "edge.h" @@ -11,42 +11,67 @@ void vectorToString(std::vector vector); - - /* -- Class Graph -- */ class Graph { - private: - std::vector vertices; // vector connecting vertices with indicies; needed for name resolution - void _letTheSalesmanTravel(const std::string& vertex, std::vector& visited, const std::string& startingPoint); - public: - Graph(); // default constructor - Graph(const std::vector& vertices, const std::vector& edges); // param. constructor - std::vector> adjacencyMatrix; // vector of vectors containing integers (the adjacency matrix) - - /* -- - Resolves a given string of a vertex and returns its position in the - adjacencyMatrix (as integer). Returns -1 if name could not be resolved, - which indicates the the name was not found. - -- */ - int resolveVertex(const std::string& name); - void printGraph(); // prints out the graph with vertices and the adjacencyMatrix +private: + std::vector vertices; // vector connecting vertices with + // indicies; needed for name resolution + void _letTheSalesmanTravel(const std::string &vertex, + std::vector &visited, + const std::string &startingPoint); - /* -- - Graph Manipulation Functions - -- */ - void insertVertex(const std::string& vertex); // inserts a new vertex; throws error, if vertex already exists in Graph - void deleteVertex(const std::string& vertex); // deletes a vertex from the Graph; throws an error, if vertex does not exist - void insertEdge(const Edge& edge); // inserts a new edge; parameter can be {std::string, std::string} due to implicit cast... - // ...does not check if the edge already exists, nor notifies user/programmer - void deleteEdge(const Edge& edge); // deletes an edge; parameter can be {std::string, std::string} due to implicit cast... - // ...does not check if edge exists - bool adjacent(const std::string& vertex1, const std::string& vertex2); // checks if vertex1 and vertex2 are adjacent; returns boolean; adjacency indicates that... - // ...a direct connection between a <--> b (direction NOT important) exists - std::vector neighbours(const std::string& vertex); // returns a vector of strings, containing all neighbouring vertices of the parameter vertex... - // ...Neighbours are all vertices that a given vertex is connected to through OUTGOING edges - - int* performDijkstra(const std::string& sourceVertex); // Dijkstra algorithm implementation - void letTheSalesmanTravel(const std::string& vertex); - std::string getNearestNeighbour(const std::string& vertex, std::vector& visited); +public: + Graph(); // default constructor + Graph(const std::vector &vertices, + const std::vector &edges); // param. constructor + std::vector> + adjacencyMatrix; // vector of vectors containing integers (the adjacency + // matrix) + std::vector> MST(const std::string &vertex); + /* -- + Resolves a given string of a vertex and returns its position in the + adjacencyMatrix (as integer). Returns -1 if name could not be resolved, + which indicates the the name was not found. + -- */ + int resolveVertex(const std::string &name); + void + printGraph(); // prints out the graph with vertices and the adjacencyMatrix + + /* -- + Graph Manipulation Functions + -- */ + void insertVertex( + const std::string &vertex); // inserts a new vertex; throws error, if + // vertex already exists in Graph + void deleteVertex( + const std::string &vertex); // deletes a vertex from the Graph; throws an + // error, if vertex does not exist + void insertEdge( + const Edge &edge); // inserts a new edge; parameter can be {std::string, + // std::string} due to implicit cast... + // ...does not check if the edge already exists, nor + // notifies user/programmer + void deleteEdge( + const Edge &edge); // deletes an edge; parameter can be {std::string, + // std::string} due to implicit cast... + // ...does not check if edge exists + bool adjacent(const std::string &vertex1, + const std::string + &vertex2); // checks if vertex1 and vertex2 are adjacent; + // returns boolean; adjacency indicates that... + // ...a direct connection between a <--> b + // (direction NOT important) exists + std::vector + neighbours(const std::string + &vertex); // returns a vector of strings, containing all + // neighbouring vertices of the parameter vertex... + // ...Neighbours are all vertices that a given + // vertex is connected to through OUTGOING edges + + int *performDijkstra( + const std::string &sourceVertex); // Dijkstra algorithm implementation + void letTheSalesmanTravel(const std::string &vertex); + std::string getNearestNeighbour(const std::string &vertex, + std::vector &visited); }; \ No newline at end of file