1. Gyanastra
  2. Algorithms
  3. Dijkstra's Algorithm
  4. Time Complexity Analysis of Dijkstra's Algorithm
Free lesson, no account needed 5 min read 8 practice questions after it
Asked in past papers6 times
  • GATE CSE20262005
  • UGC NET2019201820122007

Algorithms · Dijkstra's Algorithm

Time Complexity Analysis of Dijkstra's Algorithm

Overview

Dijkstra's algorithm with a min-heap has time complexity O(Elog⁡V)O(E \log V), 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 Elog⁡VE \log V 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 QQ, so extract min runs VV 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 =E= E.
  • 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 O(Elog⁡V)O(E \log V).
Graph diagram: Time Complexity Analysis of Dijkstra's Algorithm
Figure. Time Complexity Analysis of Dijkstra's Algorithm

How it works

  1. Initialization: write all nodes to infinity. Cost O(V)O(V); very small, not a concern.
  2. S={s}S = \{s\}: set the source distance to 0 / put the source in the set. Not much time.
  3. Build heap: add VV vertices to the heap. Building a heap from scratch on NN elements takes order of NN, so here it takes O(V)O(V).
  4. Extract min: a heap operation costing O(log⁡V)O(\log V) per execution; performed VV times.
  5. Relax: when a node already inside the heap is relaxed, its distance (key) decreases — a decrease-key operation.
  6. Decrease key: costs O(log⁡V)O(\log V) per execution; performed EE times in the worst case.
  7. Add all costs; the leading term is Elog⁡VE \log V.

Formulas

  • Initialization cost =O(V)= O(V)
  • Build heap cost =O(V)= O(V) (building a heap from scratch on VV vertices)
  • Extract min: O(log⁡V)O(\log V) per operation, performed VV times →O(Vlog⁡V)\rightarrow O(V \log V)
  • Decrease key: O(log⁡V)O(\log V) per operation, performed EE times (worst case) →O(Elog⁡V)\rightarrow O(E \log V)
  • Total: O(V)+O(V)+O(Vlog⁡V)+O(Elog⁡V)=O(Elog⁡V)O(V) + O(V) + O(V \log V) + O(E \log V) = O(E \log V) (since Elog⁡VE \log V is the leading term)
  • Symbols: VV = number of vertices, EE = number of edges.

Worked examples

Operation-count table for Dijkstra with a min-heap:

OperationCost per executionNumber of executionsTotal
Initialization (all nodes to infinity)O(V)O(V)1O(V)O(V)
S={s}S = \{s\}negligible1—
Build heapO(V)O(V)1O(V)O(V)
Extract minO(log⁡V)O(\log V)VV (all vertices must leave the heap)O(Vlog⁡V)O(V \log V)
Decrease key (from relax)O(log⁡V)O(\log V)EE (worst case: every edge relaxed once)O(Elog⁡V)O(E \log V)

Sum =O(V)+O(V)+O(Vlog⁡V)+O(Elog⁡V)= O(V) + O(V) + O(V \log V) + O(E \log V); the leading term Elog⁡VE \log V gives overall O(Elog⁡V)O(E \log V).

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 VV times (every vertex must come out of the heap), each O(log⁡V)O(\log V).
  • Decrease key runs EE times in the worst case (number of edges = number of relaxes = number of decrease keys), each O(log⁡V)O(\log V).
  • Not every relax necessarily decreases the key; the worst case assumes it does.
  • Final complexity of Dijkstra with a min-heap: O(Elog⁡V)O(E \log V); Elog⁡VE \log V dominates initialization O(V)O(V), build heap O(V)O(V), and extract min O(Vlog⁡V)O(V \log V).
  • Fibonacci heaps improve this to O(E+Vlog⁡V)O(E + V \log V) because decrease-key is O(1)O(1) amortized.

Previous-year exam insights

  • With a binary min-heap, Dijkstra runs in O((V+E)log⁡V)O((V+E) \log V) = O(Elog⁡V)O(E \log V) for connected graphs;
    with an array (linear scan for extract-min) it is O(V2+E)O(V^{2} + E) = O(V2)O(V^{2});
    with a Fibonacci heap it is O(E+Vlog⁡V)O(E + V \log V).
    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 (O(VE)O(VE)) must be used.
    Asked inGATE 2026UGC NET 2012
  • For a DAG, single-source shortest paths are computed in O(V+E)O(V+E) by relaxing edges in topological order, which is faster than Dijkstra and works with negative weights.
    Asked inGATE 2026
  • Standard complexity matches: Prim's O(Elog⁡V)O(E \log V) with a heap (O(V2)O(V^{2}) with an array), Kruskal's O(Elog⁡E)O(E \log E) = O(Elog⁡V)O(E \log V), Floyd-Warshall Θ(V3)\Theta(V^{3}), Bellman-Ford O(VE)O(VE), Edmonds-Karp O(VE2)O(VE^{2}).
    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 O(Elog⁡V)O(E \log V) bound counts at most V extract-min operations and at most E decrease-key operations, each O(log⁡V)O(\log V); the initialization O(V)O(V) is dominated.
    Asked inGATE 2005UGC NET 2018
Pages 1–2 of 2