弗洛伊德的算法(Floyd’s algorithm )b.) Consider a weighted directed graph G with 5 vertices {v1,v2,v3,v4,v5}.The weights of the edges are shown in the matrix below.Apply Floyd’s algorithm to G to find the shortest path length for all pa

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/02 06:34:24
弗洛伊德的算法(Floyd’s algorithm )b.) Consider a weighted directed graph G with 5 vertices {v1,v2,v3,v4,v5}.The weights of the edges are shown in the matrix below.Apply Floyd’s algorithm to G to find the shortest path length for all pa

弗洛伊德的算法(Floyd’s algorithm )b.) Consider a weighted directed graph G with 5 vertices {v1,v2,v3,v4,v5}.The weights of the edges are shown in the matrix below.Apply Floyd’s algorithm to G to find the shortest path length for all pa
弗洛伊德的算法(Floyd’s algorithm )
b.) Consider a weighted directed graph G with 5 vertices {v1,v2,v3,v4,v5}.The weights of the edges are shown in the matrix below.

Apply Floyd’s algorithm to G to find the shortest path length for all pairs of vertices.[20 marks]


 说下答案, 也说下怎么做的

弗洛伊德的算法(Floyd’s algorithm )b.) Consider a weighted directed graph G with 5 vertices {v1,v2,v3,v4,v5}.The weights of the edges are shown in the matrix below.Apply Floyd’s algorithm to G to find the shortest path length for all pa

假设这个图的weight matrix存在map[5][5]中,

for (int k=0; k<5; k++)
    for (int i=0; i<5; i++)
        for (int j=0; j<5; j++) if (i != j) {
            if (map[i][k] + map[k][j] < map[i][j])
                map[i][j] = map[i][k] + map[k][j];
        }

处理完之后map[i][j]存的就是i,j之间的最短路径长度.

简单的说,当执行完一次最外层循环时,map记录的时i,j之间允许使用中间节点{0, ..., k}的最短路径.