bellman_ford算法 python实现

来源:互联网 发布:saas java开源架构 编辑:程序博客网 时间:2024/06/10 00:15
def bellman_ford( graph, source ):        distance = {}    parent = {}        for node in graph:        distance[node] = float( 'Inf' )        parent[node] = None    distance[source] = 0    for i in range( len( graph ) - 1 ):        for from_node in graph:            for to_node in graph[from_node]:                if distance[to_node] > graph[from_node][to_node] + distance[from_node]:                    distance[to_node] = graph[from_node][to_node] + distance[from_node]                    parent[to_node] = from_node    for from_node in graph:        for to_node in graph[from_node]:            if distance[to_node] > distance[from_node] + graph[from_node][to_node]:                return None, None    return distance, parentdef test():    graph = {        'a': {'b': -1, 'c':  4},        'b': {'c':  3, 'd':  2, 'e':  2},        'c': {},        'd': {'b':  1, 'c':  5},        'e': {'d': -3}    }    distance, parent = bellman_ford( graph, 'a' )    print distance    print parentif __name__ == '__main__':    test()

0 0