Algorithms · Dijkstra's Algorithm
Time Complexity Analysis of Dijkstra's Algorithm
Overview
Dijkstra's algorithm with a min-heap has time complexity , found by aggregate analysis: cost of each main operation multiplied by how many times it runs, then summed. The heap operations dominate, and the decrease-key term is the leading one.
Key concepts
- Aggregate analysis: list every main operation of the algorithm, find the cost of one execution of each, multiply by how many times that operation is performed, and add everything up.
- Relax = decrease key: relaxing a node already inside the heap only reduces its distance (its key), which is exactly a decrease-key operation in a min-heap.
- Extract min count: every vertex must come out of the heap, even the last element in , so extract min runs times.
- Decrease key count: the number of decrease keys equals the number of relax operations, which equals the number of edges, because in the worst case every edge is relaxed at least once.
- Worst case assumption: not every relax necessarily decreases the key, but in the worst case every relax may lead to a decrease key, so decrease keys .
- Min-heap is the standard: generally all implementations use a min-heap. Fibonacci heaps can sometimes be better but are not in the GATE syllabus, so fix to the heap implementation with complexity .

How it works
- Initialization: write all nodes to infinity. Cost ; very small, not a concern.
- : set the source distance to 0 / put the source in the set. Not much time.
- Build heap: add vertices to the heap. Building a heap from scratch on elements takes order of , so here it takes .
- Extract min: a heap operation costing per execution; performed times.
- Relax: when a node already inside the heap is relaxed, its distance (key) decreases — a decrease-key operation.
- Decrease key: costs per execution; performed times in the worst case.
- Add all costs; the leading term is .
Formulas
- Initialization cost
- Build heap cost (building a heap from scratch on vertices)
- Extract min: per operation, performed times
- Decrease key: per operation, performed times (worst case)
- Total: (since is the leading term)
- Symbols: = number of vertices, = number of edges.
Worked examples
Operation-count table for Dijkstra with a min-heap:
| Operation | Cost per execution | Number of executions | Total |
|---|---|---|---|
| Initialization (all nodes to infinity) | 1 | ||
| negligible | 1 | — | |
| Build heap | 1 | ||
| Extract min | (all vertices must leave the heap) | ||
| Decrease key (from relax) | (worst case: every edge relaxed once) |
Sum ; the leading term gives overall .
Revision summary
- Use aggregate analysis: cost of each operation × number of times it is performed, then add.
- Relax inside the heap is decrease key, because relaxing only reduces a node's distance.
- Extract min runs times (every vertex must come out of the heap), each .
- Decrease key runs times in the worst case (number of edges = number of relaxes = number of decrease keys), each .
- Not every relax necessarily decreases the key; the worst case assumes it does.
- Final complexity of Dijkstra with a min-heap: ; dominates initialization , build heap , and extract min .
- Fibonacci heaps improve this to because decrease-key is amortized.
Previous-year exam insights
- With a binary min-heap, Dijkstra runs in = for connected graphs;
with an array (linear scan for extract-min) it is = ;
with a Fibonacci heap it is .
Asked inGATE 2005UGC NET 2019UGC NET 2018 - Dijkstra's algorithm requires non-negative edge weights; with negative weights it may give wrong answers, and Bellman-Ford () must be used.
Asked inGATE 2026UGC NET 2012 - For a DAG, single-source shortest paths are computed in by relaxing edges in topological order, which is faster than Dijkstra and works with negative weights.
Asked inGATE 2026 - Standard complexity matches: Prim's with a heap ( with an array), Kruskal's = , Floyd-Warshall , Bellman-Ford , Edmonds-Karp .
Asked inUGC NET 2019UGC NET 2018UGC NET 2012 - Dijkstra's algorithm does not work correctly on graphs with negative-weight edges even if no negative cycle exists, because a finalized vertex's distance is never revisited.
Asked inGATE 2026 - The bound counts at most V extract-min operations and at most E decrease-key operations, each ; the initialization is dominated.
Asked inGATE 2005UGC NET 2018