Prim's algo

This commit is contained in:
Samuel Oberhofer 2022-06-21 21:24:50 +02:00
parent 86f7340599
commit 57cde52b27
2 changed files with 104 additions and 39 deletions

View File

@ -445,3 +445,43 @@ std::string Graph::getNearestNeighbour(const std::string &vertex,
s << this->vertices[nearestNeighbour]; s << this->vertices[nearestNeighbour];
return s.str(); return s.str();
} }
// Prim's algorithm
std::vector<std::vector<int>> Graph::MST(const std::string &vertex) {
std::vector<std::vector<int>> mst;
mst.resize(this->vertices.size(), std::vector<int>(this->vertices.size()));
std::vector<bool> 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<std::string> 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;
}

View File

@ -1,9 +1,9 @@
#include <vector> /* type vector */
#include <string> /* type string */
#include <iostream> /* cout, cerr */
#include <sstream> /* type stringstream to easily concat a result string */
#include <limits.h> /* INT_MAX */
#include <algorithm> #include <algorithm>
#include <iostream> /* cout, cerr */
#include <limits.h> /* INT_MAX */
#include <sstream> /* type stringstream to easily concat a result string */
#include <string> /* type string */
#include <vector> /* type vector */
#include "edge.h" #include "edge.h"
@ -11,42 +11,67 @@
void vectorToString(std::vector<std::string> vector); void vectorToString(std::vector<std::string> vector);
/* -- Class Graph -- */ /* -- Class Graph -- */
class Graph { class Graph {
private: private:
std::vector<std::string> vertices; // vector connecting vertices with indicies; needed for name resolution std::vector<std::string> vertices; // vector connecting vertices with
void _letTheSalesmanTravel(const std::string& vertex, std::vector<bool>& visited, const std::string& startingPoint); // indicies; needed for name resolution
public: void _letTheSalesmanTravel(const std::string &vertex,
Graph(); // default constructor std::vector<bool> &visited,
Graph(const std::vector<std::string>& vertices, const std::vector<Edge>& edges); // param. constructor const std::string &startingPoint);
std::vector<std::vector<int>> adjacencyMatrix; // vector of vectors containing integers (the adjacency matrix)
/* -- public:
Resolves a given string of a vertex and returns its position in the Graph(); // default constructor
adjacencyMatrix (as integer). Returns -1 if name could not be resolved, Graph(const std::vector<std::string> &vertices,
which indicates the the name was not found. const std::vector<Edge> &edges); // param. constructor
-- */ std::vector<std::vector<int>>
int resolveVertex(const std::string& name); adjacencyMatrix; // vector of vectors containing integers (the adjacency
void printGraph(); // prints out the graph with vertices and the adjacencyMatrix // matrix)
std::vector<std::vector<int>> MST(const std::string &vertex);
/* -- /* --
Graph Manipulation Functions Resolves a given string of a vertex and returns its position in the
-- */ adjacencyMatrix (as integer). Returns -1 if name could not be resolved,
void insertVertex(const std::string& vertex); // inserts a new vertex; throws error, if vertex already exists in Graph which indicates the the name was not found.
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... int resolveVertex(const std::string &name);
// ...does not check if the edge already exists, nor notifies user/programmer void
void deleteEdge(const Edge& edge); // deletes an edge; parameter can be {std::string, std::string} due to implicit cast... printGraph(); // prints out the graph with vertices and the adjacencyMatrix
// ...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<std::string> 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); Graph Manipulation Functions
std::string getNearestNeighbour(const std::string& vertex, std::vector<bool>& visited); -- */
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<std::string>
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<bool> &visited);
}; };