Kurose and Ross, Computer Networking: A Top-Down Approach, sections 5.1 and 5.2.1, cross-checked against RFC 2328 (OSPF Version 2), the standard link-state protocol. Every number in the worked tables was recomputed in code.
Copy the worked table onto paper and redo it with the page covered. Dijkstra questions are marked on the table, so write every row, not just the final answer.
The picture: everyone gets the same map#
Picture six friends in six towns. Each friend gets the same printed road map, showing every road and how many minutes it takes. Nobody asks for directions. Each friend works out the fastest way from their own town to every other town.
That is link-state routing. The towns are routers, the roads are links, and the minutes are link costs. First everyone gets the same complete map. Then each router works out its own best routes from it.
The method each friend uses is Dijkstra’s algorithm. Like a ripple in a pond, it settles the nearest town first, then the next nearest, until every town is settled.
Vocabulary, before anything uses it#
| Term | What it means |
|---|---|
| Graph | A set of nodes (here, routers) joined by edges (here, links). Written G = (N, E): N is the set of nodes and E the set of edges. |
| Link cost, c(x, y) | A number on the edge between nodes x and y. Lower is better. It might stand for delay, money, or 1 per hop. If x and y are not joined, c(x, y) = ∞ (infinity). |
| Neighbor | Two nodes are neighbors if one edge joins them directly. |
| Least-cost path | The path between two nodes whose link costs add up to the smallest total. |
| Link-state broadcast | Every node sends the costs of its own links to every other node, so all nodes end up with the same full map. |
| Source | The node running the algorithm, often written u. It finds least-cost paths from itself to every other node. |
| D(v) | The cost of the cheapest path from the source to node v found so far, in the current step. |
| p(v) | The predecessor of v: the node just before v on the cheapest path found so far. |
| N′ | Read “N prime”. The set of nodes whose least-cost path is known for certain. It starts with just the source and grows by one node each step. |
Routing as a graph problem, and the kinds of routing algorithm#
A routing algorithm is a method for finding good paths through the network. Underneath, every one solves the same problem: find the least-cost path in a graph. The cost of a path is the sum of the link costs along it. If every link costs 1, the least-cost path is simply the one with the fewest hops.
Here is the network this guide uses. It has six routers (A, B, C, E, F and G) and nine links. There is no router D, so the letter D only ever means a cost. All costs are the same in both directions.
| Link | Cost | Link | Cost |
|---|---|---|---|
| A–B | 2 | C–G | 8 |
| A–C | 1 | E–F | 3 |
| B–C | 2 | E–G | 2 |
| B–G | 5 | F–G | 6 |
| C–E | 10 |
Three ways to classify a routing algorithm
| Question | One kind | The other kind |
|---|---|---|
| Who knows the whole map? | Centralized (global): every node starts with the complete map. Link state is this kind. | Decentralized: each node knows only its own links and swaps estimates with neighbors. Distance vector is this kind. |
| How often do routes change? | Static: routes change slowly, often by hand. | Dynamic: routes are recomputed when traffic or the network’s shape changes. |
| Do costs follow traffic? | Load-sensitive: a link’s cost rises when it is busy. | Load-insensitive: cost ignores current load. Today’s main Internet routing protocols are this kind. |
How every router gets the map: link-state broadcast#
Dijkstra needs the whole map, so the routers must build it first. Each router knows only the links attached to it and their costs. It packs that list into a link-state packet (also called a link-state advertisement, LSA) and sends it to every other router in the network. Each router passes these packets on to its neighbors, a process called flooding, until every router has a copy from every other router.
When the flooding is done, every router holds an identical map. Each one then runs Dijkstra with itself as the source. Because the maps match, their answers fit together.
OSPF (Open Shortest Path First), the standard link-state protocol, works this way. It gets its own guide later in chapter 5.
Dijkstra’s algorithm, worked in full#
The source here is A. The algorithm has a start and a loop.
Start. N′ = {A}. For each other node v, set D(v) = c(A, v) if v is a neighbor of A, and D(v) = ∞ if not. Set p(v) = A for each neighbor.
Loop, until every node is in N′:
- Among the nodes not yet in N′, pick the one with the smallest D. Call it w. Add w to N′. Its D(w) is now final.
- For every neighbor v of w that is not in N′, check whether going through w is cheaper:
D(v) = min( D(v), D(w) + c(w, v) )
In words: keep the old cost, or reach w and then take the link to v, whichever is smaller. If going through w wins, also set p(v) = w.
Here is the whole run from A. Each cell shows D(v), p(v). A blank cell means that node is already in N′, so its value is final and no longer changes.
| Step | N′ | D(B), p(B) | D(C), p(C) | D(E), p(E) | D(F), p(F) | D(G), p(G) |
|---|---|---|---|---|---|---|
| 0 | {A} | 2, A | 1, A | ∞ | ∞ | ∞ |
| 1 | {A, C} | 2, A | 11, C | ∞ | 9, C | |
| 2 | {A, C, B} | 11, C | ∞ | 7, B | ||
| 3 | {A, C, B, G} | 9, G | 13, G | |||
| 4 | {A, C, B, G, E} | 12, E | ||||
| 5 | {A, C, B, G, E, F} |
The bold entry in each row is the smallest unsettled D: the node added in the next step.
From the table to a forwarding table
To read the path to any node, follow the predecessors backwards. For F: p(F) = E, p(E) = G, p(G) = B, p(B) = A. So the path is A → B → G → E → F, with cost 2 + 5 + 2 + 3 = 12. Doing this for every node gives the shortest-path tree: the links A–B, A–C, B–G, E–G and E–F.
A router only needs the first hop of each path, because the next router makes its own decision. So A’s forwarding table is:
| Destination | Least-cost path | Cost | Send out of link |
|---|---|---|---|
| B | A → B | 2 | (A, B) |
| C | A → C | 1 | (A, C) |
| E | A → B → G → E | 9 | (A, B) |
| F | A → B → G → E → F | 12 | (A, B) |
| G | A → B → G | 7 | (A, B) |
How much work it takes, and the oscillation problem#
The cost: O(n²)
Say there are n nodes besides the source. To find the smallest D, the first step looks through all n of them, the next step n − 1, and so on down to 1. The total is n(n + 1) / 2. That grows like n², so the worst case is written O(n²), read “order n squared”.
In our network there are 5 nodes besides A, so the algorithm looks at 5 + 4 + 3 + 2 + 1 = 5 × 6 / 2 = 15 nodes in total. Keeping the unsettled nodes in a data structure called a heap makes each “find the smallest” step faster.
The oscillation problem
Trouble starts when link costs are load-sensitive, meaning a link’s cost equals how much traffic it is carrying. Picture two routes from S to T: a top route and a bottom route. S sends 10 units of traffic.
- Both routes start empty, so both cost 0, and the tie is broken toward the top route. Now the top costs 10 and the bottom costs 0.
- Every router reruns Dijkstra. The bottom is cheaper, so all the traffic moves there. Now the bottom costs 10 and the top costs 0.
- Every router reruns Dijkstra again, and all the traffic swings back to the top.
The routes flip back and forth and never settle. That is oscillation. One fix is not to make costs depend on load, though the textbook rejects this because routers then cannot steer around congestion. The better fix is to stop routers all rerunning at once, by having each send its link-state packets at a slightly random time.
In link-state routing every router floods its own link costs so that all routers share one complete map, then each runs Dijkstra from itself, settling the cheapest unsettled node each step and updating D(v) = min(D(v), D(w) + c(w, v)), which costs O(n²) and gives a shortest-path tree whose first hops become the forwarding table.
Practice#
Write out the whole Dijkstra table whenever a question involves one. The rows earn the marks.
- Classify Dijkstra’s algorithm as used in link-state routing: centralized or decentralized? Then say what information a router must have before it can start.
- Using the six-router network in this guide, run Dijkstra with G as the source. Give the final least cost to every other node and G’s forwarding table (which neighbor each destination is sent to).
- In the worked table from A, D(E) was 11 after step 1 and 9 after step 3. Which node caused the change, and what was the calculation?
- The cost of link E–G rises from 2 to 6, and A reruns Dijkstra. What are A’s new least costs to E and to F, and which link does A now use for E?
- A network has 21 routers. Using the simple version of Dijkstra, how many nodes in total does one router look at to find the smallest D across the whole run?
- Explain why link costs that follow traffic load can make routes oscillate, and give one fix.
Solutions#
Find the first line where your working split from the solution, and redo the problem from that line. Do not just read the answer and nod.
- Centralized (global). Each router runs it itself, but only after link-state broadcast has given it the complete map: every link and its cost.
- A 7, B 5, C 7, E 2, F 5. Forwarding: A, B and C go to B; E and F go to E. Start: D(B) = 5, D(C) = 8, D(E) = 2, D(F) = 6, D(A) = ∞. Settle E (2): F improves to 2 + 3 = 5 through E; C through E would be 12, worse than 8. B and F now tie at 5; either order gives the same answer. Settle B (5): A becomes 5 + 2 = 7 through B, and C improves to 5 + 2 = 7 through B. Settling F, A and C improves nothing. Paths: G→B→A, G→B, G→B→C, G→E, G→E→F.
- Node G, when it joined N′ in step 3. D(G) + c(G, E) = 7 + 2 = 9, which beats the old 11 (through C, 1 + 10), so D(E) became 9 and p(E) became G.
- E costs 11, F costs 13, and E is reached over link (A, C). Through B and G, E would now cost 7 + 6 = 13, so the route A → C → E at 1 + 10 = 11 wins. F is now best as A → B → G → F at 2 + 5 + 6 = 13.
- 210. There are n = 20 nodes besides the source, so the total is n(n + 1) / 2 = 20 × 21 / 2 = 210.
- Moving traffic changes the costs that chose the route. All routers move onto the cheaper path together, which makes it the expensive one, so they all move back next run. Fixes: do not make link costs depend on load, or make routers run (and send link-state packets) at randomized times so they do not all switch together (the preferred fix).
Test-readiness checklist#
- ☐ I can describe a network as a graph with link costs c(x, y), and say what c(x, y) = ∞ means.
- ☐ I can sort a routing algorithm by the three classifications: centralized or decentralized, static or dynamic, load-sensitive or not.
- ☐ I can explain how link-state broadcast gives every router the same map.
- ☐ I can fill in a full Dijkstra table with N′, D(v) and p(v), one row per step.
- ☐ I can read paths from the predecessors and turn them into a forwarding table.
- ☐ I can state the O(n²) cost and compute n(n + 1) / 2 for a given network.
- ☐ I can explain the oscillation problem and one way to avoid it.
In every Dijkstra row, do two things in order: first settle the unsettled node with the smallest D, then update only its unsettled neighbors.
Frequently asked questions#
What is link-state routing?
Every router floods the costs of its own links to all other routers, so each has the complete map. Each then runs a least-cost path algorithm, usually Dijkstra, from itself. OSPF is the standard example.
What do D(v), p(v) and N prime mean in Dijkstra's algorithm?
D(v) is the cost of the cheapest path from the source to v found so far. p(v) is the node just before v on that path. N prime is the set of nodes whose least-cost path is already final.
What is the time complexity of Dijkstra's algorithm in link-state routing?
In its simple form it is O(n squared). With n nodes besides the source, it looks at n, then n minus 1, down to 1 node to find each minimum, which totals n(n + 1) / 2. A heap-based version is faster.
How do you get a forwarding table from Dijkstra's algorithm?
Follow the predecessors back from each destination to find its least-cost path, then keep only the first hop. The forwarding table maps each destination to the link toward that first-hop neighbor.
Something here not clear? A topic you wish we covered? Tell us. We read every message, and a request is the fastest way to get a guide written.
Test yourself on this topic
The free networking practice app has 10 questions on this guide and 500 across the whole course, each with an explanation for every answer. Offline, no ads, no account.
Practise this topic →