Dijkstra’s Algorithm
Dijkstra’s algorithm is what you reach for when a short route is not necessarily a cheap one. A road can be slow, a tile can be muddy, and one move can cost more than another. Instead of visiting cells in discovery order, Dijkstra always picks the frontier cell with the lowest known cost from the start.
Each time it reaches a neighbor, it asks whether going through the current cell would be cheaper than the neighbor’s best route so far. If so, it updates that cost and the parent link. With nonnegative edge costs, a cell removed from the priority queue has its optimal cost. When that cell is the goal, the cheapest route is settled.
Try Costly shortcut. Normal cells cost one to enter and cells marked 5 cost five. Dijkstra can walk farther around the expensive patch and still pay less. Moves are up, down, left, and right; there are no diagonals. The starting cell costs zero. No route demonstrates what happens when walls completely separate the endpoints.
Draw walls, paint terrain, or move S and E, then press Play. Step backward, forward, or scrub to inspect a decision. The source uses a binary min-heap with decrease-key: O((V + E) log V) time and O(V) auxiliary space. Storing visual snapshots adds memory beyond the algorithm itself.