Created in 1956 by Edsger Dijkstra, published in 1959. Single source shortest path problem for non-negative weighted graphs $(G,w)$.

Problem

Input:graph $G=(V,E)$ with weight function $w:E→R_{≥0}$, and a source vertex $s∈V$.

Output:all the shortest paths from $s→u$ where $u∈V$. $δ(s,u)$ is defined as the length of shortest path from $s$ to $u$, which is just the sum of the weights on the path.

Imagine that the vertices are ping pong balls, and edges are strings that connect them. Intuitively, Dijkstra’s asks us at each step, how much do we have to pull up by ball $s$ before the next ball leaves the ground? The ball connected to the shortest rope will lift up first, before all other balls.

## Structural lemma

Let $S⊂V$ such that for each $u∈S$, it is true that $D(u)=δ(s,u)$. This is the set of nodes that we’ve already found their shortest paths for.

Suppose that $v∈V−S$ such that $∀u∈S$, it is true that

$D(v)≤D(u)+w(u,v)$and we pick the $D(v)$ that is the smallest value out of all $v∈V−S$. Then $D(v)=δ(s,v)$, and we can immediately bring it into $S$.

### Proof

Assume that path $p$ is the shortest path $s→v$. Consider vertex $u$ such that $p:s→u→v$, and $(u,v)∈E$. We want to show that $δ(s,v)=D(v)$. There are two cases:

#### First case

When $u∈S$, meaning we’ve already found the shortest path for the vertex right before $v$.

Then $D(u)=δ(s,u)$ is true, so the inequality above is equivalent to $D(v)=δ(s,u)+w(u,v)$. This is just the definition of the length of $p$, which we’ve already established as a shortest path, and so $D(v)=δ(s,v)$.

#### Second case

When $u∈V−S$. Then, the vertex right before $v$ is not in the set $S$, implying that we’ve already crossed over (at least once).

Let $z∈V−S$ be the vertex of where we first crossed over from $S$ to $V−S$. So currently, the path is defined by $s→z→u→v$.

AFC $D(u)>δ(s,v)$. We know $δ(s,v)=δ(s,z)+δ(z,v)$ from Characterizations about optimal shortest paths, and we know $δ(s,z)=D(z)$, because this is just the first case:

- We’ve established $s→z$ to be the shortest path, because of the $δ(s,z)$ we derived above. This is the assumption.
- There exists a $u_{′}$ such that $s→u_{′}→z$ is a path, $u_{′}∈V−S$, and $(u_{′},z)∈E$. This has to be true because we defined $z$ to be the
*first*vertex that crossed over. - See the first case for the rest.

The inequality goes

$D(u)>δ(s,v)⟹D(u)>δ(s,z)+δ(z,v)⟹D(u)>D(z)+δ(z,v)$This implies $D(u)≥D(z)$, which means we should have instead chosen vertex $z$ to put into our set $S$ next instead of $v$. This is a contradiction because we know $z∈V−S$ and we chose $v$ instead.

## Algorithm

This naturally brings us to an algorithm where at each step, we find this vertex $v∈V−S$, and add it to our set $S$. We then adjust $D(⋅)$ for all other vertices in $V−S−{v}$. We repeat until every vertex is added to $S$, at which point we have discovered $δ(s,v)$ for all $s∈V$.

### Procedure

- Initialize a set $S$. Store the current distance in a function $D:V→R_{≥0}∪{∞}$. Set $D(s)=0$ and $D(u)=∞$ for all other vertices $u∈V−{s}$.
- Keep a min-heap of vertices holding $V−S$, where the keys are $D(v),v∈V−S$.
- Extract the minimum from the heap. $D(v)$ has become fixed; set $δ(s,v)←D(v)$.
- For all $z$ such that $(v,z)$ is an edge, we set $D(z)=min{D(z),D(v)+w(v,z)}$ for the corresponding vertex in the heap.

## Proof of correctness

We maintain two loop invariants. At any point in our algorithm, it must be true that

- $D(v)≥δ(s,v)$
- if $u∈S$ and $v∈V−S$, then $D(v)≤D(u)+w(u,v)$.

## Runtime

The algorithm ends when we finish extracting all vertices: there are $n$ such operations. The extracted vertex is operated on exactly once, by iterating over its edges, and possibly relaxing their weights. Then, we will decrease the key of each edge at most once, with $m$ such operations. We have

$mgn+ngn=O((m+n)gn)$Using Fibonacci heaps, we can cut the decrease key operation time down to amortized $O(1)$ time, which means runtime decreases to expected $O(m+ngn)$.