Skip to content
FoundationSource checked

Dijkstra's Algorithm

Finds shortest paths from one source in a graph with nonnegative edge weights.

Quick Facts

Compact metadata for implementation and discovery.

Category
Pathfinding
Difficulty
Intermediate
Family
Shortest Path
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
23
Code example
Not ready
Visualization
Ready
Aliases
Dijkstra, Dijkstra's Shortest Path

Overview

Dijkstra's Algorithm maintains tentative distances and repeatedly settles the unsettled node with the smallest known distance, relaxing its outgoing edges.

Core Concepts

tentative distanceedge relaxationpriority queuenonnegative weights

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O((V + E) log V)

Average

O((V + E) log V)

Worst

O((V + E) log V)

Space

O(V)

When to Use

  • Use it when all edge weights are nonnegative.
  • Use it when shortest paths from one source are needed.
  • Use a priority queue implementation for sparse graphs.

When Not to Use

  • Avoid it when any edge weight can be negative.
  • Avoid it for unweighted graphs where BFS is simpler.
  • Avoid recomputing it from scratch when the graph changes incrementally and a dynamic method is available.

What It Does

  • Computes single-source shortest paths
  • Uses edge relaxation and a priority queue

Use Cases

  • Road-network routing with nonnegative costs
  • Network delay calculations
  • Weighted graph search

Inputs

  • Weighted graph with nonnegative edges
  • Starting node

Outputs

  • Shortest distance to each reachable node
  • Previous-node links for paths

Key Formulas (1)

Relaxation step

dist[v] = min(dist[v], dist[u] + weight(u, v))
dist[v] = min(dist[v], dist[u] + weight(u, v))

Updates a neighbor distance if the path through the current node is shorter.

Pseudocode

  1. 1.Set the start distance to zero
  2. 2.Put the start node in a priority queue
  3. 3.Remove the node with the smallest distance
  4. 4.Relax each outgoing edge
  5. 5.Update neighbors when a shorter distance is found
  6. 6.Repeat until the queue is empty

Canonical Example

Input
edges = A-B: 1, B-C: 2, A-C: 5; start = A; goal = C
Output
path = A -> B -> C, cost = 3
Explanation
Dijkstra's Algorithm: The algorithm compares route costs and selects the cheaper path through B.

Implementation Notes

  • Initialize the source distance to zero and all others to infinity.
  • Relax each outgoing edge from the node with the smallest tentative distance.
  • Store predecessors if the actual path must be reconstructed.

Common Pitfalls

  • Using Dijkstra's algorithm with negative edge weights.
  • Treating a stale priority-queue entry as final without checking the current distance.
  • Forgetting to initialize unreachable nodes to infinity.

Interactive Mechanism Walkthrough

2D Grid Wavefront & Trajectory

Show tentative distances updating while the lowest-distance node is settled.

S
#
#
#
#
#
#
#
G
Step 1 of 6: Set the start distance to zero
weighted-graph-frontier

Notes

With a standard priority queue, duplicate queue entries are acceptable if stale entries are skipped when popped.

Sources (1)

What each reference supports for this entry.

CLRS

Book

Textbook reference for the algorithmic family and analysis of Dijkstra's Algorithm.

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Prerequisite ofA* Search

A* adds a heuristic estimate to Dijkstra-style path search.

Alternative toBellman-Ford

Both solve shortest paths, but Bellman-Ford supports negative edge weights.

Bidirectional Dijkstra runs Dijkstra-style search from both directions.

Johnson's Algorithm runs Dijkstra from each source after reweighting.

Efficient Dijkstra implementations use a priority queue for tentative distances.

Related Algorithms

Tags

Shortest PathWeighted GraphPriority QueueGraph