learn.aathan.in

Shortest Paths — Overview & Map

The shortest-path problem, the family of algorithms that solve it, and how to pick the right one.

Finding the shortest route between two points is one of the most useful things a computer does — it’s what powers map directions, network routing, game AI pathfinding, and a surprising number of problems that don’t look like maps at all (word ladders, puzzle solving, dependency resolution).

This page is the map of the territory. Each algorithm below has its own detailed page; start here to understand which one you actually need.

The problem, stated precisely

We have a graph: a set of nodes (also called vertices) connected by edges. Each edge may carry a weight — a cost, distance, or time to travel along it.

4 2 1 5 8 10 2 6 3 A B C D E F
A weighted graph. We'll reuse this exact graph across the algorithm pages so you can watch each one solve the same problem.

The shortest path from a start node ss to a target node tt is the sequence of edges connecting them with the minimum total weight. In the graph above, the cheapest way from A to F is A → C → B → D → E → F with total cost 2+1+5+2+3=132+1+5+2+3 = 13 — not the route with the fewest hops, and not the one that looks most direct.

Variants of the question

“Shortest path” is really several related problems:

VariantWhat you ask for
Single-pairShortest path from one ss to one tt (map directions)
Single-sourceShortest paths from one ss to every other node
All-pairsShortest paths between every pair of nodes

Most single-source algorithms also solve single-pair — you just stop early once tt is reached.

The family of algorithms

Which algorithm you reach for depends on two questions: are the edges weighted? and can weights be negative?

AlgorithmWorks onHandles negative weights?Typical usePage
BFSUnweighted graphsn/a (no weights)Fewest-hops path, mazesBFS
DijkstraNon-negative weights❌ NoThe default for road/network mapsDijkstra
Bidirectional DijkstraNon-negative weights❌ NoLong single-pair queries, faster in practiceBidirectional Dijkstra
A*Non-negative weights + a heuristic❌ NoGames, grids, geographic routingA*
Bellman–FordAny weights✅ Yes (and detects negative cycles)Currency arbitrage, routing protocolsBellman–Ford
Floyd–WarshallAny weights (all-pairs)✅ YesSmall graphs, all-pairs distances(mentioned below)

A decision guide

Are edges weighted?
├── No  ──────────────►  BFS
└── Yes
    ├── Any negative weights?
    │   ├── Yes ─────────►  Bellman–Ford  (or Floyd–Warshall for all-pairs)
    │   └── No
    │       ├── Need one s → t, large graph?
    │       │   ├── Have a good distance heuristic? ──►  A*
    │       │   └── No heuristic ───────────────────►  Bidirectional Dijkstra
    │       └── Need distances to everything?  ─────►  Dijkstra

Floyd–Warshall is the odd one out: instead of exploring outward from a start node, it fills a table of every node-to-node distance using dynamic programming in O(V3)O(V^3) time. It’s only practical for small graphs (a few hundred nodes), but when you genuinely need all-pairs distances and the graph is small, its three-nested-loop simplicity is unbeatable. For point-to-point queries on big graphs, prefer the outward-exploring algorithms above.

The one idea underneath most of them

Almost every algorithm here is built on edge relaxation. Each node keeps a “best distance found so far” estimate, starting at \infty for everything except the source (which is 00). Relaxing an edge uvu \to v with weight ww asks a single question:

if dist(u)+w<dist(v), then dist(v)dist(u)+w\text{if } \operatorname{dist}(u) + w < \operatorname{dist}(v) \text{, then } \operatorname{dist}(v) \leftarrow \operatorname{dist}(u) + w

In words: “Is going through uu a cheaper way to reach vv than anything I’ve found before? If so, record it.”

The algorithms differ mainly in the order they relax edges — greedily by smallest distance (Dijkstra), guided by a heuristic (A*), or exhaustively in rounds (Bellman–Ford). Understanding relaxation once makes all of them click.

Where to go next