【问题标题】:Floyd Warshall Algorithm and Grid GraphsFloyd Warshall 算法和网格图
【发布时间】:2018-11-11 00:46:05
【问题描述】:

使用在 wikipedia 上找到的伪代码在邻接表表示上实现 floyd warshall 算法,创建了以下代码。该图是一个网格,所以如果它是一个 3 x 3 网格,则顶点 0 有两条边,顶点 1 有 3,而顶点 2 有两条,依此类推。

self-> V = 图中的顶点数!!

void floyd(Graph *self, int** dist, int** next)
{
    int i, j, k;
    EdgeNodePtr current;

    current =  malloc(sizeof(current));

    for (i = 0; i < self->V; i++)
    {
        for (j = 0; j < self->V; j++) {
            dist[i][j] = INT_MAX; // Sets all minimun distances to infintiy
            next[i][j] = -1; // Sets all next vertex to a non existant vertex
        }
    }

    for (i = 0; i < self->V; i++)
    {
        for (j = 0; j < self->V; j++)
        {
            current = self->edges[i].head;

            while (current != NULL) // Cycles through all the edges in edgelist
            {
                if (current->edge.to_vertex == j) // If an edge to the correct vertex is found then adds distance
                {
                    dist[i][j] = current->edge.weight;
                    next[i][j] = j; // Updates next node
                }
                current =  current->next;
            }

        }
    }

    PRINT

    // Standard implemnation of floyds algorithm
    for (k = 0; k < self->V; k++)
    {
        for (i = 0; i < self->V; i++)
        {
            for (j = 0; j < self->V; j++)
            {
                if (dist[i][j] > dist[i][k] + dist[k][j])
                {
                    dist[i][j] = dist[i][k] + dist[k][j];
                    next[i][j] = next[i][k];
                }
            }
        }
    }
 PRINT
}

发生的情况是所有边缘都正确插入到距离数组中,通过简单的打印检查。算法运行时会遇到问题,它将所有距离转换为 INT_MINS 或类似数字。虽然没有实际计算距离。

我相信网格的末端距离图应该在数组中填充所有可能的距离,除了从顶点到自身的距离为无穷大。

打印列表图的输出图片,上面写着 PRINT

【问题讨论】:

    标签: c algorithm graph floyd-warshall


    【解决方案1】:

    您需要小心 int 溢出。 Wikipedia pseudocode(在这个答案的底部)使用“infinity”,其中“infinity +(任何有限的东西)=infinity”。但是,当您使用INT_MAX 表示由于溢出而导致的“无穷大”时,情况并非如此。尝试将您的 if 语句条件更改为:

    if (dist[i][k] != INT_MAX &&
             dist[k][j] != INT_MAX &&
             dist[i][j] > dist[i][k] + dist[k][j]) {
    

    这将避免 int 溢出向INT_MAX 添加正距离。

    维基百科伪代码:

    1 let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)
    2 for each edge (u,v)
    3    dist[u][v] ← w(u,v)  // the weight of the edge (u,v)
    4 for each vertex v
    5    dist[v][v] ← 0
    6 for k from 1 to |V|
    7    for i from 1 to |V|
    8       for j from 1 to |V|
    9          if dist[i][j] > dist[i][k] + dist[k][j] 
    10             dist[i][j] ← dist[i][k] + dist[k][j]
    11         end if
    

    【讨论】:

    • 当然不是,在路径重建的伪代码中进一步向下它不会这样做。
    • 所以如果我在行中添加“if (i == j) dist[i][j] = 0;”当我设置数组值时?没什么区别
    • @Secernere 看看我的编辑,这对你来说可能是一个更好的答案
    • 现在可以使用了,谢谢。所以据我了解,它增加了循环到最低 INT 值的 INT_MAX?
    • @Secernere 是的,标准的 int 溢出——dist[i][k] 或 dist[k][j] 是 INT_MAX,但 INT_MAX +(小而正)导致的结果非常接近 INT_MIN,小于 @ 987654328@
    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2011-02-10
    • 2014-06-28
    • 1970-01-01
    • 2017-09-12
    • 2012-06-02
    相关资源
    最近更新 更多