为什么 BellmanFord 算法需要循环“顶点个数 - 1”次呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JasonLaw
V2EX    算法

为什么 BellmanFord 算法需要循环“顶点个数 - 1”次呢?

  •  
  •   JasonLaw 2020-10-08 16:50:12 +08:00 2360 次点击
    这是一个创建于 1881 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面是我所找到的一些资料:

    但我还是不能够理解 BellmanFord 算法。

    • 为什么 BellmanFord 算法需要循环“顶点个数 - 1”次?
    • 每次循环到底保证了什么不变性( invariant )?最后可以帮助证明,经过“顶点个数 - 1”次循环后,能够找到顶点 s 到其他顶点的最短距离。
    第 1 条附言    2020-10-09 22:16:17 +08:00

    我又重新看了一遍MIT 6.006 Lecture 17: Bellman-Ford 中的正确性证明,终于算是明白了,希望也对后来看这个主题的人有所帮助。

    6 条回复    2020-10-10 07:14:08 +08:00
    litmxs
        1
    litmxs  
       2020-10-08 19:06:10 +08:00 via Android
    因为每次的松弛操作都可以保证从一个结点到其相邻结点的最短路估计值达到最短路的实际值,也就是保证了所有深度为 1 的路径最短。n 次操作则可以保证所有深度为 n 的路径最短。由于在不存在负圈的情况下,从 s 出发到任意结点的最短路不会经过同一个结点两次,所以最短路的长度(路径上边的数量)不会超过|v|-1 。所以算法可以在有限次数的松弛下结束。
    lidlesseye11
        2
    lidlesseye11  
       2020-10-09 00:17:20 +08:00
    关于 V-1
    https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/
    参考 How does this work? 那一段

    "不变性( invariant )"是啥意思?
    JasonLaw
        3
    JasonLaw  
    OP
       2020-10-09 22:26:26 +08:00
    @lidlesseye11 #2 我说的“不变性( invariant )”其实是 loop invariant -https://stackoverflow.com/questions/3221577/what-is-a-loop-invariant
    lidlesseye11
        4
    lidlesseye11  
       2020-10-09 23:48:50 +08:00
    @JasonLaw
    受教了。
    那 BF 的 loop invariant 应该就是 After the i-th iteration of the outer loop, the shortest paths with at most i edges are calculated. 吧
    JasonLaw
        5
    JasonLaw  
    OP
       2020-10-10 06:47:15 +08:00 via iPhone
    @lidlesseye11 对,而且可以通过这个 loop invariant 证明算法的正确性。
    JasonLaw
        6
    JasonLaw  
    OP
       2020-10-10 07:14:08 +08:00 via iPhone
    @litmxs 感觉你说的“ 因为每次的松弛操作都可以保证从一个结点到其相邻结点的最短路估计值达到最短路的实际值,也就是保证了所有深度为 1 的路径最短。n 次操作则可以保证所有深度为 n 的路径最短。”怪怪的,但是我又说不太上来是哪里有问题,替换为“ After the i-th iteration of the outer loop, the shortest paths with at most i edges are calculated.”会更好理解。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4728 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 01:09 PVG 09:09 LAX 17:09 JFK 20:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86