AlgoViz
← Back to trail

Routing (shortest path)

Medium+15 XP

How the internet picks a route: the fastest path, not the fewest hops. Watch routers find the lowest-latency way across a mesh.

No visualization loaded.

Watch

i

Press Run to begin.

A packet leaving your computer rarely takes the straightest line — it takes the FASTEST one. Routers cooperate to find the path with the lowest total latency across a mesh of links. Press Run and watch them settle the closest router each step, updating routes as a cheaper way appears — and see the winning path light up at the end.

Why isn't the route just the fewest hops?

Because some links are slow. Three quick hops can beat one slow one. In the mesh here, the direct route through A is fewer hops but 9ms; the detour through B and C is more hops but only 6ms — so the routers pick the detour. On the internet, distance and congestion matter more than hop count.

Isn't this just Dijkstra's algorithm?

It IS — that's the point. The shortest-path idea you learned abstractly is exactly how routing protocols decide where to send packets, with latency (or cost) as the edge weight. Seeing it on a router mesh is seeing Dijkstra do a real job: keep the cheapest total route, settling one router at a time.

Does every router really know the whole map?

Routers share what they know with their neighbors, and over time each builds a picture of the cheapest way to reach the rest of the network — then forwards each packet to the best next hop. The animation shows the 'find the cheapest path' brain; in reality that knowledge is spread across many routers updating each other.

What happens when a link gets slow or breaks?

The weights change, so the cheapest path changes — and routers re-route. A link that was fastest a minute ago might be congested now, and traffic shifts to a better path. That constant re-deciding is why the internet flows around traffic jams and outages instead of getting stuck.

🧠Routing finds the lowest-cost path, not the fewest hops — it's Dijkstra with latency as the weight. Routers settle the closest one first, update routes when a cheaper way appears, and the winning path can take more hops yet still be faster.
📈

How the work grows

input size n →work ↑
Time
O((V+E) log V) with a heap
Space
O(n) — best-route table per router

O((V+E) log V) with a heap is good — the practical sorting speed. It's Dijkstra's shortest-path: each router is finalized once and each link is checked once, with a small log factor for always grabbing the cheapest-known router next. The space holds each router's best-known distance to the destination.

Faint dotted lines = O(1) (flat) and O(n) (straight) for comparison.

router being settledneighbor: route improvedneighbor: no improvementthe chosen fastest path