Given a network with arc lengths (or arc cost) for each arc , we seek the shortest path from a source node to every node . That is, the paths from to each with the smallest length

We let be the arc adjacency list of node and define .

As a min-cost flow problem, you can view this as sending 1 unit of flow throughout the network as cheaply as possible. This results in the following optimization problem:

We make the following assumptions:

  1. All arc lengths are integers.
  2. The network contains a directed path from node to every other node in the network.
  3. The network does not contain a negative cycle.
  4. The network is directed.

Dijkstra’s Algorithm

Under the assumption that all arcs have non-negative arc lengths, Dijkstra’s algorithm finds the shortest paths from the source node to all other nodes in time.

Dijkstra’s algorithm maintains a label which is an upper bound on the shortest path length from to node . Nodes are either permanently labeled or temporarily labeled. The idea of the algorithm is to walk through the network and permanently label nodes in the order of their distances from .

We start by setting and every other node a temporary label of . At each iteration, we pick the node with the smallest , make it permanent, and scan arcs in to update the temporary labels of adjacent nodes.

We maintain an array which gives defines an out-tree rooted at the source node . Every tree arc satisfies the condition .

Algorithm 2 Dijkstra's shortest path algorithm

procedure Dijkstra(G=(N,A), sG=(N,A),~s)

S:=S:=\emptyset

S:=N\overline{S} := N

d(i):=+d(i):=+\infty for all iNi\in N

d(s):=0d(s):=0

pred(s):=0\textup{pred}(s) := 0

while S<n|S|<n do

let iSi\in\overline{S} be a node for which d(i)=min{d(j):jS}d(i)=\min\{d(j):j\in\overline{S}\}

S:=S{i}S:=S\cup\{i\}

S:=S{i}\overline{S}:=\overline{S}\setminus\{i\}

for all (i,j)A(i)(i,j)\in A(i) do

if d(j)>d(i)+cijd(j) > d(i)+c_{ij} then

d(j):=d(i)+cijd(j) := d(i) + c_{ij}

pred(j):=i\textup{pred}(j) := i

end if

end for

end while

end procedure

Fibonacci Heap Implementation

If we make use of a Fibonacci heap where the keys are the distance labels and the values are the nodes, then we can pick the temporarily node with minimum distance label in time. The new algorithm is given below:

Algorithm 3 Dijkstra's shortest path algorithm via a heap

procedure HeapDijkstra(G=(N,A), sG=(N,A),~s)

create heap HH

d(i):=+d(i):=+\infty for all iNi\in N

d(s):=0d(s):=0

pred(s):=0\textup{pred}(s) := 0

H[0]=sH[0] = s

while HH\ne\emptyset do

take the node ii from HH with minimum key

delete node ii from HH

for all (i,j)A(i)(i,j)\in A(i) do

if d(j)>d(i)+cijd(j) > d(i)+c_{ij} then

d(j):=d(i)+cijd(j) := d(i) + c_{ij}

pred(j):=i\textup{pred}(j) := i

set key for node jj in HH to d(j)d(j)

end if

end for

end while

end procedure

The modified version runs in time.