The Bellman-Ford Algorithm 笔记

  1. Single-Source Shortest Paths Revisited
    1.1 The Single-Source Shortest Path Problem.
    Input: Directed graph G=(V,E) G = ( V , E ) , edge lengths ce c e for each eE e ∈ E , source vertex sV s ∈ V . [Can assume no parallel edges].
    1.2 Goal: For every destination vV v ∈ V , compute the length(sum of edge costs) pf a shortest s-v path.
  2. On Dijkstra’s Algorithm
    2.1 Good news: O(mlogn) O ( m l o g n ) running time using heaps.
    Bad news:
    (1) Not always correct with negative edge lengths.
    (2)Not very distributed (relevant for Internet routing)
    Solution: The Bellman-Ford algorithm.
    On Negative Cycles.
    Question:How to define shortest path when G G has a negative cycle?
    Soution #1: Compute the shortest s-v path, which cycles allowed.
    Problem: Undefined or − ∞ .
    Solution #2: Computes shortest cycle-free s-v path.
    Problem: NP-hard (no polynomial algorithm, unless P=NP)
    Solution #3: (For now) Assume input graph has no negative cycles.
    Single-Source Shortest Path Problem, Revisited.
    Input: Directed graph G=(V,E) G = ( V , E ) , edge lengths ce c e ,
    source vertex sV s ∈ V .
    Goal: either
    (A) For all destinations vV v ∈ V ,compute the length of a shortest sv s − v path. OR
    (B) Output a negative cycle.
    Optimal Substructure(Informal)
    Intuition: Exploit sequential nature of paths. Subpath of a shortest path should itself be shortest.
    Issue: Not clear how to define “smaller” & “larger” subproblems.
    Key idea: Artificially restrict the number of edges in a path.
    Subproblem size Number of permitted edges.
    Lemma: Let G=(V,E) G = ( V , E ) be a directed graph with edge lengths ce c e and source vertex s.
    [G might or might not have a negative cycle]
    For every vV,i{1,2…} v ∈ V , i ∈ { 1 , 2… } , let P= P = shortest sv s − v path with at most i i edges. (Cycles are permitted)
    Case 1: If P has (i1) ≤ ( i − 1 ) edges, it is a shortest sv s − v path with (i1) ≤ ( i − 1 ) edges.
    Case 2: If P has i i edges with last hop( w w , v v ), then P P ′ is a shortest sw s − w path with (i1) ≤ ( i − 1 ) edges.
    The Basic Algorithm
    The Recurrence
    Notation: Let Li,v= L i , v = minimum length of a s-v path with i ≤ i edges.
    With cycles allowed
    Defined as + + ∞ if no sv s − v paths with i ≤ i edges.
    Recurrence: For every vV,i{1,2,...} v ∈ V , i ∈ { 1 , 2 , . . . }

Li,v=min{L(i1),vmin(u,v)E{L(i1),w+cwv},(1) (1) L i , v = m i n { L ( i − 1 ) , v m i n ( u , v ) ∈ E { L ( i − 1 ) , w + c w v } ,

If No Negative Cycles

Now: Suppose input graph

G G has negative cycles.


Shortest paths do not have cycles.

[removing a cycle only decrease length]


Have

(n1) ≤ ( n − 1 ) egdes.

Point: If

G G has negative cycle, only need to solve subproblems up to

i=n1 i = n − 1 .

Point: If

G G has no negative cycle, only need to solve subproblems up to

i=n1 i = n − 1 .

Subproblems:

Compute

Li,v L i , v for all

i{0,1,...,n1} i ∈ { 0 , 1 , . . . , n − 1 } and all

vV v ∈ V .

The Bellman-Ford Algorithm

(1) Let

A=2D A = 2 − D array(indexed by

i i and

v v )

Base case:


A[0,s]=0;A[0,v]=+ A [ 0 , s ] = 0 ; A [ 0 , v ] = + ∞ for all

vs v ≠ s /

For

i=1,2,...,n1 i = 1 , 2 , . . . , n − 1

For each

vV v ∈ V


Li,v=min{L(i1),vmin(u,v)E{L(i1),w+cwv},(2) (2) L i , v = m i n { L ( i − 1 ) , v m i n ( u , v ) ∈ E { L ( i − 1 ) , w + c w v } ,

As discussed: IF G has no negative cycle, then algorithm is correct.

[with final answer =

A[n1,v]s A [ n − 1 , v ] ′ s ]

Whatis the running time of the Bellman-Ford algorithm?
Reason: Total work is O(nvVin.deg(v)) O ( n ∑ v ∈ V i n . d e g ( v ) ) = O(mn) O ( m n ) 入度。
Checking for a Negative Cycle
Question: What if the input graph G has a negative cycle.
Claim:
G has a no negative-cost cycle In the extended Bellman-Ford algorithm,
A[n1,v]=A[n,v] A [ n − 1 , v ] = A [ n , v ] for all vV v ∈ V .
Consequence: Can check for a negative cycle just by running Bellman-Ford for one extra iteration(running time still O(mn) O ( m n ) ).
Proof of Claim
( ) Already proved in correctness of Bellman-Ford.
( ) Assume A[n1,v]=A[n,v] A [ n − 1 , v ] = A [ n , v ] for all
vV v ∈ V
Consequence: Can check for a negative cycle just by jsut running Bellman-Ford for one extra iteration.
(running time still O(mn) O ( m n ) ) .
Let d(v) d ( v ) denote the common value of A[n1,v] A [ n − 1 , v ] and A[n,v] A [ n , v ] .
Recall algorithm:

A[n,v]=min{A[i1,v]min(w,v)E{A[n1,w]+cwv},(3) (3) A [ n , v ] = m i n { A [ i − 1 , v ] m i n ( w , v ) ∈ E { A [ n − 1 , w ] + c w v } ,

其中

A[n,v]d(v),A[n1,w]d(w) A [ n , v ] → d ( v ) , A [ n − 1 , w ] → d ( w )

Thus:

d(v)d(w)+cwv d ( v ) ≤ d ( w ) + c w v for all edges

(w,v)E ( w , v ) ∈ E

Equivalently :

d(v)d(w)cwv d ( v ) − d ( w ) ≤ c w v

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_31805127/article/details/80812972
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞