算法90----图的最短路径
一、单源最短路径:迪杰斯特拉【权值需非负】
目标:从某个起点出发,找到到各个点的最短距离。
思路:一个S集合存已经遍历的顶点,一个Q集合存未遍历的顶点。一个dist列表存从初始点到当前点的最短路径,即dist[i] 表示初始点到i所需的最短距离。
如果用堆优先队列来找dist中最小值,时间复杂度为O(E+VlogV),否则时间复杂度为O(V2),V 为顶点。
伪代码:
代码:
MAX_value = 999999 def dijkstra(graph, s): dist = [MAX_value] * len(graph) dist[s] = 0 S = [] Q = [i for i in range(len(graph))] while Q: u_dist = min([d for i,d in enumerate(dist) if i in Q]) u = dist.index(u_dist) S.append(u) Q.remove(u) for v , d in enumerate(graph[u]): if 0 < d < MAX_value: dist[v] = min(dist[v] , dist[u] + d) return dist if __name__ == '__main__': graph_list = [[0, 9, MAX_value, MAX_value, MAX_value, 14, 15, MAX_value], [9, 0, 24, MAX_value, MAX_value, MAX_value, MAX_value, MAX_value], [MAX_value, 24, 0, 6, 2, 18, MAX_value, 19], [MAX_value, MAX_value, 6, 0, 11, MAX_value, MAX_value, 6], [MAX_value, MAX_value, 2, 11, 0, 30, 20, 16], [14, MAX_value, 18, MAX_value, 30, 0, 5, MAX_value], [15, MAX_value, MAX_value, MAX_value, 20, 5, 0, 44], [MAX_value, MAX_value, 19, 6, 16, MAX_value, 44, 0]] distance = dijkstra(graph_list, 0) print(distance)
|
二、弗洛伊德算法【所有节点的最短路径】---动态规划
思路:时间复杂度O(n3),
代码:DP相当于上面的Dis矩阵
MAX_value = 99999 def Floyd(graph): N = len(graph) DP = [[MAX_value] * N for i in range(N)] path = [[MAX_value] * N for i in range(N)] #####初始化DP和path矩阵。DP相当于动态规划的DP矩阵,path【i】【j】是记录i到j的最短路径的中间节点。 for i in range(N): for j in range(N): DP[i][j] = graph[i][j] path[i][j] = -1 ######动态规划过程 for i in range(N): for j in range(N): for k in range(N): if DP[i][j] > DP[i][k] + DP[k][j]: DP[i][j] = DP[i][k] + DP[k][j] path[i][j] = k return DP,path if __name__ == '__main__': graph_list = [[0, 9, MAX_value, MAX_value, MAX_value, 14, 15, MAX_value], [9, 0, 24, MAX_value, MAX_value, MAX_value, MAX_value, MAX_value], [MAX_value, 24, 0, 6, 2, 18, MAX_value, 19], [MAX_value, MAX_value, 6, 0, 11, MAX_value, MAX_value, 6], [MAX_value, MAX_value, 2, 11, 0, 30, 20, 16], [14, MAX_value, 18, MAX_value, 30, 0, 5, MAX_value], [15, MAX_value, MAX_value, MAX_value, 20, 5, 0, 44], [MAX_value, MAX_value, 19, 6, 16, MAX_value, 44, 0]] print(Floyd(graph_list))