Dijkstra
This commit is contained in:
parent
cb566580bd
commit
384992373a
|
|
@ -223,22 +223,69 @@ int ExtendedGraph::resolveVertex(const std::string &vertexName) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ExtendedGraph::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;
|
||||||
|
}
|
||||||
|
|
||||||
void ExtendedGraph::performDijkstraPath(std::string sourceVertex,
|
void ExtendedGraph::performDijkstraPath(std::string sourceVertex,
|
||||||
std::string targetVertex) {
|
std::string targetVertex) {
|
||||||
|
int dist[this->vertices.size()];
|
||||||
int distances[this->vertices.size()] = {INT_MAX};
|
bool sptSet[vertices.size()];
|
||||||
bool visited[this->vertices.size()] = {false};
|
int parent[vertices.size()];
|
||||||
distances[this->resolveVertex(sourceVer)] = 0;
|
for (int i = 0; i < vertices.size(); i++) {
|
||||||
|
parent[i] = -1;
|
||||||
visited[this->resolveVertex(sourceVer)] = true;
|
}
|
||||||
|
|
||||||
for (int i = 0; i < this->vertices.size(); i++) {
|
for (int i = 0; i < this->vertices.size(); i++) {
|
||||||
|
dist[i] = INT_MAX;
|
||||||
|
sptSet[i] = false;
|
||||||
}
|
}
|
||||||
|
dist[resolveVertex(sourceVertex)] = 0;
|
||||||
std::vector<std::pair<bool, int>> data;
|
for (int count = 0; count < this->vertices.size() - 1; count++) {
|
||||||
std::string currentVertex = sourceVertex;
|
int m = minDistance(dist, sptSet);
|
||||||
// std::map<std::string, int> sptSet;
|
sptSet[m] = true;
|
||||||
|
for (int v = 0; v < vertices.size(); v++) {
|
||||||
for (const std::string &neighbour : this->neighbours(currentVertex)) {
|
if (!sptSet[v] && this->adjacencyMatrix[m][v] && dist[m] != INT_MAX &&
|
||||||
|
dist[m] + this->adjacencyMatrix[m][v] < dist[v]) {
|
||||||
|
dist[v] = dist[m] + this->adjacencyMatrix[m][v];
|
||||||
|
parent[v] = m;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (sptSet[resolveVertex(targetVertex)]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// int src = 0;
|
||||||
|
// std::cout << "Vertex\t Distance\tPath";
|
||||||
|
/*
|
||||||
|
for (int i = 1; i < this->vertices.size(); i++) {
|
||||||
|
printf("\n%d -> %d \t\t %d\t\t%d ", src, i, dist[i], src);
|
||||||
|
printPath(parent, i);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
printf("\n%d -> %d \t\t %d\t\t%d ", resolveVertex(sourceVertex),
|
||||||
|
resolveVertex(targetVertex), dist[resolveVertex(targetVertex)],
|
||||||
|
resolveVertex(sourceVertex));
|
||||||
|
*/
|
||||||
|
std::cout << sourceVertex << " ";
|
||||||
|
int sumPath = 0;
|
||||||
|
printPath(parent, resolveVertex(targetVertex), dist, sumPath);
|
||||||
|
std::cout << "; Gesamter Zollbetrag: " << sumPath << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExtendedGraph::printPath(int parent[], int j, int dist[], int &sum) {
|
||||||
|
// Base Case : If j is source
|
||||||
|
if (parent[j] == -1)
|
||||||
|
return;
|
||||||
|
printPath(parent, parent[j], dist, sum);
|
||||||
|
std::cout << " -(" << this->adjacencyMatrix[parent[j]][j] << ")-> "
|
||||||
|
<< this->vertices[j] << std::flush;
|
||||||
|
sum += adjacencyMatrix[parent[j]][j];
|
||||||
}
|
}
|
||||||
|
|
@ -15,6 +15,8 @@ class ExtendedGraph {
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> vertices; // vector connecting vertices with
|
std::vector<std::string> vertices; // vector connecting vertices with
|
||||||
// indicies; needed for name resolution
|
// indicies; needed for name resolution
|
||||||
|
int minDistance(int dist[], bool sptSet[]);
|
||||||
|
void printPath(int parent[], int j, int dist[], int &sum);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExtendedGraph(); // default constructor
|
ExtendedGraph(); // default constructor
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,13 @@ int main() {
|
||||||
{"Rom", "Moskau", 3}, {"Rom", "Lyon", 4},
|
{"Rom", "Moskau", 3}, {"Rom", "Lyon", 4},
|
||||||
{"Amsterdam", "Paris", 1}, {"Amsterdam", "Moskau", 1},
|
{"Amsterdam", "Paris", 1}, {"Amsterdam", "Moskau", 1},
|
||||||
{"Moskau", "Rom", 3}, {"Moskau", "Paris", 1},
|
{"Moskau", "Rom", 3}, {"Moskau", "Paris", 1},
|
||||||
{"Moskau", "Berlin", 2}, {"Berlin", "Moskau", 2},
|
{"Moskau", "Amsterdam", 1}, {"Moskau", "Berlin", 2},
|
||||||
{"Berlin", "Lyon", 2}, {"Lyon", "Madrid", 3},
|
{"Berlin", "Moskau", 2}, {"Berlin", "Lyon", 2},
|
||||||
{"Lyon", "Rom", 4}, {"Lyon", "Paris", 2},
|
{"Lyon", "Madrid", 3}, {"Lyon", "Rom", 4},
|
||||||
{"Lyon", "Berlin", 2}};
|
{"Lyon", "Paris", 2}, {"Lyon", "Berlin", 2}};
|
||||||
ExtendedGraph g(vertices, edges);
|
ExtendedGraph g(vertices, edges);
|
||||||
g.printGraph();
|
// g.printGraph();
|
||||||
|
g.performDijkstraPath("Amsterdam", "Mailand");
|
||||||
|
|
||||||
/* insertVertices */
|
/* insertVertices */
|
||||||
// g.insertVertex("D");
|
// g.insertVertex("D");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue