Dijkstra's algorithm computes single-source shortest paths in graphs with non‑negative edge weights by repeatedly settling the closest unsettled vertex and relaxing its outgoing edges using a priority queue.
Given a weighted graph G = (V, E) with non‑negative edge weights w(u,v) ≥ 0 and a source vertex s, Dijkstra's algorithm computes the shortest path distance dist[v] from s to every vertex reachable from s. It maintains a frontier of tentative distances and progressively finalizes (settles) the minimum one.
Using duplicate insertions (lazy deletion) avoids implementing decrease-key at cost of extra heap size.
Fibonacci heap achieves O(E + V log V); rarely worth complexity in practice. With dense graphs, adjacency matrix + simple selection yields O(V^2).
1Dijkstra(G, source):2 for each vertex v in G:3 dist[v] ← ∞4 parent[v] ← NIL5 visited[v] ← false // settled / finalized flag6 dist[source] ← 07 PQ ← empty min-priority queue (key = dist)8 insert (source, 0) into PQ9 while PQ not empty:10 (u, du) ← extract-min(PQ) // smallest tentative distance11 if visited[u]: // stale entry check (optional optimization)12 continue13 visited[u] ← true // u is now settled; dist[u] is final14 for each edge (u, v, w) outgoing from u:15 if visited[v]:16 continue // already finalized17 if dist[u] + w < dist[v]: // relaxation18 dist[v] ← dist[u] + w19 parent[v] ← u20 insert (v, dist[v]) into PQ // may create duplicate stale entries21 return dist, parent