From 2dbd827031c285e2dbc690d39cb18e2eb2f37e16 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Fri, 24 Jun 2022 12:23:11 +0200 Subject: [PATCH] Fix --- Uebung 5/Uebung5_1/graph.cpp | 123 ++++++++++++++++++++++++++++++----- Uebung 5/Uebung5_1/graph.h | 4 ++ 2 files changed, 111 insertions(+), 16 deletions(-) diff --git a/Uebung 5/Uebung5_1/graph.cpp b/Uebung 5/Uebung5_1/graph.cpp index 5b882eb..8ad45ed 100644 --- a/Uebung 5/Uebung5_1/graph.cpp +++ b/Uebung 5/Uebung5_1/graph.cpp @@ -173,9 +173,11 @@ std::vector Graph::neighbours(const std::string &vertex) { for (int i = 0; i < this->adjacencyMatrix[indexVertex].size(); i++) { /* Loops through all entries of the subvector of adjacencyMatrix[indexVertex] */ - if (this->adjacencyMatrix[indexVertex][i] != - 0) { /* and checks if the entry at position [indexVertex][i] is not 0. - */ + if (this->adjacencyMatrix[indexVertex][i] != 0 && + this->adjacencyMatrix[indexVertex][i] < + INT_MAX) { /* and checks if the entry at position [indexVertex][i] + * is not 0. + */ resVector.push_back( this->vertices[i]); /* If the condition is fulfilled, add the vertex (name) to the result vector resVector */ @@ -371,7 +373,18 @@ void Graph::_letTheSalesmanTravel(const std::string &vertex, } std::cout << " -("; if (this->adjacencyMatrix[startIndex][vertexIndex] == INT_MAX) { - std::cout << "INVALID EDGE"; + + // std::cout << "INVALID123 EDGE"; + /* + std::vector path = + performDijkstraPath(vertices[startIndex], vertices[vertexIndex], + MST(vertices[startIndex])); + for (auto it = path.begin(); it != path.end(); ++it) { + std::cout << this->adjacencyMatrix[this->resolveVertex(*(it))] + [this->resolveVertex(*(it + 1))]; + std::cout << ")-> " << *(it + 1) << " -("; + } + */ } else { std::cout << this->adjacencyMatrix[startIndex][vertexIndex]; } @@ -434,9 +447,22 @@ std::string Graph::getNearestNeighbour(const std::string &vertex, visited[nearestNeighbour] = true; // mark the nearestVertex as visited std::stringstream s; - std::cout << " -("; + std::cout << " -(" << std::flush; if (distNearest == INT_MAX) { - std::cout << "INVALID EDGE"; + // std::cout << "INVALID EDGE"; + // std::cout << "INVALID123 EDGE"; + std::vector> mst = MST(vertex); + std::vector path = + performDijkstraPath(vertex, this->vertices[nearestNeighbour], mst); + for (auto it = path.begin(); it != path.end() - 2; it++) { + std::cout << this->adjacencyMatrix[this->resolveVertex(*(it))] + [this->resolveVertex(*(it + 1))] + << std::flush; + std::cout << ")-> " << *(it + 1) << " -(" << std::flush; + } + std::cout << this->adjacencyMatrix[this->resolveVertex(*(path.end() - 2))] + [this->resolveVertex(*(path.end() - 1))] + << std::flush; } else { std::cout << distNearest; } @@ -454,11 +480,14 @@ std::vector> Graph::MST(const std::string &vertex) { 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]; + /* + int neighbourIndex = + this->resolveVertex(this->getNearestNeighbour(vertex, visited)); + mst[vertexIndex][neighbourIndex] = + this->adjacencyMatrix[vertexIndex][neighbourIndex]; + mst[neighbourIndex][vertexIndex] = + this->adjacencyMatrix[neighbourIndex][vertexIndex]; + */ while (std::count(visited.begin(), visited.end(), false) > 0) { int leastweight = INT_MAX; @@ -466,22 +495,84 @@ std::vector> Graph::MST(const std::string &vertex) { std::string lastVisited; for (int i = 0; i < this->vertices.size(); i++) { if (visited[i]) { - std::vector theNeighbours = this->neighbours(vertex); + std::vector theNeighbours = + this->neighbours(this->vertices[i]); for (auto it = theNeighbours.begin(); it != theNeighbours.end(); it++) { - if (!visited[resolveVertex(*it)] && - this->adjacencyMatrix[this->resolveVertex(vertex)][i] < + if (!visited[this->resolveVertex(*it)] && + this->adjacencyMatrix[i][this->resolveVertex(*it)] < leastweight) { - leastweight = this->adjacencyMatrix[this->resolveVertex(vertex)][i]; + leastweight = this->adjacencyMatrix[i][this->resolveVertex(*it)]; leastVertex = *it; lastVisited = vertices[i]; } } } } - visited[this->resolveVertex(lastVisited)] = true; + visited[this->resolveVertex(leastVertex)] = true; mst[this->resolveVertex(lastVisited)][this->resolveVertex(leastVertex)] = this->adjacencyMatrix[this->resolveVertex(lastVisited)] [this->resolveVertex(leastVertex)]; + mst[this->resolveVertex(leastVertex)][this->resolveVertex(lastVisited)] = + this->adjacencyMatrix[this->resolveVertex(leastVertex)] + [this->resolveVertex(lastVisited)]; } return mst; } + +std::vector +Graph::performDijkstraPath(std::string sourceVertex, std::string targetVertex, + std::vector> mst) { + int dist[this->vertices.size()]; + bool sptSet[vertices.size()]; + int parent[vertices.size()]; + for (int i = 0; i < vertices.size(); i++) { + parent[i] = -1; + } + + for (int i = 0; i < this->vertices.size(); i++) { + dist[i] = INT_MAX; + sptSet[i] = false; + } + dist[resolveVertex(sourceVertex)] = 0; + for (int count = 0; count < this->vertices.size() - 1; count++) { + int m = minDistance(dist, sptSet); + sptSet[m] = true; + for (int v = 0; v < vertices.size(); v++) { + if (!sptSet[v] && mst[m][v] && dist[m] != INT_MAX && + dist[m] + mst[m][v] < dist[v]) { + dist[v] = dist[m] + mst[m][v]; + parent[v] = m; + } + } + if (sptSet[resolveVertex(targetVertex)]) { + break; + } + } + std::vector path; + std::string previous = targetVertex; + while (previous != sourceVertex) { + path.push_back(previous); + previous = vertices[parent[this->resolveVertex(previous)]]; + } + path.push_back(sourceVertex); + /* + for (int i = 0; i < vertices.size(); i++) { + if (parent[i] == -1) { + continue; + } + path.push_back(this->vertices[i]); + } + */ + std::reverse(path.begin(), path.end()); + return path; +} + +int Graph::minDistance(int dist[], bool sptSet[]) { + int min = INT_MAX, min_index; + + for (int v = 0; v < this->vertices.size(); v++) + if (sptSet[v] == false && dist[v] <= min) + min = dist[v], min_index = v; + + return min_index; +} \ No newline at end of file diff --git a/Uebung 5/Uebung5_1/graph.h b/Uebung 5/Uebung5_1/graph.h index 63eec2f..5afa74a 100644 --- a/Uebung 5/Uebung5_1/graph.h +++ b/Uebung 5/Uebung5_1/graph.h @@ -74,4 +74,8 @@ public: void letTheSalesmanTravel(const std::string &vertex); std::string getNearestNeighbour(const std::string &vertex, std::vector &visited); + std::vector + performDijkstraPath(std::string sourceVertex, std::string targetVertex, + std::vector> mst); + int minDistance(int dist[], bool sptSet[]); }; \ No newline at end of file