Bellman-Ford

From Algowiki
Revision as of 17:22, 17 June 2015 by Luedecke (talk | contribs)
Jump to navigation Jump to search
All pairs shortest paths Bellman-Ford

General information

Algorithmic problem: All pairs shortest paths

Prerequisites:

Type of algorithm: loop

Auxiliary data:

  1. A real-valued (n×n) matrix M, where n=|V|. The eventual contents of M will be returned as the result of the algorithm.
  2. A real-valued (n×n) matrix L, where n=|V|. This matrix represents the graph and the arc lengths and will not be changed throughout the algorithm.

Notation: On this page, Mi refers to the contents of M after i0 iterations.

Abstract view

Invariant: After i0 iterations, Mi(v,w) contains the length of a shortest (v,w)-path with at most i+1 arcs (for all v,wV).

Variant: i increases by 1.

Break condition: i=n1.

Induction basis

Abstract view: For all v,wV, we set

  1. M0(v,v):=L(v,v):=0;
  2. M0(v,w):=L(v,w):=(v,w) if (v,w)A;
  3. M0(v,w):=L(v,w):=+, if vw and (v,w)A.

Induction step

Abstract view: For all v,wV, we set Mi+1(v,w):=min{Mi(v,u)+L(u,w)uV}.

(Note that u=v is included, so the right-hand side is identical to min{Mi(v,w),min{Mi(v,u)+L(u,w)uV}}.)

Implementation: Obvious.

Correctness: Follows from the fact that a shortest path cannot have more than |V|1 arcs.

Complexity

Statement: The asymptotic complexity is Θ(n4) in the best and worst case.

Proof: The main loop terminates after n1 iterations. In each iteration of this loop, we update all n2 matrix entries, and computing one update value requires Θ(n) steps.