AlgoPlusAlgoPlus
Learn/Networking
Lesson

Link-State Routing

Every router floods a map of its own links, then each runs Dijkstra independently to compute loop-free shortest paths.

9 min read Watch it move Build it

Link-state routing solves a hard problem cleanly: how do thousands of routers, each seeing only its own neighbours, agree on consistent paths across the whole network? The trick is to *separate* two jobs. First, give every router an identical map of the network. Then let each router selfishly compute its own shortest paths on that shared map. Because everyone computes from the same truth, the routes stitch together with no loops. The most widely deployed real-world example is OSPF.

Phase 1 — flood the map

Each router knows only one local fact: which neighbours it connects to and the cost of each link. It packages that fact into a link-state advertisement (LSA) and floods it — passing it to every neighbour, who pass it onward, until every router in the network has a copy. After flooding settles, each router has assembled the same link-state database: a full map of every link and cost.

Why flooding works
Each LSA carries a sequence number, so routers forward only newer advertisements and drop duplicates. That stops the flood from looping forever and guarantees everyone converges on the *same* database.

Phase 2 — each router runs Dijkstra

With an identical map in hand, every router independently runs Dijkstra's algorithm, treating *itself* as the source. It repeatedly settles the closest router not yet finalized, then relaxes that router's neighbours — lowering their tentative distance whenever a cheaper path is found. The result is a shortest-path tree: the cheapest route from this router to every other one.

  1. 1Discover your own links and their costs.
  2. 2Flood that as an LSA so every router gets it.
  3. 3Assemble the identical link-state database (the full map).
  4. 4Run Dijkstra from yourself: settle the nearest unsettled router, then relax its neighbours.
  5. 5Read off the next hop toward each destination from your shortest-path tree.
Link-state vs distance-vector
Distance-vector routers share only *their own distances* with direct neighbours and can be slow to converge (count-to-infinity). Link-state routers share the *topology itself* with everyone, so each computes paths locally and converges fast.
OperationTimeSpace
Flooding an LSA · every link forwards it onceO(E)O(V + E)
Dijkstra per router · with a binary heapO((V + E) log V)O(V)
Check yourself
Why do link-state routers produce loop-free paths without coordinating their final routing tables?