...

/

Best-First: Dijkstra’s Algorithm

Best-First: Dijkstra’s Algorithm

Learn about Dijkstra’s algorithm and its applications in solving the shortest paths problem.

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 vv is its tentative distance dist(v)dist(v), 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 uvu\rightarrow v is tense if and only if vertex uu 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 GG.

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


Dijkstra(s):InitSSSP(s)Insert(s,0)while the priority queue is not emptyuExtractMin()for all edges uvif uv is tenseRelax(uv)if v is in the priority queueDecreaseKey(v,dist(v))elseInsert(v,dist(v))\underline{Dijkstra(s):} \\ \hspace{0.4cm} InitSSSP(s) \\ \hspace{0.4cm} Insert(s, 0) \\ \hspace{0.4cm} while\space the\space priority\space queue\space is\space not\space empty \\ \hspace{1cm} u ← ExtractMin( ) \\ \hspace{1cm} for\space all\space edges\space u\rightarrow v \\ \hspace{1.7cm} if \space u\rightarrow v\space is\space tense \\ \hspace{2.3cm} Relax(u\rightarrow v) \\ \hspace{2.3cm} if\space v\space is\space in\space the\space priority\space queue \\ \hspace{2.7cm} DecreaseKey(v, dist(v)) \\ \hspace{2.3cm} else \\ \hspace{2.7cm} Insert(v, dist(v))

Implementation

Press + to interact
import heapq
import sys
class Edge:
def __init__(self, v, weight):
self.v = v
self.weight = weight
class Graph:
def __init__(self, V):
self.V = V
self.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] = 0
for i in range(len(dist)):
if i != s:
dist[i] = sys.maxsize
def dijkstra(s, G):
pq = []
dist = [0] * G.V
init_SSSP(s, dist)
heapq.heappush(pq, (0, s))
while pq:
u = heapq.heappop(pq)[1]
for e in G.adj[u]:
v = e.v
w = e.weight
if dist[u] != sys.maxsize and dist[v] > dist[u] + w:
dist[v] = dist[u] + w
heapq.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 = 5
G = 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 graph G.

  • Line 35: This line iterates through each edge e in the adjacency list G.adj[u] of vertex u.

  • 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 ss, passing over vertices in increasing order of their distance from ss, 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 ii, let uiu_i denote the vertex returned by the iith call to ExtractMinExtractMin, and let did_i be the value of dist(ui)dist(u_i) just after this ExtractionExtraction. In particular, we have u1=su_1 = s and d1=0d_1 = 0. We cannot assume at this point that the vertices uiu_i are distinct; in principle, the same vertex might be ExtractedExtracted more than once.

Lemma 1: If G ...