Link-State and Distance-Vector Routing

We know from the router lesson that routing is the control-plane job of computing the forwarding tables — the map that forwarding then reads. But how, exactly, do a few thousand routers scattered across a network, none of whom can see the whole thing, cooperatively work out the shortest path to everywhere? There is no central mapmaker. Each router starts knowing only its own immediate neighbours and the cost of the links to them, and yet, within seconds, every router must know the best next hop toward every destination.

Two great algorithmic families solve this, and the split between them is one of the most beautiful either/ors in networking. In link-state routing, everyone learns the whole map and each computes shortest paths alone. In distance-vector routing, nobody ever sees the whole map; routers only trade distance estimates with their neighbours and the right answer emerges from the gossip. Same goal, opposite philosophies — "tell everyone the local facts" versus "tell your neighbours your conclusions."

The shared setting: a weighted graph

Both algorithms model the network as a weighted graph: routers are nodes, links are edges, and each edge has a cost (delay, inverse bandwidth, an admin's number). "Best path" means least total cost. Here is the small network we'll reason about throughout — hover the steps to build it up and reveal a shortest path.

Link-state: hand everyone the whole map

A link-state router's motto is tell the truth about your neighbourhood, to the entire network. Each router:

  1. Discovers its own links and their costs — the local facts it knows first-hand.
  2. Floods a small "link-state advertisement" (LSA) listing those links to every other router. Flooding means each LSA is passed on to all neighbours until everyone has a copy, so every router ends up holding the same complete map of the whole network.
  3. Runs Dijkstra's shortest-path algorithm locally on that map to compute the least-cost path — and hence the first hop — to every destination.

Because every router has identical, complete information, they all compute consistent routes. This is how OSPF (Open Shortest Path First), the workhorse of enterprise networks, operates. Dijkstra grows a tree of confirmed-shortest nodes outward from the source, always confirming the nearest not-yet-confirmed node next — on our graph it would confirm x (cost 1), then v (2), then y (2), and so on, until z is nailed at cost 4 via u→x→y→z.

Because the only thing each router announces is the state of its own links — who its neighbours are and what the links to them cost. It never announces routes or conclusions, just raw local facts. Every router then assembles the same jigsaw from everyone's link-state pieces and does its own thinking. Contrast this with distance-vector, where routers announce distances (their conclusions) rather than link facts — the names capture exactly what gets advertised.

Distance-vector: trust your neighbours' conclusions

Distance-vector routers never build a map. Each knows only the cost to its direct neighbours, and keeps a vector of its current best-known distance to every destination. Periodically it sends that whole vector to its neighbours, and updates its own estimates using what it hears. The update is the celebrated Bellman–Ford equation:

d_x(y) \;=\; \min_{v}\;\bigl\{\, c(x,v) + d_v(y) \,\bigr\}

Read it aloud: my cheapest distance from x to y is the best I can do over all my neighbours v — the cost to reach that neighbour, c(x,v), plus the neighbour's own best distance onward to y, d_v(y). It is astonishingly local: x consults only its link costs and its neighbours' latest reported distances — never the full topology. Yet iterate it, let vectors slosh back and forth, and the estimates converge to the true shortest paths. This is how RIP (Routing Information Protocol) works.

The demo runs one Bellman–Ford relaxation round at a router, then iterates to convergence on a line of routers, so you can watch the estimates ripple outward one hop per round:

// Distance-vector on a small graph. Adjacency: node -> {neighbour: linkCost}. const graph: Record<string, Record<string, number>> = { u: { v: 2, x: 1 }, v: { u: 2, x: 2, w: 3 }, x: { u: 1, v: 2, w: 3, y: 1 }, w: { v: 3, x: 3, z: 5 }, y: { x: 1, z: 2 }, z: { w: 5, y: 2 }, }; const nodes = Object.keys(graph); const INF = Infinity; // dist[a][b] = a's current best-known distance to b (its distance vector). const dist: Record<string, Record<string, number>> = {}; for (const a of nodes) { dist[a] = {}; for (const b of nodes) dist[a][b] = a === b ? 0 : INF; } // ONE relaxation round: every node applies Bellman-Ford using neighbours' vectors. function round(): boolean { let changed = false; const snapshot = JSON.parse(JSON.stringify(dist)); for (const x of nodes) { for (const y of nodes) { if (x === y) continue; let best = snapshot[x][y]; for (const v of Object.keys(graph[x])) { // over neighbours v const cand = graph[x][v] + snapshot[v][y]; // c(x,v) + d_v(y) if (cand < best) best = cand; } if (best < dist[x][y]) { dist[x][y] = best; changed = true; } } } return changed; } let r = 0; while (round()) { r++; const row = nodes.map((b) => `${b}:${dist.u[b] === INF ? "-" : dist.u[b]}`).join(" "); console.log(`after round ${r}, u's vector -> ${row}`); } console.log(`Converged in ${r} rounds. u -> z least cost = ${dist.u.z}`);

Watch u's distance to z improve round by round as the good news creeps in one hop at a time — first u learns its neighbours, then its neighbours' neighbours, until the true cost 4 settles. That "one hop per round" propagation is the crux of distance-vector's biggest weakness.

The dark side of distance-vector: count-to-infinity

Distance-vector's charm — routers trust neighbours' conclusions without seeing why — is also its curse. Good news travels fast; bad news travels agonisingly slowly. When a link fails, a router may still hear an old, now-invalid low distance echoed back from a neighbour who learned it through the failed router in the first place, and believe it. Each round they nudge their estimates up by one, bouncing the stale route between them — 4, 5, 6, 7… — creeping toward infinity. This is the notorious count-to-infinity problem, and it can leave a routing loop festering for many rounds after a failure.

Two partial cures are worth knowing:

The trap is assuming distance-vector reacts to a broken link as quickly as it reacts to a new, better one. It does not. When a shorter path appears, a single round of updates spreads the good news outward and everyone benefits immediately. But when a link breaks, a router can keep believing a phantom low-cost route because a neighbour is still parroting the old distance — a distance that was only ever valid via the now-dead link. The two mislead each other, incrementing 3→4→5→6… over many rounds until the count finally reaches the "infinity" threshold and the route is declared dead. Link-state does not suffer this: because every router holds the whole map, a failed link is simply erased from the map and Dijkstra instantly recomputes correct paths — no phantom echoes to chase. If you see slow convergence and transient loops after a failure, suspect a distance-vector protocol counting to infinity.

Head to head

The trade-offs fall out directly from what each algorithm knows and shares:

Link-state (OSPF)Distance-vector (RIP)
What each router knowsthe whole topology maponly distances via its neighbours
What it advertisesits own link states, flooded to allits full distance vector, to neighbours only
Local algorithmDijkstra (shortest-path tree)Bellman–Ford update
Convergencefast; a failure is erased from the map at oncecan be slow — count-to-infinity after failures
Message overheadsmall LSAs, but flooded network-widebigger vectors, but only to neighbours
Memorystores the entire map (heavier)stores only its own vectors (lighter)
Robustness to bad dataa router computes its own routes; harder to poisontrusts neighbours' conclusions; a lie propagates

Neither wins outright. Link-state converges fast and resists bad information, at the cost of every router storing the whole map and flooding updates. Distance-vector is lightweight and simple — perfect for small, stable networks — but pays for its locality with slow, loop-prone convergence when things break. Both are intra-domain protocols, run inside a single administrative network. Routing between networks answers to a completely different master — policy — which is the subject of BGP in the next lesson.