用于无向图的Floyd Warshall算法

时间:2016-02-29 17:49:37

标签: python graph floyd-warshall

我有Floyd Warshall算法的有向图,但我在第24行遇到错误,这是这段代码:     如果newdist< DIST [U] [V]: 任何人都知道我该如何解决它? 我还想将此有向图更改为无向图。我应该在这里改变什么?

def floydwarshall(graph):

# Initialize dist and pred:
# copy graph into dist, but add infinite where there is
# no edge, and 0 in the diagonal
dist = {}
pred = {}
for u in graph:
    dist[u] = {}
    pred[u] = {}
    for v in graph:
        dist[u][v] = 1000
        pred[u][v] = -1
    dist[u][u] = 0
    for neighbor in graph[u]:
        dist[u][neighbor] = graph[u][neighbor]
        pred[u][neighbor] = u

for t in graph:
    # given dist u to v, check if path u - t - v is shorter
    for u in graph:
        for v in graph:
            newdist = dist[u][t] + dist[t][v]
            if newdist < dist[u][v]:
                dist[u][v] = newdist
                pred[u][v] = pred[t][v] # route new path through t

 return dist, pred



graph = {0 : {1:6, 2:8},
     1 : {4:11},
     2 : {3: 9},
     3 : {},
     4 : {5:3},
     5 : {2: 7, 3:4}}

dist, pred = floydwarshall(graph)
print "Predecesors in shortest path:"
for v in pred: print "%s: %s" % (v, pred[v])
print "Shortest distance from each vertex:"
for v in dist: print "%s: %s" % (v, dist[v])





python floydwarshall.py 
Predecesors in shortest path:
0: {0: -1, 1: 0, 2: 0, 3: 2, 4: 1, 5: 4}
1: {0: -1, 1: -1, 2: 5, 3: 5, 4: 1, 5: 4}
2: {0: -1, 1: -1, 2: -1, 3: 2, 4: -1, 5: -1}
3: {0: -1, 1: -1, 2: -1, 3: -1, 4: -1, 5: -1}
4: {0: -1, 1: -1, 2: 5, 3: 5, 4: -1, 5: 4}
5: {0: -1, 1: -1, 2: 5, 3: 5, 4: -1, 5: -1}
Shortest distance from each vertex:
0: {0: 0, 1: 6, 2: 8, 3: 17, 4: 17, 5: 20}
1: {0: 1000, 1: 0, 2: 21, 3: 18, 4: 11, 5: 14}
2: {0: 1000, 1: 1000, 2: 0, 3: 9, 4: 1000, 5: 1000}
3: {0: 1000, 1: 1000, 2: 1000, 3: 0, 4: 1000, 5: 1000}
4: {0: 1000, 1: 1000, 2: 10, 3: 7, 4: 0, 5: 3}
5: {0: 1000, 1: 1000, 2: 7, 3: 4, 4: 1000, 5: 0}

0 个答案:

没有答案