字典表示
G = {1: {1: 0, 2: 1, 3: 12},
2: {2: 0, 3: 9, 4: 3},
3: {3: 0, 5: 5},
4: {3: 4, 4: 0, 5: 13, 6: 15},
5: {5: 0, 6: 4},
6: {6: 0}}
def Dijkstra(G, v0, INF=999):
""" 使用 Dijkstra 算法计算指定点 v0 到图 G 中任意点的最短路径的距离
INF 为设定的无限远距离值
此方法不能解决负权值边的图
"""
book = set()
minv = v0
dis = dict((k, INF) for k in G.keys())
dis[v0] = 0
while len(book) < len(G):
book.add(minv)
for w in G[minv]:
if dis[minv] + G[minv][w] < dis[w]:
dis[w] = dis[minv] + G[minv][w]
new = INF
for v in dis.keys():
if v in book: continue
if dis[v] < new:
new = dis[v]
minv = v
return dis
dis = Dijkstra(G, v0=1)
print(dis.values())
邻接矩阵表示
from collections import defaultdict
from heapq import *
def dijkstra_raw(edges, from_node, to_node):
g = defaultdict(list)
for l, r, c in edges:
g[l].append((c, r))
q, seen = [(0, from_node, ())], set()
while q:
(cost, v1, path) = heappop(q)
if v1 not in seen:
seen.add(v1)
path = (v1, path)
if v1 == to_node:
return cost, path
for c, v2 in g.get(v1, ()):
if v2 not in seen:
heappush(q, (cost + c, v2, path))
return float("inf"), []
def dijkstra(edges, from_node, to_node):
len_shortest_path = -1
ret_path = []
length, path_queue = dijkstra_raw(edges, from_node, to_node)
if len(path_queue) > 0:
len_shortest_path = length
left = path_queue[0]
ret_path.append(left)
right = path_queue[1]
while len(right) > 0:
left = right[0]
ret_path.append(left)
right = right[1]
ret_path.reverse()
return len_shortest_path, ret_path
list_nodes_id = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
M = 99999
M_topo = [
[M, 1, 1, M, 1, M, 1, 1, 1, M, M, M, M, M, M, M, M, M, M, M, M],
[1, M, 1, M, M, 1, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M],
[1, 1, M, 1, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M],
[M, M, 1, M, 1, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M],
[1, M, M, 1, M, M, M, M, M, 1, 1, 1, M, M, M, M, M, M, M, M, M],
[M, 1, M, M, M, M, 1, M, M, M, M, M, M, M, M, M, M, M, M, M, M],
[1, M, M, M, M, 1, M, 1, M, M, M, M, M, M, M, M, M, M, M, M, M],
[1, M, M, M, M, M, 1, M, 1, M, M, M, M, M, M, M, M, M, M, M, M],
[1, M, M, M, M, M, M, 1, M, 1, M, M, 1, M, M, M, M, M, M, M, M],
[M, M, M, M, 1, M, M, M, 1, M, M, 1, M, M, M, M, M, M, M, M, M],
[M, M, M, M, 1, M, M, M, M, M, M, 1, M, 1, M, M, M, M, M, M, M],
[M, M, M, M, 1, M, M, M, M, 1, 1, M, M, 1, 1, M, M, M, M, M, M],
[M, M, M, M, M, M, M, M, 1, M, M, M, M, M, 1, M, M, M, M, M, M],
[M, M, M, M, M, M, M, M, M, M, 1, 1, M, M, 1, M, M, 1, 1, M, M],
[M, M, M, M, M, M, M, M, M, M, M, 1, 1, 1, M, 1, 1, M, M, M, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M, 1, M, 1, 1, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1, 1, M, M, M, M, 1],
[M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M, M, M, M, 1, M, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M, 1, M, 1, M, 1, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M, M, 1, M, 1],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M, M, 1, M]
]
edges = []
for i in range(len(M_topo)):
for j in range(len(M_topo[0])):
if i != j and M_topo[i][j] != M:
edges.append((i, j, M_topo[i][j]))
print("=== Dijkstra ===")
print("Let's find the shortest-path from 0 to 13:")
length, Shortest_path = dijkstra(edges, 0, 13)
print('length = ', length)
print('The shortest path is ', Shortest_path)