有向图单源非负权值回路最短路径——BellmanFord算法

BellmanFord算法是一种暴力求解算法O(N3),它考虑所有情况,所以可以允许边的权值为负。(不过不允许出现负权值回路,因为那样会出现无限小)

之所以说它暴力,是因为它求出了每个节点所有长度为1的路径,再求所有长度为2的路径,并更新最短路径数组dist[]和path[],如此迭代直至求到长度n-1的路径。(n为图节点个数)

整体来看,每个节点纵向比较,从1到n-1长度的路径中取最小值作为最终路径长度。

因为它考虑了所有情况,所以负边也可以算出。

因为暴力,复杂度比Dijkstra高一个数量级,达到了O(N3)。不过Dijkstra不能计算负边,所以倒也划算。

下面给出算法。

 1   public int minPathBF(int source, int target){
 2         if (source == target){
 3             System.out.println("(" + source + " " + target + ")");
 4             return 0;
 5         }
 6         int[] path = bellmanFord(source);
 7         int result = 0;
 8         String way = "";
 9         while (-1 != path[target]){
10             way = "(" + path[target] + " " + target + ") " + way;
11             result += graph[path[target]][target];
12             target = path[target];
13         }
14         System.out.println(way);
15         return result;
16     }
17 
18     private int[] bellmanFord(int vertex){
19         int[] path = new int[numVertices];
20         int[] dist = new int[numVertices];
21         //initial
22         for (int i = 0; i < numVertices; i++) {
23             if (graph[vertex][i] != 0 && graph[vertex][i] < 1000){
24                 dist[i] = graph[vertex][i];
25                 path[i] = vertex;
26             }else {
27                 dist[i] = 1000;
28                 path[i] = -1;
29             }
30         }
31         for (int i = 2; i < numVertices; i++){// calculate to n-1 path length
32             for (int j = 0; j < numVertices; j++){// calculate num j vertex path
33                 if (j != vertex){
34                     for (int k = 0; k < numVertices; k++) {
35                         if (k != vertex && graph[k][j] != 0 && graph[k][j] < 1000 && dist[k] < 1000){
36                             if (graph[k][j] + dist[k] < dist[j]){
37                                 dist[j] = dist[k] + graph[k][j];
38                                 path[j] = k;
39                             }
40                         }
41                     }
42                 }
43             }
44         }
45         return path;
46     }

 

转载于:https://www.cnblogs.com/zqiguoshang/p/6679355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
离字典,将起始节点的距离设为0,其他节点的距离设为无穷大 distances = {node: sys.maxsize for node in graph} distances[start] = 0 # 初始化已访问节点的集合和未访以下是使用问节点D的集ijkstra合 visited = set() unvisited算法求解最路径的Python = set(graph) while unvisited: # 代码示例: ```python class D选择当前ijkstra距: def __init__(self, graph离最小的节点 , start, current goal): self.graph = graph # 邻接表_node = min(unvisited, key=lambda self node: distances[node]) # 更新.start = start当前节点的 # 起邻居节点点 self.goal =的距离 goal # 终点 for neighbor in graph self.open[current_node]: _list = {} if neighbor in # open 表 self.closed_list unvisited: new_distance = distances[current_node] + = {} graph[current_node][neighbor # closed 表 self.open_list[start] if new_distance] = < distances[neighbor]: 0.0 # 将 distances[neighbor] = new_distance # 将当前起点放入 open_list 中 self.parent = {节点标记start:为已访 None} 问,并从未访问集合中移除 visited.add # 存储节点的父子关系。键为(current_node) 子节点, unvisited值为父.remove(current_node) return节点。方便做最 distances def print后_path(dist路径的ances,回 start溯 self.min, end): _dis = None # 根 # 最路径的长度 def shortest_path据距离字典和终点节点(self): while True: ,逆向 if self打印路径.open_list is path = [end None: ] print('搜索 current_node =失败 end while current_node !=, 结束!') break distance start: , min_node = for neighbor in graph min(zip[current_node]: if(self.open_list distances[current.values(), self_node] ==.open_list.keys distances[neighbor())) #] + graph 取出距[neighbor][current_node]: 离最小的节点 self path.open_list.pop.append(min_node)(neighbor) current_node = neighbor break path.reverse() # 将其从 open_list 中去除 self print.closed("_list[minShortest_node] = path from", distance # 将节点加入 closed start, "to", end,_list ":", "->".join(path)) # 示例 中 if min_node == self.goal: # 如果节点为的邻接矩阵终点 self.min_dis = distance 表示 graph shortest = { _path = [ 'Aself.goal]': {'B': # 5, 'C 记录从': 终1}, 点回溯的路径 'B
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值