Sub topic 20
Link-state routing and Dijkstra's algorithm
This chapter assumes you know what a router is, how weighted graphs work (nodes, edges, path cost), and the idea of a forwarding table from the earlier chapters on longest-prefix match and on forwarding versus routing. It also assumes you have seen distance-vector routing, because link-state is easiest to understand as a contrast with it.
The core idea
In distance-vector routing, a router knows only what its neighbours tell it about distances. In link-state routing, every router builds the same complete map of the network and then computes shortest paths on that map by itself.
The work happens in four steps.
- Discover neighbours. Each router sends small "hello" packets on each link and learns who is on the other end.
- Measure link costs. The router assigns a cost to each link, such as one derived from bandwidth or delay.
- Flood link-state advertisements. The router packages its neighbour list and link costs into a link-state advertisement (LSA). It sends the LSA to every other router in the network.
- Compute shortest paths. Each router collects all the LSAs into a link-state database. It then runs Dijkstra's algorithm with itself as the source.
Because all routers hold the same database, they compute consistent results. The output of Dijkstra's algorithm is a shortest-path tree rooted at the router. From the tree, the router reads the first hop toward each destination and installs it in its forwarding table.
Flooding
Flooding means a router sends each new LSA out of every interface except the one it arrived on. The next router does the same. Two details keep this from looping forever.
- Each LSA carries a sequence number. A router keeps an LSA only if its sequence number is newer than the copy it already has. Older or duplicate copies are discarded and not forwarded again.
- Each LSA may carry an age field, so stale information eventually expires.
An LSA is sent when a router starts up, when a link changes state or cost, and periodically as a refresh. A link failure therefore reaches every router after about one flooding delay. This is why link-state converges faster than distance-vector and does not suffer from count-to-infinity.
Dijkstra's algorithm
Dijkstra's algorithm finds the shortest path from a source to every other node in a graph whose edge weights are non-negative. It keeps a set of nodes whose shortest distance is final and grows that set one node at a time.
The procedure:
- Set the distance of the source to 0 and every other node to infinity.
- Pick the node not yet finalised with the smallest tentative distance. Mark it final.
- For each neighbour of that node, check whether going through it gives a shorter path. If so, update the neighbour's distance and record the predecessor.
- Repeat from step 2 until every node is final.
The code below implements this with a priority queue. It uses the same four-node network as the Bellman-Ford example from the distance-vector chapter, so you can compare the answers.
import heapq
def dijkstra(graph, source):
dist = {n: float("inf") for n in graph}
dist[source] = 0
heap = [(0, source)]
while heap:
d, u = heapq.heappop(heap)
if d > dist[u]:
continue
for v, w in graph[u].items():
if d + w < dist[v]:
dist[v] = d + w
heapq.heappush(heap, (dist[v], v))
return dist
graph = {
"A": {"B": 2, "C": 5},
"B": {"A": 2, "C": 1, "D": 4},
"C": {"A": 5, "B": 1, "D": 1},
"D": {"B": 4, "C": 1},
}
print(dijkstra(graph, "A"))The output is {'A': 0, 'B': 2, 'C': 3, 'D': 4}. The line if d > dist[u]: continue skips stale heap entries, which appear when a node's distance is improved after it was already pushed.
Worked trace from A
The table shows the tentative distances after each node is finalised. The notation d(pred) gives the distance and the predecessor.
| Step | Finalised | A | B | C | D |
|---|---|---|---|---|---|
| 0 | none | 0 | inf | inf | inf |
| 1 | A | 0 | 2(A) | 5(A) | inf |
| 2 | B | 0 | 2(A) | 3(B) | 6(B) |
| 3 | C | 0 | 2(A) | 3(B) | 4(C) |
| 4 | D | 0 | 2(A) | 3(B) | 4(C) |
In step 2, B is finalised because 2 is the smallest tentative distance. Going through B lowers C from 5 to 3 and gives D a first value of 6. In step 3, C lowers D from 6 to 4. Following the predecessors back gives the tree: A to B, B to C, C to D.
Router A's forwarding table then needs only the first hop for each destination. B, C and D are all reached through B, because every shortest path starts with the edge A to B.
| Destination | Cost | Next hop |
|---|---|---|
| B | 2 | B |
| C | 3 | B |
| D | 4 | B |
Complexity
With an array and a linear scan for the minimum, Dijkstra's algorithm takes O(V²) time for V nodes. With a binary heap it takes O((V + E) log V). GATE questions often ask for these bounds, and for the V² version on a dense graph. Bellman-Ford takes O(V·E), so Dijkstra's algorithm is faster, but it needs the whole graph in one place. That requirement is the reason link-state routing needs flooding.
Common errors
- Negative weights. Dijkstra's algorithm can give wrong answers if any edge is negative. Link costs in routing are positive, so this does not arise in practice, but it matters in exam questions that compare algorithms.
- Forgetting that tentative values change. A node's distance is final only when the node is picked as the minimum, not when it first receives a value. In the trace above, D is 6 at step 2 and 4 at step 3.
- Reporting the whole path instead of the next hop. A forwarding table stores only the next hop. Each router along the way looks up the next step on its own.
- Assuming each router sees a different graph. All routers have the same database. They differ only in which node is the source of the computation.
- Tie-breaking. When two paths cost the same, the tree is not unique. Exam questions usually avoid ties or state a rule. Some protocols allow load-sharing across equal-cost paths.
Costs and trade-offs
| Aspect | Link-state | Distance-vector |
|---|---|---|
| What a router learns | Whole topology | Only distances from neighbours |
| Information sent | Own links, to everyone | Whole table, to neighbours |
| Computation | Dijkstra at each router | Bellman-Ford distributed |
| Convergence | Fast | Slower, count-to-infinity risk |
| Memory and CPU | Higher | Lower |
Link-state costs more memory and processing, because each router stores the full database and reruns Dijkstra when it changes. It also exposes a risk: if routers hold different databases for a short time, they may forward packets in a loop until flooding finishes. OSPF, covered in a later chapter, applies link-state routing inside one autonomous system and splits large networks into areas to limit the size of the database and the flooding.
Exercise
A router X has links X–Y (cost 1), X–Z (cost 4), Y–Z (cost 2) and Y–W (cost 6), and Z–W (cost 1). Run Dijkstra's algorithm from X. What are the shortest distances to Y, Z and W, and what is X's next hop for W?
Show answer
Start with X = 0. Finalise X: Y = 1, Z = 4. Finalise Y (1): Z becomes 1 + 2 = 3, W becomes 1 + 6 = 7. Finalise Z (3): W becomes 3 + 1 = 4. Finalise W (4). The distances are Y = 1, Z = 3, W = 4. The path to W is X to Y to Z to W, so the next hop for W is Y.
Exercise
Why does link-state routing not suffer from count-to-infinity, even though routers react to a link failure at different times?
Show answer
Every router receives the failed link's new LSA by flooding and recomputes shortest paths from a complete, shared map. It does not rely on distance estimates that other routers computed from older information, so bad routes are not passed around and slowly incremented. Short-lived loops can occur until flooding finishes, but the distances do not count upward.