Algorithms · Dijkstra's Algorithm
Dijkstra's Algorithm with Min-Heap Implementation
Overview
Dijkstra's algorithm (as given in CLRS) finds shortest paths from a single source.
The min-heap (priority queue) version makes the relax operation a decrease-key operation on the heap: extract-min becomes (instead of an scan) while decrease-key costs (instead of in the array version).
Key concepts
- Min-heap (priority queue) implementation: all vertices are placed in a min-heap; the vertex with the smallest current distance is always extracted next.
- Relaxation as decrease-key: relaxing an edge means decreasing the distance (key) of a node, which is exactly the decrease-key operation of a min-heap.
- Initialize-Single-Source: gives the source the value 0 and every other node the value infinity; it is just initialization, assigning a distance value to every node.
- Set S: contains all vertices whose shortest path has already been finalized. It starts empty, and vertices are added to S one by one as their shortest paths get decided.
- Q = G.V: a min-heap built with all the vertices as members, using the build-heap method.
- Array-based version: simple Dijkstra runs in order of , i.e. ; the heap version is presented to make the operations (extract-min, decrease-key) faster.

How it works
Inputs to the algorithm:
- A graph — tells how many vertices and how many edges there are.
- A weight matrix — gives the weight of every edge.
- A source vertex .
Steps:
- Initialize-Single-Source(G, s): give the source the value 0 and every other node the value infinity.
- Set S: initially empty; it will hold the vertices whose shortest paths have been finalized.
- Q = G.V: build a min-heap with all the vertices as members (build-heap).
- Main loop — while Q is not empty:
- — take out the smallest element from the heap; it is deleted from the queue.
- Add to — the extracted vertex now has its shortest path finalized.
- For each vertex adjacent to (): relax the edge . This is the same relaxation done in the hand-worked examples, but now done through the heap so the operations are easier.
The process repeats: each time take the minimum, add it to S, relax neighbours, values change, take the next minimum, and so on.
Formulas
- Build-heap cost: order of ( = number of vertices)
- Array-based Dijkstra total time: order of , i.e.
- in heap form
Worked examples
Initialization on the graph:
- Source .
- All other nodes .
First relaxation from the source (value 0):
- One adjacent node: .
- Another adjacent node: .
- Heap now holds the values 2 and 10 (plus the remaining infinity values).
Next iteration:
- Extract-Min returns 2 (the smallest value).
- 2 is removed from Q and added to S.
- All neighbours of that node are relaxed again; their values change accordingly.
- The process repeats: extract the minimum, add to S, relax neighbours.
Revision summary
- All vertices go into the heap; Extract-Min picks the smallest tentative distance.
- Relaxing an edge is a Decrease-Key on the heap.
- S starts empty and grows by one vertex per Extract-Min.
- Initialize-Single-Source: source = 0, all others = infinity.
- Building the heap with all vertices costs order of (build-heap).
- The loop runs while Q is not empty; each iteration extracts the minimum, adds it to S, and relaxes all its adjacent edges.
- Array-based Dijkstra takes order of ; the heap implementation is used so that extract-min and decrease-key are cheaper.
Previous-year exam insights
- Dijkstra's algorithm is a greedy algorithm; it may give wrong distances if any edge has negative weight (correctness is only guaranteed for non-negative weights), and it does not work for graphs with negative cycles.
Asked inGATE 2008UGC NET 2019UGC NET 2017 - Time complexities of Dijkstra: array/linear-scan implementation Θ(V²); binary heap (equivalently ); Fibonacci heap .
Asked inGATE 2005UGC NET 2018UGC NET 2019 - For unweighted graphs, single-source shortest paths are computed most efficiently by BFS in time (a simple FIFO queue, not a heap).
Asked inGATE 2007GATE 2006 - For a DAG (weighted, possibly negative edges), single-source shortest paths are computed in by relaxing edges in topological order — faster than Dijkstra.
Asked inGATE 2026 - The order in which vertices are finalized (added to S) is non-decreasing in distance; ties may be broken arbitrarily, so more than one valid order can exist.
Asked inGATE 2004 - Algorithm-to-problem mapping: Dijkstra = single-source shortest path (greedy);
Bellman-Ford = single-source with negative edges;
Floyd-Warshall = all-pairs shortest path;
Prim/Kruskal = minimum spanning tree.
Asked inISRO 2023UGC NET 2018