This commit is contained in:
Samuel Oberhofer 2022-06-24 12:23:11 +02:00
parent 57cde52b27
commit 2dbd827031
2 changed files with 111 additions and 16 deletions

View File

@ -173,8 +173,10 @@ std::vector<std::string> 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
@ -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<std::string> 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<std::vector<int>> mst = MST(vertex);
std::vector<std::string> 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<std::vector<int>> 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];
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<std::vector<int>> Graph::MST(const std::string &vertex) {
std::string lastVisited;
for (int i = 0; i < this->vertices.size(); i++) {
if (visited[i]) {
std::vector<std::string> theNeighbours = this->neighbours(vertex);
std::vector<std::string> 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<std::string>
Graph::performDijkstraPath(std::string sourceVertex, std::string targetVertex,
std::vector<std::vector<int>> 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<std::string> 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;
}

View File

@ -74,4 +74,8 @@ public:
void letTheSalesmanTravel(const std::string &vertex);
std::string getNearestNeighbour(const std::string &vertex,
std::vector<bool> &visited);
std::vector<std::string>
performDijkstraPath(std::string sourceVertex, std::string targetVertex,
std::vector<std::vector<int>> mst);
int minDistance(int dist[], bool sptSet[]);
};