基本思想:在现有路径代价矩阵中依次计算增加途径节点k所需消耗的新的路径代价,并刷新路径代价矩阵。
算法详细讲解可以参考《坐在马桶上看算法》
# coding: utf-8
_INF = float('inf')
e = [\
[0, 2, 6, 4],\
[_INF, 0, 3, _INF],\
[7, _INF, 0, 1],\
[5, _INF, 12, 0]\
]
for k in range(4):
for i in range(4):
for j in range(4):
if e[i][j] > (e[i][k] + e[k][j]):
e[i][j] = e[i][k] + e[k][j]
print e