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:
- All arc lengths are integers.
- The network contains a directed path from node to every other node in the network.
- The network does not contain a negative cycle.
- 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()
for all
while do
let be a node for which
for all do
if then
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()
create heap
for all
while do
take the node from with minimum key
delete node from
for all do
if then
set key for node in to
end if
end for
end while
end procedure
The modified version runs in time.