修改Dijkstra算法打印最短路径中的节点
我想知道如何修改这个函数来保存节点的最终最短路径。这是我的教科书的内容,稍加修改。
template <class vType, int size>
void weightedGraphType<vType, size>::shortestPath(vType vertex) {
int i, j;
double minWeight;
for (j = 0; j < gSize; j++) {
smallestWeight[j] = weights[vertex][j];
}
bool weightFound[size];
for (j = 0; j < gSize; j++) {
weightFound[j] = false;
}
for (i = 0; i < gSize; i++) {
int v;
cout << vertex << " to " << i << endl;
minWeight = INFINITY;
for (j = 0; j < gSize; j++) {
if (!weightFound[j]) {
if (smallestWeight[j] < minWeight) {
v = j;
minWeight = smallestWeight[v];
}
}
}
weightFound[v] = true;
for (j = 0; j < gSize; j++) {
if (!weightFound[j]) {
if (minWeight + weights[v][j] < smallestWeight[j]) {
smallestWeight[j] = minWeight + weights[v][j];
}
}
}
} //end for
} //end shortestPath
I'm wondering how I can modify this function to save the final shortest path of nodes. This is from my textbook with minor modificatons.
template <class vType, int size>
void weightedGraphType<vType, size>::shortestPath(vType vertex) {
int i, j;
double minWeight;
for (j = 0; j < gSize; j++) {
smallestWeight[j] = weights[vertex][j];
}
bool weightFound[size];
for (j = 0; j < gSize; j++) {
weightFound[j] = false;
}
for (i = 0; i < gSize; i++) {
int v;
cout << vertex << " to " << i << endl;
minWeight = INFINITY;
for (j = 0; j < gSize; j++) {
if (!weightFound[j]) {
if (smallestWeight[j] < minWeight) {
v = j;
minWeight = smallestWeight[v];
}
}
}
weightFound[v] = true;
for (j = 0; j < gSize; j++) {
if (!weightFound[j]) {
if (minWeight + weights[v][j] < smallestWeight[j]) {
smallestWeight[j] = minWeight + weights[v][j];
}
}
}
} //end for
} //end shortestPath
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一个提示:对于每个节点,您知道到达该节点的最小权重。您还可以在到达此节点之前知道“到达此节点的最短路径”来自哪里。
Here's a hint: for each node, you know the smallest weight you have found to reach it. You also could know where that "shortest path to reach this node" came from right before you hit this node.
创建数组来记住目标节点的前驱,然后回溯。
这是修改后的 dijkstra 的完整实现
Create array to remember the predecessor of destination node and then track back.
Here is the full implementation of modified dijkstra
做到这一点的一种方法是引入一个额外的循环,在所有节点上迭代算法,并且使用距离数组,您可以存储“通过节点”元素。
一旦计算出从每个节点到每个其他节点的最短路径,您所要做的就是遵循您存储的“通过节点”值。
顺便说一句,就效率而言,这个算法很糟糕,它是 O(n^3)。
one way to do this would be to introduce one extra loop that iterates the algorithm over all the nodes, and with distance array you can store the "via node" element.
once you have shortest path calculated from each node to every other node, all you have to do is to follow the "via node" value that you have stored.
btw, in terms of efficiency, this algorithm sucks, it's O(n^3).