Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Floyd-Warshall算法与网格图_C_Algorithm_Graph_Floyd Warshall - Fatal编程技术网

C Floyd-Warshall算法与网格图

C Floyd-Warshall算法与网格图,c,algorithm,graph,floyd-warshall,C,Algorithm,Graph,Floyd Warshall,使用维基百科上的伪代码在邻接列表表示上实现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));

使用维基百科上的伪代码在邻接列表表示上实现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
}
void floyd(图*self,int**dist,int**next)
{
int i,j,k;
边缘电流;
当前=malloc(sizeof(当前));
对于(i=0;iV;i++)
{
对于(j=0;jV;j++){
dist[i][j]=INT_MAX;//将所有最小距离设置为infinity
next[i][j]=-1;//将所有下一个顶点设置为不存在的顶点
}
}
对于(i=0;iV;i++)
{
对于(j=0;jV;j++)
{
当前=自身->边缘[i]。头部;
while(current!=NULL)//循环遍历edgelist中的所有边
{
if(current->edge.to_vertex==j)//如果找到到正确顶点的边,则添加距离
{
dist[i][j]=当前->边缘权重;
next[i][j]=j;//更新下一个节点
}
当前=当前->下一步;
}
}
}
印刷品
//floyds算法的标准实现
对于(k=0;kV;k++)
{
对于(i=0;iV;i++)
{
对于(j=0;jV;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];
}
}
}
}
印刷品
}
所发生的情况是,所有边都正确插入到距离数组中,并通过简单打印进行检查。当算法运行时会遇到这个问题,它会将所有距离转换为整数或类似的数字。而不是实际计算距离

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

打印列表图形输出的图片,其中显示打印
您需要小心int溢出。(在这个答案的底部)使用“infinity”,其中“infinity+(anything finite)=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\u MAX
添加正距离

维基百科伪代码:


当然不是,在路径重建的伪代码中,它没有这样做。因此,如果我在设置数组值时在“if(I==j)dist[I][j]=0”行中添加?没有任何意义difference@Secernere请看我的编辑,这可能是youWorks现在更好的答案,谢谢。因此,据我所知,它是在整数最大值的基础上加上整数最大值,而整数最大值循环到了最低的整数值?@Secernere是的,标准的整数溢出——或者
dist[i][k]
或者
dist[k][j]
是整数最大值,但是整数最大值+(一个小而正的值)会产生一个非常接近整数最小值的结果,它小于
dist i][j]
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