我正在尝试为加权有向图实现Floyd Warshall图算法,但无法使其工作。
仅为后继的权重,否则为1和0。
实现看起来像这样(尽管我从某个地方采取了实现)。
INF = 999999999
def printSolution(distGraph):
string = "inf"
nodes =distGraph.keys()
for n in nodes:
print "\t%6s"%(n),
print " "
for i in nodes:
print"%s"%(i),
for j in nodes:
if distGraph[i][j] == INF:
print "%10s"%(string),
else:
print "%10s"%(distGraph[i][j]),
print" "
def floydWarshall(graph):
nodes = graph.keys()
distance = {}
for n in nodes:
distance[n] = {}
for k in nodes:
distance[n][k] = graph[n][k]
for k in nodes:
for i in nodes:
for j in nodes:
distance[i][j] = min (distance[i][j], distance[i][k] + distance[k][j])
printSolution(distance)
if __name__ == '__main__':
graph = { u'node_10': { u'node_10': 0,
u'node_4': 1,
u'node_5': 0,
u'node_6': 0,
u'node_7': 0,
u'node_8': 0,
u'node_9': 0},
u'node_4': { u'node_10': 0,
u'node_4': 1,
u'node_5': 1,
u'node_6': 0,
u'node_7': 0,
u'node_8': 0,
u'node_9': 0},
u'node_5': { u'node_10': 0,
u'node_4': 0,
u'node_5': 0,
u'node_6': 1,
u'node_7': 0,
u'node_8': 1,
u'node_9': 0},
u'node_6': { u'node_10': 0,
u'node_4': 0,
u'node_5': 0,
u'node_6': 1,
u'node_7': 1,
u'node_8': 0,
u'node_9': 0},
u'node_7': { u'node_10': 0,
u'node_4': 0,
u'node_5': 0,
u'node_6': 0,
u'node_7': 1,
u'node_8': 1,
u'node_9': 0},
u'node_8': { u'node_10': 0,
u'node_4': 0,
u'node_5': 0,
u'node_6': 0,
u'node_7': 0,
u'node_8': 0,
u'node_9': 1},
u'node_9': { u'node_10': 1,
u'node_4': 0,
u'node_5': 1,
u'node_6': 0,
u'node_7': 0,
u'node_8': 0,
u'node_9': 0}}
floydwarshall (graph)
答案 0 :(得分:0)
我犯了一个错误,我不应该放0重量。在指定INF而不是0之后,它就像魅力一样。