Fix
This commit is contained in:
parent
57cde52b27
commit
2dbd827031
|
|
@ -173,9 +173,11 @@ std::vector<std::string> Graph::neighbours(const std::string &vertex) {
|
||||||
for (int i = 0; i < this->adjacencyMatrix[indexVertex].size();
|
for (int i = 0; i < this->adjacencyMatrix[indexVertex].size();
|
||||||
i++) { /* Loops through all entries of the subvector of
|
i++) { /* Loops through all entries of the subvector of
|
||||||
adjacencyMatrix[indexVertex] */
|
adjacencyMatrix[indexVertex] */
|
||||||
if (this->adjacencyMatrix[indexVertex][i] !=
|
if (this->adjacencyMatrix[indexVertex][i] != 0 &&
|
||||||
0) { /* and checks if the entry at position [indexVertex][i] is not 0.
|
this->adjacencyMatrix[indexVertex][i] <
|
||||||
*/
|
INT_MAX) { /* and checks if the entry at position [indexVertex][i]
|
||||||
|
* is not 0.
|
||||||
|
*/
|
||||||
resVector.push_back(
|
resVector.push_back(
|
||||||
this->vertices[i]); /* If the condition is fulfilled, add the vertex
|
this->vertices[i]); /* If the condition is fulfilled, add the vertex
|
||||||
(name) to the result vector resVector */
|
(name) to the result vector resVector */
|
||||||
|
|
@ -371,7 +373,18 @@ void Graph::_letTheSalesmanTravel(const std::string &vertex,
|
||||||
}
|
}
|
||||||
std::cout << " -(";
|
std::cout << " -(";
|
||||||
if (this->adjacencyMatrix[startIndex][vertexIndex] == INT_MAX) {
|
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 {
|
} else {
|
||||||
std::cout << this->adjacencyMatrix[startIndex][vertexIndex];
|
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
|
visited[nearestNeighbour] = true; // mark the nearestVertex as visited
|
||||||
|
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
std::cout << " -(";
|
std::cout << " -(" << std::flush;
|
||||||
if (distNearest == INT_MAX) {
|
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 {
|
} else {
|
||||||
std::cout << distNearest;
|
std::cout << distNearest;
|
||||||
}
|
}
|
||||||
|
|
@ -454,11 +480,14 @@ std::vector<std::vector<int>> Graph::MST(const std::string &vertex) {
|
||||||
visited.resize(this->vertices.size(), false);
|
visited.resize(this->vertices.size(), false);
|
||||||
int vertexIndex = this->resolveVertex(vertex);
|
int vertexIndex = this->resolveVertex(vertex);
|
||||||
visited[vertexIndex] = true;
|
visited[vertexIndex] = true;
|
||||||
|
/*
|
||||||
int neighbourIndex =
|
int neighbourIndex =
|
||||||
this->resolveVertex(this->getNearestNeighbour(vertex, visited));
|
this->resolveVertex(this->getNearestNeighbour(vertex, visited));
|
||||||
mst[vertexIndex][neighbourIndex] =
|
mst[vertexIndex][neighbourIndex] =
|
||||||
this->adjacencyMatrix[vertexIndex][neighbourIndex];
|
this->adjacencyMatrix[vertexIndex][neighbourIndex];
|
||||||
|
mst[neighbourIndex][vertexIndex] =
|
||||||
|
this->adjacencyMatrix[neighbourIndex][vertexIndex];
|
||||||
|
*/
|
||||||
while (std::count(visited.begin(), visited.end(), false) > 0) {
|
while (std::count(visited.begin(), visited.end(), false) > 0) {
|
||||||
|
|
||||||
int leastweight = INT_MAX;
|
int leastweight = INT_MAX;
|
||||||
|
|
@ -466,22 +495,84 @@ std::vector<std::vector<int>> Graph::MST(const std::string &vertex) {
|
||||||
std::string lastVisited;
|
std::string lastVisited;
|
||||||
for (int i = 0; i < this->vertices.size(); i++) {
|
for (int i = 0; i < this->vertices.size(); i++) {
|
||||||
if (visited[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++) {
|
for (auto it = theNeighbours.begin(); it != theNeighbours.end(); it++) {
|
||||||
if (!visited[resolveVertex(*it)] &&
|
if (!visited[this->resolveVertex(*it)] &&
|
||||||
this->adjacencyMatrix[this->resolveVertex(vertex)][i] <
|
this->adjacencyMatrix[i][this->resolveVertex(*it)] <
|
||||||
leastweight) {
|
leastweight) {
|
||||||
leastweight = this->adjacencyMatrix[this->resolveVertex(vertex)][i];
|
leastweight = this->adjacencyMatrix[i][this->resolveVertex(*it)];
|
||||||
leastVertex = *it;
|
leastVertex = *it;
|
||||||
lastVisited = vertices[i];
|
lastVisited = vertices[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
visited[this->resolveVertex(lastVisited)] = true;
|
visited[this->resolveVertex(leastVertex)] = true;
|
||||||
mst[this->resolveVertex(lastVisited)][this->resolveVertex(leastVertex)] =
|
mst[this->resolveVertex(lastVisited)][this->resolveVertex(leastVertex)] =
|
||||||
this->adjacencyMatrix[this->resolveVertex(lastVisited)]
|
this->adjacencyMatrix[this->resolveVertex(lastVisited)]
|
||||||
[this->resolveVertex(leastVertex)];
|
[this->resolveVertex(leastVertex)];
|
||||||
|
mst[this->resolveVertex(leastVertex)][this->resolveVertex(lastVisited)] =
|
||||||
|
this->adjacencyMatrix[this->resolveVertex(leastVertex)]
|
||||||
|
[this->resolveVertex(lastVisited)];
|
||||||
}
|
}
|
||||||
return mst;
|
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;
|
||||||
|
}
|
||||||
|
|
@ -74,4 +74,8 @@ public:
|
||||||
void letTheSalesmanTravel(const std::string &vertex);
|
void letTheSalesmanTravel(const std::string &vertex);
|
||||||
std::string getNearestNeighbour(const std::string &vertex,
|
std::string getNearestNeighbour(const std::string &vertex,
|
||||||
std::vector<bool> &visited);
|
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[]);
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue