Maths WorkshopMaths problems with full solutions
Exercise 1684Combinatorics › Graph theoryL1Added 3 Nov 2026

Dijkstra's algorithm

#Dijkstra#shortest path#weighted graph

Statement

Describe Dijkstra's algorithm for single-source shortest paths.

Hint

Maintain tentative distances; repeatedly extract the vertex with minimum distance and relax its neighbors.

Show solutionHide solution

Solution

Algorithm.
1. Set d(r)=0d(r)=0, d(v)=+∞d(v)=+\infty for v≠rv\neq r.
2. Priority queue with all vertices.
3. Extract vertex uu with minimum d(u)d(u); mark as finalized.
4. For each neighbor vv of uu: relax d(v)=min⁡(d(v),d(u)+w(u,v))d(v)=\min(d(v), d(u)+w(u,v)).

Correctness. Nonnegative weights guarantee that the extracted vertex's distance is final.

Complexity. O((∣V∣+∣E∣)log⁡∣V∣)O((|V|+|E|)\log|V|).

Conclusion. Dijkstra solves SSSP in weighted nonnegative graphs.