algorithm - 找到所有边的最小最高成本的算法是什么?
问题描述
我正在尝试解决一个问题,我需要找到从开始到目标节点的每一步最小成本。我认为这个算法存在,但我找不到这个算法的名称。在我正在研究的情况下,只有积极的边缘,并且可能存在周期。这不是 dijkstra 的,因为我不是在寻找最低总成本,而是在寻找代表所有步骤中最低最高成本的成本。
在以下示例中,该算法将因此输出 3,因为 3 是该算法可以找到路径的最高最小成本。因此不是最低成本,因为那将是 4。
*起始节点为灰色,目标节点为绿色。
我认为存在这样的算法,我曾尝试在 google 上搜索,但目前找不到该算法的名称。
解决方案
这可以通过对 dijkstra 的简单修改来解决。
Dijkstra 通过始终选择最低成本路径来工作。我们知道,只要我们在图中移动时路径成本永远不会减少(在您的情况下是这样),我们总是会通过从最低到最高路径成本的顺序迭代来找到最佳答案。我们只需要将成本函数修改为每条路径的最大值并运行 dijkstra。
这是一个伪代码(基本上是python)实现:
import priority_queue
def min_cost_path(start, end):
min_heap = priority_queue([[0, start]]) # queue in form of (cost, node)
visited = set()
while min_heap:
# get lowest weight path thus far
# cost is the lowest cost possible to get to node
cost, node = min_heap.pop()
# optimal path to this node has already been found, ignore this
if node in visited: continue
if node == end: return cost
# this node has been visited
visited.add(node)
# add candidate node-weights to the queue
for weight, neighbor in neighbors(node):
if neighbor not in visited:
min_heap.push((max(weight, cost), neighbor))
return -1 # not possible
推荐阅读
- angular - 如何添加 MatButtonModule Angular
- python - 为什么我在 python 中得到这个组错误?
- php - 如果满足 php 条件,如何使用 PDO 准备语句
- mongodb - 如何在 F# 中使用字符串动态指定字段 MongoDB 定义
- javascript - Bcrypt 返回 [Object Promise] 或 Promise {
哈希代码后 - node.js - mongoose group by unwind 多个数组重复数据删除
- java - 带有两个文件的 Spring 调用外部 API - multipart/form-data
- javascript - getlastrow,然后是最后一列,然后根据该信息进行填充
- python - 如何在网站上托管 discord.py 机器人?(/运行 python 脚本)
- java - 用java对两个集合进行分组的高级编写,例如使用流API或其他高级类型?