ENع
→Bellman-Ford
الدرس 34 · Shortest paths and spanning trees

Bellman-Ford and negative edges

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

Dijkstra's algorithm is fast, but it trusts a place as soon as it is the closest, so one negative road can make it wrong. Bellman-Ford trusts nothing: it relaxes every road, in a fixed order, V − 1 times. Watch four places and four roads, one with a cost of −4, settle round by round, see why V − 1 rounds are always enough, and use one extra pass to catch a loop that pays you back. The lesson "Dijkstra's algorithm" covers the greedy method this one replaces.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 34 · تفاعلي
01 / 09

Roads with a refund

Four places joined by one-way roads. Each road has a cost; the road from b to a costs −4, a refund. We want the cheapest total cost from s to every place. A negative road is allowed; a loop that pays back is not.

a directed graph with edge weights (costs)
  a -> c  cost 1
  b -> a  cost -4
  s -> a  cost 2
  s -> b  cost 5

the same roads in Python, as a list of (u, v, w):
edges = [("a", "c", 1), ("b", "a", -4), ("s", "a", 2), ("s", "b", 5)]
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Roads with a refund

    Four places joined by one-way roads. Each road has a cost; the road from b to a costs −4, a refund. We want the cheapest total cost from s to every place. A negative road is allowed; a loop that pays back is not.

    a directed graph with edge weights (costs)
      a -> c  cost 1
      b -> a  cost -4
      s -> a  cost 2
      s -> b  cost 5
    
    the same roads in Python, as a list of (u, v, w):
    edges = [("a", "c", 1), ("b", "a", -4), ("s", "a", 2), ("s", "b", 5)]
  2. Dijkstra trusts too early

    Dijkstra's algorithm settles the closest place first and never revisits it. It settles a at 2, then c at 3. But s→b→a→c costs 5 − 4 + 1 = 2. The refund comes too late: a (really 1) and c (really 2) are both wrong.

    Dijkstra settles in order: s, a, c, b
    its answers: s 0 · a 2 · b 5 · c 3
    true cheapest: s 0 · a 1 · b 5 · c 2
    
    s -> b -> a -> c = 5 + (-4) + 1 = 2 < 3
    so a (2, really 1) and c (3, really 2) are both wrong
    its proof assumes no road has a negative cost
  3. Relax: keep the cheaper total

    Start with d[s] = 0 and every other place at ∞, meaning not reached yet. Relaxing a road u→v with cost w asks: is d[u] + w less than d[v]? If yes, v gets that cheaper total. That is the only move.

    d = {v: float("inf") for v in nodes}
    d["s"] = 0
    d: s 0 · a ∞ · b ∞ · c ∞
    
    relax(u, v, w):
        if d[u] + w < d[v]:
            d[v] = d[u] + w
    float("inf") + w is still inf, so a road out of
    an unreached place never changes anything
  4. Round 1: try every road

    Bellman-Ford tries every road in one fixed list order: a→c, b→a, s→a, s→b. a and b are still at ∞, so their roads do nothing yet. s→a gives a = 2, and s→b gives b = 5.

    round 1
      a→c ( 1): d[a] + w = ∞  (skip)
      b→a (-4): d[b] + w = ∞  (skip)
      s→a ( 2): d[s] + w = 0 + 2 = 2 < ∞  → d[a] = 2
      s→b ( 5): d[s] + w = 0 + 5 = 5 < ∞  → d[b] = 5
    
    d: s 0 · a 2 · b 5 · c ∞
  5. Round 2: the refund lands

    Same list again. a→c gives c = 2 + 1 = 3. b→a gives 5 − 4 = 1, cheaper than 2, so a drops to 1. Unlike in Dijkstra, a place can get cheaper again and again.

    round 2
      a→c ( 1): d[a] + w = 2 + 1 = 3 < ∞  → d[c] = 3
      b→a (-4): d[b] + w = 5 + (-4) = 1 < 2  → d[a] = 1
      s→a ( 2): d[s] + w = 0 + 2 = 2, not < 1
      s→b ( 5): d[s] + w = 0 + 5 = 5, not < 5
    
    d: s 0 · a 1 · b 5 · c 3
  6. Round 3: c catches up

    a→c now gives 1 + 1 = 2, so c drops from 3 to 2. Nothing else changes. The best route s→b→a→c has 3 roads, and it took 3 rounds to carry the news along it.

    round 3
      a→c ( 1): d[a] + w = 1 + 1 = 2 < 3  → d[c] = 2
      b→a (-4): d[b] + w = 5 + (-4) = 1, not < 1
      s→a ( 2): d[s] + w = 0 + 2 = 2, not < 1
      s→b ( 5): d[s] + w = 0 + 5 = 5, not < 5
    
    d: s 0 · a 1 · b 5 · c 2
  7. Why V − 1 rounds

    After round k, every place whose cheapest route uses at most k roads is right. A route with no loop has at most V − 1 roads (V = number of places). Each round tries all E roads, so the work is O(V·E).

    after round k: d[v] <= the cheapest cost of any route
    from s to v that uses at most k roads
    
    V = number of places = 4, E = number of roads = 4
    3 rounds x 4 roads = 12 tries
    time O(V·E), extra space O(V) for d
    
    route s -> b -> a -> c: 3 roads = V - 1
  8. One more pass checks

    Try every road once more. Nothing gets cheaper, so the answers are final: s 0, a 1, b 5, c 2. If a whole round ever changes nothing, the loop can also stop early.

    check pass (lines 5 to 7)
      a→c ( 1): d[a] + w = 1 + 1 = 2, not < 2
      b→a (-4): d[b] + w = 5 + (-4) = 1, not < 1
      s→a ( 2): d[s] + w = 0 + 2 = 2, not < 1
      s→b ( 5): d[s] + w = 0 + 5 = 5, not < 5
    
    no road relaxes: return d
    d: s 0 · a 1 · b 5 · c 2
  9. A loop that pays back

    Add a road c→b costing 2. The loop b→a→c→b costs −4 + 1 + 2 = −1, so every lap is cheaper and no cheapest total exists. After 3 rounds, the extra pass still improves a: a negative cycle.

    add c -> b (2): loop b -> a -> c -> b = -4 + 1 + 2 = -1
    after 3 rounds: s 0 · a 1 · b 4 · c 2
    check pass:
      a→c ( 1): d[a] + w = 1 + 1 = 2, not < 2
      b→a (-4): d[b] + w = 4 + (-4) = 0 < 1  → d[a] = 0
    
    a road still relaxes after V - 1 rounds:
    return None (a negative cycle is reachable from s)

اختبار يتاح بعد الخطوة الأخيرة.