Dijkstra’s algorithm can fail with negative edge-lengths

Adding a large positive constant to all edge-lengths does not work

Idea: Convert all edge-lengths to be non-negative

  • by adding a large positive constant

Adding a large positive constant to all edge-lengths can change identity of shortest paths from

Bellman-Ford Algorithms

Assumptions: There are no negative cycles.
Negative cycles no shortest paths!

Consequence

For any two vertices and , there is shortest path having at most edges, where is number of vertices

  • Let be a shortest path with fewest edges
  • If a vertex repeats on , then delete the cycle from
    • The number of edges on decreases
    • Length of cannot increase as there are no negative cycles

For each vertex and each , let

  • denote length of shortest path having at most edges ( is fixed source)

Setting up the recurrence:

  1. If the shortest path uses at most edges
    • Then the value is
  2. Otherwise the last edge, i.e., the i-th edge has to be for some
    • The cost of path is
    • We need to minimise this over all such that is an edge in
  • We need to take minimum over both these choices

Algorithm

Correctness

  • Proof by induction on . Base case is
  • Inductive step is essentially argues the correctness of the recurrence.

Running time

  • The table has entries.
  • Computing each entry needs to compute to look up known entries
    • Computing needs knowledge of for all
    • Need to perform min operations and additions
  • Total running time is entries time to compute each entry.