Find the shortest path in a directed graph from a fixed vertex .

Setting:

  • A directed graph with vertices
  • Edge-lengths given by length:
  • A fixed source vertex $s$$
  • Question: For each , find length of shortest path.

Observe that we are asked to need to find ( values because is ) values

Algorithm

Running time is where is number of vertices and edges respectively.

  • while loop runs time from to
  • within each while loop we need time

Example of Dijkstra’s

Correctness of Dijkstra’s Algorithm

Correctness of each intermediate step

Theorem

Consider the set at any point during the algorithm. For each , the quantity stores the value of shortest path.

We prove this theorem by induction on :

  • Base case: and
  • Inductive Hypothesis : Suppose that the theorem holds for
  • Suppose that we now grow by one more vertex by adding .
  • By line 5: for some
  • Suppose there is a path shorter than
  • Let this path leave via the edge for some

  • Contradiction

Comments on Dijkstra’s algorithm

  • Modify the algorithm to maintain the actual shortest paths from
    • Not just the distance of shortest paths!
  • Dijkstra’s algorithm is very similar to Breadth First Search (BFS)
  • Can we apply this algorithm to undirected graphs ?
    • Yes! Just replace an undirected edge of length with two directed edges and of length each

Dijkstra’s algorithm fails if edge lengths can be negative!