# Dijkstra算法——通过边实现松弛# 指定一个点到其他各顶点的路径——单源最短路径# 初始化图参数
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}}
# 每次找到离源点最近的一个顶点,然后以该顶点为重心进行扩展# 最终的到源点到其余所有点的最短路径# 一种贪婪算法defDijkstra(G, v0, INF=999):""" 使用 Dijkstra 算法计算指定点 v0 到图 G 中任意点的最短路径的距离
INF 为设定的无限远距离值
此方法不能解决负权值边的图
"""
book = set() # 存储当前的顶点集合
minv = v0
# 源顶点到其余各顶点的初始路程
dis = dict((k, INF) for k in G.keys())
dis[v0] = 0# 此时dis为 {1: 0, 2: 999, 3: 999, 4: 999, 5: 999, 6: 999}while len(book) < len(G): # 还没有遍历完所有顶点的时候
book.add(minv) # 把当前的顶点加入到book中for w in G[minv]: # 以当前点的中心向外扩散,G[v0] : {1: 0, 2: 1, 3: 12}, w : 1,2,3if dis[minv] + G[minv][w] < dis[w]: # 如果从当前点扩展到某一点的距离小与已知最短距离
dis[w] = dis[minv] + G[minv][w] # 对已知距离进行更新
new = INF # 从剩下的未确定点中选择最小距离点作为新的扩散点# 不能直接取min是因为要记录最短的距离所对应的节点vfor v in dis.keys():
if v in book: continueif 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 *
defdijkstra_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 notin seen:
seen.add(v1)
path = (v1, path)
if v1 == to_node:
return cost, path
for c, v2 in g.get(v1, ()):
if v2 notin seen:
heappush(q, (cost + c, v2, path))
return float("inf"), []
defdijkstra(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 # 1. Get the length firstly;# 2. Decompose the path_queue, to get the passing nodes in the shortest path.
left = path_queue[0]
ret_path.append(left) # 2.1 Record the destination node firstly;
right = path_queue[1]
while len(right) > 0:
left = right[0]
ret_path.append(left) # 2.2 Record other nodes, till the source-node.
right = right[1]
ret_path.reverse() # 3. Reverse the list finally, to make it be normal sequence.return len_shortest_path, ret_path
# ==================== Given a list of nodes in the topology shown in Fig. 1.
list_nodes_id = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
# ==================== Given constants matrix of topology.
M = 99999# This represents a large distance. It means that there is no link.# M_topo is the 2-dimensional adjacent matrix used to represent a topology.
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]
]
# --- Read the topology, and generate all edges in the given topology.
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])) # (i,j) is a link; M_topo[i][j] here is 1, the length of link (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)