Best-First: Dijkstra’s Algorithm
Learn about Dijkstra’s algorithm and its applications in solving the shortest paths problem.
We'll cover the following...
Proof of tenseness property in Dijkstra’s algorithm
If we replace the FIFO queue in breadth-first search with a priority queue, where the key of a vertex is its tentative distance , we obtain an algorithm first published in 1957 by a team of researchers at the Case Institute of Technology led by Michael Leyzorek in an annual project report for the Combat Development Department of the US Army Electronic Proving Ground. The same algorithm was independently discovered by Edsger Dijkstra in 1956 (but not published until 1959), again by George Minty sometime before 1960, and again by Peter Whiting and John Hillier in 1960. A nearly identical algorithm was also described by George Dantzig in 1958. Although several early sources called it “Minty’s algorithm,” this approach is now universally known as “Dijkstra’s algorithm,” in full accordance with Stigler’s Law. The pseudocode for this algorithm is shown below.
An easy induction proof implies that at all times during the execution of this algorithm, an edge is tense if and only if vertex is either in the priority queue or is the vertex most recently extracted from the priority queue. Thus, Dijkstra’s algorithm is an instance of Ford’s general strategy, which implies that it correctly computes shortest paths, provided there are no negative cycles in .
In the image below, we compute shortest paths in a dag by relaxing outgoing edges in topological order. In each iteration, bold edges indicate predecessors, and the bold vertex is about to be scanned.
Algorithm
Implementation
import heapqimport sysclass Edge:def __init__(self, v, weight):self.v = vself.weight = weightclass Graph:def __init__(self, V):self.V = Vself.adj = [[] for _ in range(V)]def add_edge(self, u, v, w):self.adj[u].append(Edge(v, w))def init_SSSP(s, dist):dist[s] = 0for i in range(len(dist)):if i != s:dist[i] = sys.maxsizedef dijkstra(s, G):pq = []dist = [0] * G.Vinit_SSSP(s, dist)heapq.heappush(pq, (0, s))while pq:u = heapq.heappop(pq)[1]for e in G.adj[u]:v = e.vw = e.weightif dist[u] != sys.maxsize and dist[v] > dist[u] + w:dist[v] = dist[u] + wheapq.heappush(pq, (dist[v], v))for i in range(G.V):print(f"Shortest path from {s} to {i} is {dist[i]}")if __name__ == "__main__":V = 5G = Graph(V)G.add_edge(0, 1, 10)G.add_edge(0, 2, 5)G.add_edge(1, 2, 2)G.add_edge(1, 3, 1)G.add_edge(2, 1, 3)G.add_edge(2, 3, 9)G.add_edge(2, 4, 2)G.add_edge(3, 4, 4)G.add_edge(4, 3, 6)dijkstra(0, G)
Explanation
-
Line 5–8: These lines define the
Edge
data structure, which contains the destination vertex (v
) and weight (weight
) of an edge in the graph. -
Lines 11–17: These lines define the
Graph
data structure, which contains the number of vertices (V
) and an adjacency list (adj
) that stores the edges for each vertex. -
Line 27: This method implements Dijkstra’s algorithm to compute the SSSP from the source vertex
s
in the graphG
. -
Line 35: This line iterates through each edge
e
in the adjacency listG.adj[u]
of vertexu
. -
Line 39: This line updates the distance to vertex
v
.
No negative edges
Dijkstra’s algorithm is particularly well-behaved when the input graph has no negative-weight edges. In this setting, the algorithm intuitively expands a wavefront outward from the source vertex , passing over vertices in increasing order of their distance from , similar to breadth-first search.
The figure below shows the algorithm in action, with the first four iterations of Dijkstra’s algorithm on a graph with no negative edges. In each iteration, bold edges indicate predecessors, shaded vertices are in the priority queue, and the bold vertex is about to be scanned. The remaining iterations do not change the distances or the shortest-path tree.
We can derive a self-contained proof of correctness for Dijkstra’s algorithm in this setting by formalizing this wavefront intuition. For each integer , let denote the vertex returned by the th call to , and let be the value of just after this . In particular, we have and . We cannot assume at this point that the vertices are distinct; in principle, the same vertex might be more than once.
Lemma 1: If ...