Dijkstra
General information
Algorithmic problem: Single source shortest paths
Prerequisities: For all
Type of algortihm: loop
Auxiliary data:
- A temporary distance value
for each node . At termination, it is for all where is the shortest -path. - A bounded priority queue
of size , which contains nodes from and takes their -values as keys. The node with minimal key is returned.
Abstract view
Invariant: After
contains all nodes of except and further nodes.- For the nodes
not in , it is . - For the nodes
in , is the length of a shortest -path that solely contains nodes not in (except for itself, of course). As usual, this means if there is no such path.
In particular, it is
Variant:
Break condition:
, which means that all nodes are reachable from and have been processed;- otherwise, if
for the next node of , which means that for every node in and hence none of the nodes in is reachable from .
Induction basis
Abstract view:
- All nodes of
except have to be members of . - Root
must have correct distance . - The other nodes must meet Invariant #3.
Implementation:
;- For all
, set . - For all
with set . - Insert all nodes in
into .
Proof:
Obvious.
Induction step
Abstract view:
One node is extracted from
Implementation:
- Extract the next node
from . - For each outgoing arc
such that , set .
Correctness:
The first invariant is trivially maintained. So we will focus on the second and third invariants.
Consider the moment immediately before the
Now we know
For a node
Complexity
Statement: The worst-case complexity is in
Proof: Each node is inserted in and extracted from
- in the initialization if
, - when
is extracted from otherwise.
Therefore, all decrease-key operations require
Pseudocode
DIJKSTRA(G,w,s)
DIJKSTRA(G,w,s)
1 INITIALIZE-SINGLE-SOURCE(G,s)
2 S = ∅
3 Q = G.V
4 while Q ≠ ∅
5 u = EXTRACT-MIN(Q)
6 S = S ∪ {u}
7 for each vertex v ∈ G.Adj[u]
8 RELAX(u,v,w)
RELAX(u,v,w)
RELAX(u,v,w)
1 if v.d > u.d + w(u,v)
2 v.d = u.d + w(u,v)
3 v.π = u
INITIALIZE-SINGLE-SOURCE(G,s)
INITIALIZE-SINGLE-SOURCE(G,s)
1 for each vertex v ∈ G.V
2 v.d = ∞
3 v.π = NIL
4 s.d = 0
Heuristic speed-up techniques
- We do not need to insert all nodes except for
in . In fact, it suffices to initially insert the endnodes of the outgoing arcs of and, during an iteration, to insert the nodes of the outgoing arcs of the node that is being extracted from . In a sense, the nodes of then constitute the "frontierline" of the algorithm. We choose another variant on Dijkstra's algorithm mainly for simplicity of presentation. - Clearly, Dijkstra's algorithm also solves the single-source single-target case. In this case, Dijkstra may safely terminate once has been extracted from
. In fact, the loop invariant implies from then on. This variant on Dijkstra's algorithm is sometimes called the early termination variant. - In the single-source single-target case, two searches may be applied simultaneously: one from
on , the other one from on the transpose of . This variant is called bidirectional search. It terminates once one node has been seen by both searches. Then either the concatenation of the paths to this node from both searches is a shortest -path, or such a path may be found among the paths of the following kind: There is an arc such that has been seen in the forward search from the source and has been seen in the backward search from . - In a scenario where we perform a large number of searches on the same graph and arc lengths, we do not need to touch the whole graph in each search:
- We change the algorithm as suggested by the first point: only keep the "frontierline" in
. - We also change the algorithm as suggested by the second point: terminate once
is processed. - To avoid touching all nodes for initializing the
-values, we maintain sort of a version counter, or time stamp, for each node, which holds the ID of the last search that met this node. Whenever a node is met in a search and the nodes version counter does not equal the ID of the current search, the distance value of this node is regarded as (no matter what its current value is).
- We change the algorithm as suggested by the first point: only keep the "frontierline" in
- The early termination variant may be modified as follows: For admissible node potentials
, replace length by where for each . This variant is usually called goal-directed search. The heuristic idea is that we (hopefully) reach earlier because the arcs that roughly point towards are shortened, and the arcs the roughly point in a direction away from are lengthened.