Skip to content

Dijkstra Playground

Run Dijkstra on weighted terrain, draw walls, and compare settled distances with the cheapest path

1. The playground

Every open cell has a terrain cost from 1 to 9. Moving into a cell pays that cost; the start is free and walls are impassable. Run Dijkstra to watch the settled set grow in distance order. The yellow route is the cheapest path to E.

2. What to notice

  • The search does not expand in rings; it expands in cost order. A cheap detour can be settled before an expensive direct neighbor.
  • The number in a settled cell is the shortest distance from S, not the terrain cost. Before settlement, the cell shows its cost.
  • Drawing a wall can change the route completely. Because all costs are positive, a settled distance is final.
  • The path cost is the sum of the cells you enter, including E and excluding S. The readout and the path label agree.

3. The algorithm

Dijkstra settles the unsettled cell with the smallest tentative distance, then relaxes its open neighbours. The priority queue is the heap from the heap lesson.

Relaxation
With a binary heap

Dijkstra requires non-negative weights. A negative edge could improve a cell after it was settled, invalidating the order. Use Bellman–Ford for negative edges.

4. Related lessons

Site soundtrack