Dijkstra's Algorithm Visualizer

Explore Dijkstra's shortest-path algorithm with animated relaxation, tentative distances, visited nodes, and reconstructed routes.

Current status: Ready. Use the controls to begin exploring Dijkstra's Algorithm.

Dijkstra's Algorithm Visualizer

Click a node to set Start/End, click an edge to edit weight
Start: A
End: F
Legend
Start Node
End Node
Currently Visiting (greedy pick)
Visited
Shortest Path

Distances from A

A
B
C
D
E
F
Step
-

Why is Dijkstra's Algorithm Greedy?

At every step, Dijkstra's algorithm picks the closest unvisited node and commits to it - it never revisits that choice. This greedy strategy works because all edge weights are non-negative: once a node is settled with its minimum distance, no future relaxation could ever make it shorter.

Time & Space Complexity

  • With Binary HeapO((V + E) log V)
  • With Array (naive)O(V²)
  • SpaceO(V + E)

Real-World Use Cases

  • GPS navigation and route planning
  • Network routing protocols (OSPF)
  • Flight/transit connection search

Concept guide

Review the mental model, tradeoffs, and practical use cases after you experiment.

Dijkstra's Algorithm Complete Info Card

Greedy AlgorithmShortest Path

Dijkstra's Algorithm greedily expands the closest unvisited node at every step, relaxing its outgoing edges, until the shortest distance to every reachable node is known.

Algorithm Characteristics

Time Complexity (Binary Heap)

Using a min-priority queue over unvisited nodes

O((V+E) log V)

Time Complexity (Array)

Naive linear scan for the minimum each round

O(V²)

Space Complexity

Distance table, parent pointers, and adjacency list

O(V + E)

Negative Weights

Greedy choice breaks down once weights can be negative

Not Supported

Greedy Choice

Always settles the unvisited node with the smallest distance

Closest node

Optimality

Guaranteed shortest paths when all weights are non-negative

Yes*

Algorithm Steps

1

Set distance to source = 0, all others = infinity

2

Pick the unvisited node with the smallest distance

3

Mark it visited (its distance is now final)

4

Relax each of its edges: update neighbor distances

5

Repeat until every reachable node is visited

6

Backtrack parent pointers to reconstruct the path

When to Use

  • Single-source shortest path with non-negative weights
  • Routing, navigation, and network path optimization
  • Graphs where you need distances to all nodes, not just one pair

When to Avoid

  • Graphs with negative edge weights (use Bellman-Ford instead)
  • All-pairs shortest paths on dense graphs (Floyd-Warshall may be simpler)
  • Extremely large graphs without a good heap implementation
Did You Know? A* search is essentially Dijkstra's algorithm with an added heuristic to guide the search toward the goal faster.
Shortest pathPriority queueGPS routing