您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python Dijkstra k最短路径

Python Dijkstra k最短路径

毫无疑问,图中将存在大量最短的路径。因此,很难在令人满意的时间复杂度内生成所有最短路径。但是我可以给你一个简单的方法,它可以根据需要获得尽可能多的最短路径。

def find_one_shortest_path(graph, Now, target, path_info):
    if Now == target:
        print path_info
        return
    for each neighbor_point of graph[Now]:
        path_info.append(neighbor_point) 
        find_one_shortest_path(graph, neighbor_point, target, path_info) #recursion
        path_info.pop(-1) #backtracking

def all_shortest_paths(graph, starting_point, ending_point):
    disS = [] # shortest path from S
    disT = [] # shortest path from T
    new_graph = []
    disS = Dijkstra(graph, starting_point)
    disT = Dijkstra(graph, endinng_point)
    for each edge<a, b> in graph:
        if disS[a] + w<a, b> + disT[b] == disS[ending_point]:
            new_graph.add(<a, b>)
    find_one_shortest_path(new_graph, starting_point, ending_point, [])
python 2022/1/1 18:34:58 有276人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶