1. Gyanastra
  2. Algorithms
  3. Dijkstra's Algorithm
  4. Dijkstra's Algorithm with Min-Heap Implementation
Free lesson, no account needed 5 min read 14 practice questions after it
Asked in past papers14 times
  • GATE CSE202620082007200620052004
  • ISRO202320142007
  • UGC NET20192018201720142007

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 O(log⁡V)O(\log V) (instead of an O(V)O(V) scan) while decrease-key costs O(log⁡V)O(\log V) (instead of O(1)O(1) 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 n2n^2, i.e. Θ(V2)\Theta(V^2); the heap version is presented to make the operations (extract-min, decrease-key) faster.
Graph diagram: Dijkstra's Algorithm with Min-Heap Implementation
Figure. Dijkstra's Algorithm with Min-Heap Implementation

How it works

Inputs to the algorithm:

  • A graph GG — tells how many vertices and how many edges there are.
  • A weight matrix ww — gives the weight of every edge.
  • A source vertex ss.

Steps:

  1. Initialize-Single-Source(G, s): give the source the value 0 and every other node the value infinity.
  2. Set S: initially empty; it will hold the vertices whose shortest paths have been finalized.
  3. Q = G.V: build a min-heap with all the vertices as members (build-heap).
  4. Main loop — while Q is not empty:
    1. u=Extract-Min(Q)u = \text{Extract-Min}(Q) — take out the smallest element from the heap; it is deleted from the queue.
    2. Add uu to SS — the extracted vertex now has its shortest path finalized.
    3. For each vertex vv adjacent to uu (v∈G.Adj[u]v \in G.Adj[u]): relax the edge (u,v)(u, v). 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 VV (VV = number of vertices)
  • Array-based Dijkstra total time: order of n2n^2, i.e. Θ(V2)\Theta(V^2)
  • Relax(u,v)\text{Relax}(u, v) in heap form == Decrease-Key(Q,v,new distance)\text{Decrease-Key}(Q, v, \text{new distance})

Worked examples

Initialization on the graph:

  • Source s=0s = 0.
  • All other nodes =∞= \infty.

First relaxation from the source (value 0):

  • One adjacent node: ∞→2\infty \to 2.
  • Another adjacent node: ∞→10\infty \to 10.
  • 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 VV vertices costs order of VV (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 n2n^2; 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 O(Elog⁡V)O(E \log V) (equivalently O(Vlog⁡V+Elog⁡V)O(V \log V + E \log V)); Fibonacci heap O(Vlog⁡V+E)O(V \log V + E).
    Asked inGATE 2005UGC NET 2018UGC NET 2019
  • For unweighted graphs, single-source shortest paths are computed most efficiently by BFS in Θ(V+E)\Theta(V+E) 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 Θ(V+E)\Theta(V+E) 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
Pages 1–2 of 2