← back to algorithms

dedicated visualizer

Dijkstra's Algorithm

Finds the shortest path from the source to every reachable node when all edge weights are non-negative. This page keeps the runner, chart, and controls focused on a single algorithm so the walkthrough feels calmer than the overview page.

graphsadvancedbest O((V + E) log V)worst O((V + E) log V)space O(V)
weighted graphrelaxationshortest pathgreedy choice

session controls

Compare this algorithm against a related one, turn on quiz mode, or keep the current state in a shareable URL.

current shareable URL

Copy the URL to preserve this exact dataset, target, compare mode, and quiz state.

browse more

Want a different problem or visual mode? Jump back to the catalog and open another dedicated page.

open catalog

scenario presets

Load a focused input that reveals a specific behavior quickly instead of hand-editing every value first.

graph controls

Graph algorithms reuse the same learning graph so you can compare traversal and shortest-path behavior side by side.

graph note

BFS and DFS emphasize traversal order. Dijkstra adds weighted relaxations and a cost-aware final route.

step 1 / 250% complete

current action

initialize distances

current node

none

run summary

Finished in 25 steps. G has the shortest finalized distance, so Dijkstra can stop.

visited

7

relaxations

7

path cost

8

frontier

0

steps

25

final route

A → C → D → E → G

cost 8

current explanation

Initialize A with distance 0 and every other node with infinity.

simple explanation

At the start, only the source has a known distance.

pseudocode

1initialize source distance to 0 and others to infinity
2pick the unvisited node with the smallest tentative distance
3inspect each outgoing edge from that node
4relax the edge if it yields a shorter path
5stop once the target is finalized or no nodes remain reachable

complexity card

best

O((V + E) log V)

average

O((V + E) log V)

worst

O((V + E) log V)

space

O(V)

algorithm notes

intuition

Dijkstra greedily finalizes the nearest unfinished node because non-negative weights make that choice safe.

tradeoffs

  • Great for non-negative weighted shortest paths.
  • Not valid when negative-weight edges exist.
  • Priority queues improve performance on larger graphs.

when to use it

Use for shortest-path problems on weighted graphs with non-negative edge costs.

interview tips

  • Explain why finalized nodes never need to be revisited with non-negative weights.
  • Be ready to contrast Dijkstra with BFS and Bellman-Ford.

what I learned building this

typed definitions

One algorithm schema now drives the catalog, counters, pseudocode, notes, and visual modes, which keeps the UI consistent as the lab grows.

replay over mutation

Precomputed steps made it much easier to synchronize explanations, metrics, quiz prompts, and scrubber playback without hidden state drifting out of sync.

portfolio framing

Shareable URL state, compare mode, and responsive layouts mattered as much as the algorithm logic because this page needs to teach clearly and still feel polished as a product.

more in this lane

Want a different take on the same problem family? These stay in the same category but change the strategy.