Bellman-Ford: Difference between revisions

From Algowiki
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
[[Category:Videos]]
[[Category:Checkup]]
[[Category:Algorithm]]
[[Category:Algorithm]]
[[Category:Main Algorithm]]
[[Category:Main Algorithm]]
{{#ev:youtube|https://www.youtube.com/watch?v=3_zqU5GWo4w|500|right|All pairs shortest paths Bellman-Ford|frame}}
==General information==
==General information==
'''Algorithmic problem:''' [[All pairs shortest paths]]
'''Algorithmic problem:''' [[All pairs shortest paths]]
Line 11: Line 11:


'''Auxiliary data:'''
'''Auxiliary data:'''
# A [[Paths|distance-valued]] <math>(n \times n)</math> matrix <math>M</math>, where <math>n=|V|</math>. The eventual contents of <math>M</math> will be returned as the result of the algorithm.
# A real-valued <math>(n \times n)</math> matrix <math>M</math>, where <math>n=|V|</math>. The eventual contents of <math>M</math> will be returned as the result of the algorithm.
# A [[Paths|distance-valued]] <math>(n \times n)</math> matrix <math>L</math>, where <math>n=|V|</math>. This matrix represents the graph and the arc lengths and will not be changed throughout the algorithm. It is <math>L=M^1</math> in the terminology of Section "Powers of distance matrices" on [[Paths#Powers of distance matrices|this]] page.
# A real-valued <math>(n \times n)</math> matrix <math>L</math>, where <math>n=|V|</math>. This matrix represents the graph and the arc lengths and will not be changed throughout the algorithm.
 
'''Notation:'''
On this page, <math>M^i</math> refers to the contents of <math>M</math> after <math>i\geq 0</math> iterations.


==Abstract view==
==Abstract view==

Latest revision as of 22:51, 19 June 2015

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.