- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图通过计算所有对之间的最短路径,使用 Floyd-Warshall 算法找到源和目标之间的最短路径。
我需要找到最短的路径,而不仅仅是距离。这就是我想要做的:
我将第一个顶点存储在从i到j的最短路径上。每当从 i 到 j 的最短路径更新并且它现在经过 k 时,我将从 i 到 j 的最短路径上的第一个顶点设置为从 i 到 k 的最短路径上的第一个顶点。
/*first[i][j] is the first vertex after i on the shortest path from i to j.
first[i][j] is initially j if there is an edge from i to j and the dist[i][j] is the weight of the edge. Otherwise f[i][j] is -1 and the cost is infinity.
*/
for(k = 0; k < N; ++k){
for(i = 0; i < N; ++i){
for(j = 0; j < N; ++j){
if(dist[i][j] >= dist[i][k]+dist[k][j]){
dist[i][j] = dist[i][k]+dist[k][j];
//When the distance is updated, update first[i][j]
first[i][j] = first[i][k];
}
}
}
}
这个算法的问题是,当我在下图运行这个算法时,这个算法找到的路径是一个无限循环。
这是算法计算的第一个
矩阵:
4 4 4 4 4 4
2 2 2 2 2 2
5 5 5 5 5 5
1 1 1 1 1 1
0 0 0 0 0 0
2 2 2 2 2 2
根据算法,从 0 到任何其他顶点的最短路径上的第一个顶点为 4,但从 4 到任何其他顶点的最短路径上的第一个顶点为 0。
我已阅读 Wikipedia文章和一些关于 SO 的问题,但它们没有太大帮助。
最佳答案
您的 dist
矩阵似乎已正确计算,但您的 first
矩阵添加似乎存在零成本边问题。
查看代码的这个略微修改的 Python 版本,它使用 0.01
作为所有自边和其他 0 成本边的成本。
该代码输出(希望)正确的 dist
和 first
矩阵
[0.01, inf, inf, 0.01, 0.01, inf]
[0.02, 0.01, 0.01, 0.01, 0.03, 0.02]
[0.01, inf, 0.01, 0.02, 0.02, 0.01]
[ inf, inf, inf, 0.01, inf, inf]
[0.01, inf, inf, 0.02, 0.01, inf]
[0.02, inf, 0.01, 0.01, 0.03, 0.01]
和
[ 0, None, None, 3, 4, None]
[ 2, 1, 2, 3, 2, 2]
[ 0, None, 2, 5, 0, 5]
[None, None, None, 3, None, None]
[ 0, None, None, 0, 4, None]
[ 2, None, 2, 3, 2, 5]
关于c++ - 具有路径重建的 Floyd–Warshall 算法找不到路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789126/
我的输入矩阵如下: 0 1 1 0 0 0 1 0 1 0 0 1 0 0 0 0 我的 Warshall 算法代码如下: int V = A.length; for(int k = 0;
我想在这个图问题中重建从源到目标顶点的路径。 如何存储路径,以及在找到从 s 到 d 的最小成本后如何检索它? 请帮我找到一个简单的答案? 例如在这一点上, adjmat[i][j] = Math.m
是否可以使用邻接表对 Floyd Warshall 进行编码?我必须处理文本文件中的一百万个顶点,因此邻接矩阵不是解决方案。任何实现已经可用?请帮忙。 最佳答案 您不能将 Floyd Warshall
我已经在 MySQL 存储过程中实现了 Warshall 的算法。不幸的是,该过程需要很长时间才能完成。我是编写存储过程的初学者,您知道我可以做些什么来让它更快吗? 简要说明:我正在尝试计算邻接表的传
我实现了 Floyd-Warshall 算法。根据他们的矩阵,我可以得到正确的结果,关于两个地方之间的最短路径和他们的距离。我的问题是如何打印从 i 到 j 的最短距离。我做了一些研究,发现了一个类似
这可能是一个糟糕的问题,因为我的代表很低,但我已经研究了几个小时的其他解决方案,我的代码似乎与我遇到的工作解决方案几乎相同。请不要忽略基于低代表的问题。 输出矩阵 d[][] 包含给定顶点对之间最短路
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to tak
假设我有 9 个顶点。所以我有 9x9 解决方案矩阵和 matrix[6,0] = infinity, matrix[6,9]=1, matrix[9,0]=1 现在算法的工作原理如下: for k
This维基百科页面解释了 Floyd Warshall 算法,用于查找图中节点之间的最短路径。维基百科页面使用图像左侧的图表 作为起始图(在 k = 0 时的第一次迭代之前),然后显示剩余的迭代(k
我知道当图中有负权重环时,没有找到最小距离的方法,也就没有最小距离的意义了。我的问题是,如果我们向 Floyd Warshall 算法提供具有负权重循环的图,会发生什么情况?它会在 O(n3) 内无限
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
问题陈述:https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights 代码: import scala.io.StdIn._
我正在为一项作业实现 Floyd-Warshall 算法,但输出矩阵不正确。我已经在网上与其他人仔细检查了我的算法,它看起来和其他人一样。我只是错过了什么吗?感谢您的帮助。 我的输入文件是: 4 0
我想使用 Floyd-warshall 算法找到带权无向图任意两个顶点之间的最大距离。为此,我做了一些改动: 我添加负权重而不是正权重。 然后我找出最短路径。 但它没有给我正确的输出。谁能指出我犯的错
由于 Floyd-Warshall 算法是动态的,这意味着它必须始终提供最佳解决方案,对吗?因此,让我感到困惑的是,在算法的每个部分中,这些最佳解决方案的性质是什么——特别是,我试图理解以下三个问题:
我试图解决 INOI 2014 paper 中的第二个问题IE。 FREETICKET 并使用 Floyd-Warshall 算法计算答案。我的代码似乎在最后的子任务中失败,并且似乎为几个测试用例提供
我正在尝试实现 Warshall 算法来查找邻接矩阵的传递闭包。这就是我的功能: public static int[][] warshall(int A[][]){ int R[][] =
我像那里一样实现算法 http://en.algoritmy.net/article/45708/Floyd-Warshall-algorithm . void Graph::floydWarsha
我正在尝试在 python 3 中实现 Warshall 算法,以创建一个矩阵,每个点之间的距离最短。 这应该是一个简单的实现,我制作了一个矩阵并用每个点之间的距离填充它。 但是,我得到了错误的结果,
我正在编写一个程序,该程序使用 Warshall 算法来查找表示关系的矩阵的传递闭包。这是伪代码中算法的链接:http://people.cs.pitt.edu/~adamlee/courses/cs
我是一名优秀的程序员,十分优秀!