algorithm - 使用 BFS 找到从 s 到 t 的最昂贵路径

标签 algorithm graph-theory breadth-first-search

在给定的图 G=(V,E) 中,每条边都有一个成本 c(e)。我们有一个起始节点 s 和一个目标节点 t。我们如何使用以下 BFS 算法找到从 s 到 t 的边数最少的最昂贵路径?

BFS(G,s):
    foreach v in V do
        color[v] <- white; parent[v] <- nil
    color[s] <- grey; parent[s] <- s
    BFS-Visit(s)

BFS-Visit(u):
    Q <- empty queue
    Enqueue(Q,u)
    while Q != empty do
        v <- Dequeue(Q)
        foreach w in Adj[v] do
            if color[w] white then
               color[w] <- grey
               parent[w] <- v
               Enqueue(Q,w)
        color[v] <- black 

最佳答案

BFS 的特性是距离源 d 的所有节点的集合被认为恰好在距离 d+1 的所有节点的集合之前。因此,即使节点为灰色,您也必须更新“最昂贵的路径”:

BFS(G,s):
    foreach v in V do
        color[v] <- white; parent[v] <- nil; mesp[v] <- -inf
        # mesp[v]: most expensive shortest path from s to v
    color[s] <- grey; parent[s] <- s; mesp[s] <- 0
    BFS-Visit(s)

BFS-Visit(u):
    Q <- empty queue
    Enqueue(Q,u)
    while Q = empty do
        v <- Dequeue(Q)
        foreach w in Adj[v] do
            if color[w] != black and mesp[v] + c(v, w) > mesp[w]:
               color[w] <- grey
               mesp[w] = mesp[v] + c(v, w)
               parent[w] <- v
               Enqueue(Q,w)
        color[v] <- black

关于algorithm - 使用 BFS 找到从 s 到 t 的最昂贵路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441101/

相关文章:

algorithm - Quicksort 的缓存遗忘程度如何?

graph-theory - 图 OLAP 处理 - Giraph vs. Tinkerpop3 GraphComputer

algorithm - 在图中寻找最短的 4 边循环

algorithm - 确定给定障碍物可以行驶多少点

php - 从字符串中提取启发式(模糊)日期?

algorithm - 更有效地找到差异较小的最大区域

algorithm - 缓存失效——有通用的解决方案吗?

algorithm - in/out活变量计算算法详解

algorithm - 根据特定条件创建图表

java - 通过 HashSet 与链接哈希集进行迭代