简体   繁体   English

Floyd的最短路径算法C ++

[英]Floyd's Shortest Path Algorithm C++

I have implemented a function for Floyd's algorithm in C++ for weighted digraphs that works correctly except that, when I generate a path matrix that gives the next node when trying to reach a destination, it puts the vertex immediately before the destination instead of the next node from the source in the matrix. 我已经在C ++中为Floyd的算法实现了一个函数,用于正确工作的加权有向图,除了当我生成一个在尝试到达目的地时给出下一个节点的路径矩阵时,它将顶点放在目标而不是下一个节点之前来自矩阵中的来源。 The distance matrix (dist) comes out correctly, and if there is at most one node between the source and destination then the whole path matrix is correct. 距离矩阵(dist)正确输出,如果源和目的地之间最多有一个节点,那么整个路径矩阵是正确的。 So if there are long shortest paths from vertex i to j, then path[i][j] should equal ak value connected to i but instead its ak value connected to j and I cannot figure out why. 因此,如果从顶点i到j有长的最短路径,那么path [i] [j]应该等于连接到i的ak值,而是它连接到j的ak值,而我无法找出原因。 The function is shown below. 功能如下所示。

void floyd(const Graph<City>& g, double**& dist, int**& path)
{
    int n = g.size();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            path[i][j]=0;
            if (i==j)
                dist[i][j]=0;
            else if (!g.isEdge(i, j))
                dist[i][j]=INFINITY;
            else
                dist[i][j]=g.retrieveEdge(i, j);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            for (int k = 1; k <= n; k++)
            {
                if ((dist[i][k]!=INFINITY) && (dist[k][j]!=INFINITY) && k!=i && k!=j && i!=j)
                {
                    if ((dist[i][j]) > (dist[i][k]+dist[k][j]))
                    {
                        path[i][j]=k;
                        dist[i][j]=dist[i][k]+dist[k][j];
                    }
                }
            }
        }
    }
}

So if there are long shortest paths from vertex i to j, then path[i][j] should equal ak value connected to i but instead its ak value connected to j and I cannot figure out why. 因此,如果从顶点i到j有长的最短路径,那么path [i] [j]应该等于连接到i的ak值,而是它连接到j的ak值,而我无法找出原因。

Unfortunately no to both. 不幸的是两者都没有。 There is nothing in your implementation that guarantees that path[i][j] should equal a k value that comes immediately after i , and your observation that path[i][j] is currently a k value that comes immediately before j is also incorrect. 您的实现中没有任何内容可以保证path[i][j]应该等于紧跟在i之后的k值,并且您观察到path[i][j]当前是j之前的k值也是不正确。 (Please try a few more samples to verify the second point.) (请再试几个样本来验证第二点。)

The only thing that is guaranteed by your implementation is that the vertex path[i][j] == k lies somewhere in the shortest path from vertex i to vertex j . 您的实现保证唯一的事情是顶点path[i][j] == k位于从顶点i到顶点j的最短路径中的某处

Thus you can retrieve the path recursively by doing: 因此,您可以通过执行以下操作来递归检索路径:

get_path(i, j):
    k = path[i][j]
    get_path(i, k) + k + get_path(k, j)

Having clarified that, there does exist a method where you can store path[i][j] such that it is the vertex that comes immediately after i . 澄清一下, 确实存在一种方法 ,你可以存储path[i][j] ,使得它是紧跟在i之后的顶点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用具有权重限制的 Floyd Warshall 算法(C++)计算最短路径? - Is it possible to calculate the shortest path using Floyd Warshall's algorithm with a weight limit (C++)? 弗洛伊德的算法(最短路径)问题-C ++ - Floyd's Algorithm (Shortest Paths) Issue - C++ 使用Floyd算法找到最短路径 - Find the shortest path with Floyd algorithm 使用C ++和STL实现Dijkstra的最短路径算法 - Implementing Dijkstra's shortest path algorithm using C++ and STL 我的C ++ Floyd的所有对最短路径程序有什么问题? - What is wrong with my C++ Floyd's all-pairs shortest paths program? 用 C++ 最短路径算法有两个数字。 (c++) - with C++ shortest path algorithm with two numbers. (c++) 有没有一种方法可以使用 Floyd-Warshall 算法给出最短路径,其中存在负权重循环而不允许重叠边缘? - Is there a way that gives shortest path using Floyd-Warshall's algorithm where negative weight cycle exists whereas overlapped edges are not allowed? Dijkstra 的最短路径算法实现出错(使用 C++ STL) - Error in Dijkstra's shortest path algorithm implementation (using C++ STL) 修改Prim的实现以记录最短路径的权重c ++ - Modifying an Implementation of Prim's to record weight of shortest path c++ Dijkstra最短路径算法问题 - Dijkstra's Shortest Path Algorithm issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM