算法熟记-Floyd-Warshall

1. 简述

    解决任意两点间的最短路径的一种算法。

2. 复杂度

    T=O(V^3) ,S=O(V^2),使用邻接矩阵存储。

3. 伪代码
    需要注意的是i,j,k三个变量的顺序。

// dist(i,j) 为从节点i到节点j的最短距离
For i←1 to n do
   For j←
1 to n do
      dist(i,j) 
= weight(i,j)  
For k←
1 to n do // k为“媒介节点”
   For i←1 to n do
      For j←
1 to n do
         
if (dist(i,k) + dist(k,j) < dist(i,j)) then // 是否是更短的路径?
            dist(i,j) = dist(i,k) + dist(k,j)

 4. 参考资料

     维基百科

posted @ 2011-05-27 09:29  xiaodongrush  阅读(462)  评论(0编辑  收藏  举报