Link state routing — OSPF, Dijkstra
WHY does link state exist?
WHAT is link state routing? (the 5 steps)
OSPF (Open Shortest Path First) is the Internet's standard intra-domain (IGP) implementation of this idea: an open standard, runs directly over IP (protocol 89), supports areas, authentication, and equal-cost multipath.
HOW does flooding stay correct?
Dijkstra's algorithm — derived from scratch
The procedure:
- , , .
- Pick with minimum ; add to .
- Relax every edge out of .
- Repeat until = all nodes.
Complexity with a binary heap: .

Worked Example 1 — small graph
Graph (undirected, costs): A–B = 2, A–C = 5, B–C = 1, B–D = 7, C–D = 3. Source = A.
| Step | Finalized | d[A] | d[B] | d[C] | d[D] |
|---|---|---|---|---|---|
| init | {} | 0 | ∞ | ∞ | ∞ |
| pick A | {A} | 0 | 2 | 5 | ∞ |
| pick B (min=2) | {A,B} | 0 | 2 | 3 (via B:2+1) | 9 (2+7) |
| pick C (min=3) | {A,B,C} | 0 | 2 | 3 | 6 (3+3) |
| pick D (min=6) | all | 0 | 2 | 3 | 6 |
- Why pick B first? Min tentative = 2 → finalized. Why this step? Greedy invariant guarantees no shorter path to B exists.
- Why does C drop 5→3? Relaxing B's edge: . Why? Path A→B→C beats direct A→C.
- Why D = 6 not 9? After C finalized, relax C→D: . Why? Going via C is cheaper than via B.
Final shortest paths from A: B(2), C(3, via B), D(6, via B,C).
Worked Example 2 — what OSPF stores
Suppose router A's table after Dijkstra: to reach D, the first hop is B (because the path is A→B→C→D).
- Why store only next hop, not whole path? Why this step? Forwarding is hop-by-hop; A only needs to know which neighbor to hand the packet to. B will know its own next hop, etc. This is the destination → next-hop forwarding table.
Common mistakes
Active recall
Recall Test yourself (hide answers)
- What 5 steps make up link state routing?
- Why does Dijkstra need non-negative weights?
- What does an LSP contain?
- Why does flooding need sequence numbers?
- What does OSPF run on top of (protocol)?
What information does each router flood in link state routing?
Which algorithm does each router run on the assembled topology?
State the relaxation rule in Dijkstra.
Why must Dijkstra's edge weights be non-negative?
What problem of distance-vector does link state avoid?
What does OSPF stand for and where does it run?
Why do LSPs carry sequence numbers and age?
Time complexity of Dijkstra with a binary heap?
In the forwarding table, what does each router actually store?
LS vs DV in one line each?
Recall Feynman: explain to a 12-year-old
Every router sends a tiny note to all the others saying "here are the roads next to me and how long they are." After everyone reads everyone's notes, each one draws the whole map of the town. Then, using a clever rule, each router figures out the shortest way to every place — always picking the closest unfinished spot next, like exploring a maze by always stepping to the nearest unexplored room first. Because everyone uses the same map, nobody sends packets in circles.
Connections
- Distance Vector Routing — the alternative (Bellman–Ford, count-to-infinity)
- Dijkstra's Algorithm — the graph algorithm reused here
- Bellman–Ford Algorithm — needed when weights can be negative
- Flooding — the reliable LSP distribution mechanism
- OSPF Areas — scaling link state to large networks
- BGP — inter-domain routing (path-vector, contrasts with intra-domain OSPF)
- Routing Table vs Forwarding Table
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, link state routing ka core idea simple hai: har router apne aas-paas ke neighbours aur unke link ka cost ek chhoti packet (LSP) mein likh kar poore network mein flood kar deta hai. Matlab har router ke paas ek hi jaise complete map aa jaata hai. Iske baad har router khud, akele, Dijkstra algorithm chala kar har destination tak ka shortest path nikaal leta hai. Ye distance-vector se alag hai — wahan routers sirf apne "best distance" gossip karte hain aur count-to-infinity ka problem aata hai. Yahan sab ke paas raw map hota hai, isliye convergence fast aur loop-free.
Dijkstra ki asli baat: tum source se start karte ho, sabki tentative distance infinity, source ki 0. Phir baar-baar sabse closest unfinished node ko "lock" (finalize) karte ho, aur uske edges ko relax karte ho — yaani check karte ho ki uske through jaana sasta hai kya: . Ye greedy step sirf isliye kaam karta hai kyunki saare weights non-negative hain. Agar negative weight ho toh Dijkstra galat answer dega — tab Bellman–Ford use karo.
OSPF (Open Shortest Path First) yahi link state idea ka real-world protocol hai, jo Internet ke andar (intra-domain) chalta hai, directly IP ke upar (protocol 89). HELLO packets se neighbours dhoondho, LSP banao, flood karo, phir Dijkstra. Exam aur interview dono mein ye sequence aur "kyun non-negative weights chahiye" wala point bahut puchha jaata hai, toh ye yaad rakhna.