加权图
带权重的图称为加权图 (weighted graph),不带权重的图称为非加权图 (unweighted graph)。
加权图中每条边都有关联的数字,这些数字称为权重(weight)。
狄克斯特拉算法
要计算非加权图中的最短路径,可使用广度优先搜索 。要计算加权图中的最短路径,可使用狄克斯特拉算法。
狄克斯特拉算法背后的关键理念:找出图中最便宜的节点,并确保没有到该节点的更便宜的路径 !
不能将狄克斯特拉算法用于包含负权边的图。在包含负权边的图中,要找出最短路径,可使用另一种算法——贝尔曼-福德算法 (Bellman-Ford algorithm)
狄克斯特拉算法包含4个步骤:
- 找出最便宜的节点,即可在最短时间内前往的节点。
- 对于该节点的邻居,检查是否有前往它们的更短路径,如果有,就更新其开销。
- 重复这个过程,直到对图中的每个节点都这样做了。
- 计算最终路径。
代码实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| graph = {} graph["start"] = {} graph["start"]["a"] = 6 graph["start"]["b"] = 2
graph["a"] = {} graph["a"]["fin"] = 1
graph["b"] = {} graph["b"]["a"] = 3 graph["b"]["fin"] = 5
graph["fin"] = {}
infinity = float("inf") costs = {} costs["a"] = 6 costs["b"] = 2 costs["fin"] = infinity
parents = {} parents["a"] = "start" parents["b"] = "start" parents["fin"] = None
processed = []
def find_lowest_cost_node(costs): lowest_cost = float("inf") lowest_cost_node = None for node in costs: cost = costs[node] if cost < lowest_cost and node not in processed: lowest_cost = cost lowest_cost_node = node return lowest_cost_node
node = find_lowest_cost_node(costs) while node is not None: cost = costs[node] neighbors = graph[node] for n in neighbors.keys(): new_cost = cost + neighbors[n] if costs[n] > new_cost: costs[n] = new_cost parents[n] = node processed.append(node) node = find_lowest_cost_node(costs)
print(costs)
shortest_path = ['fin'] node = 'fin' while node != 'start': node = parents[node] shortest_path.insert(0, node) print(shortest_path)
|
小结
- 广度优先搜索用于在非加权图中查找最短路径。
- 狄克斯特拉算法用于在加权图中查找最短路径。
- 仅当权重为正时狄克斯特拉算法才管用。
- 如果图中包含负权边,请使用贝尔曼-福德算法。