Just added: Algorithms you can run and practice
Updated Aug 6, 2026 Edit on GitHub

Dijkstra's Shortest Path, From Scratch

Every time a map app draws a fastest route, something very close to Dijkstra's algorithm is running underneath. It answers one of the most useful questions in computing: given a network of places connected by roads of different lengths, what is the cheapest way from here to there?

If you have met BFS and graphs already, you have most of the intuition. BFS finds the shortest path when every step costs the same. The moment steps have different costs - a highway versus a side street - BFS gives the wrong answer, and Dijkstra is the fix. This guide builds it from that gap.

Every example runs in Python right in the page, so you can watch the distances settle as it runs.

How to read this

Read in order. Phase 1 shows exactly where BFS breaks and names the greedy idea that repairs it; phase 2 turns that idea into real code with a priority queue; phase 3 is where it lives in production, plus the one input that quietly breaks it.

The phases

  1. Why BFS Isn't Enough for Weighted Graphs 🟢 Basic - how a fewest-hops search picks a long road over a short detour, and the "always expand the closest unvisited node" idea that fixes it.
  2. Dijkstra with a Priority Queue 🟡 Intermediate - the full algorithm, walked step by step, implemented with a min-heap across seven languages.
  3. Where Dijkstra Runs the World 🟡 Intermediate - maps and network routing, A* as Dijkstra plus a heuristic, and why a single negative edge weight breaks the whole thing.