Kestrel Exams kestrel logo KESTREL EXAMSPRACTICE TO PASS
Networking13 min read

Link-State Routing and Dijkstra’s Algorithm, Step by Step

In link-state routing every router first collects a complete map of the network, then runs Dijkstra’s algorithm on it to find the cheapest path to every destination. Exams ask you to fill in its table by hand, so this guide does one in full.

Practice this topic in the free app →All Networking guides

Chapter 5 · Routing algorithms
Built from

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.

How to use it

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#

TermWhat it means
GraphA 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).
NeighborTwo nodes are neighbors if one edge joins them directly.
Least-cost pathThe path between two nodes whose link costs add up to the smallest total.
Link-state broadcastEvery node sends the costs of its own links to every other node, so all nodes end up with the same full map.
SourceThe 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.

LinkCostLinkCost
A–B2C–G8
A–C1E–F3
B–C2E–G2
B–G5F–G6
C–E10

Three ways to classify a routing algorithm

QuestionOne kindThe 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.
Do not confuse these
“Centralized” does not mean one computer does the work. In link-state routing every router runs Dijkstra itself. It only means each router starts with the complete map.

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.

Where you will see this again

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′:

  1. 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.
  2. 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.

StepN′D(B), p(B)D(C), p(C)D(E), p(E)D(F), p(F)D(G), p(G)
0{A}2, A1, A∞∞∞
1{A, C}2, A11, C∞9, C
2{A, C, B}11, C∞7, B
3{A, C, B, G}9, G13, 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.

Worked example
Step 2, in detail. After step 1, the unsettled nodes have D(B) = 2, D(E) = 11, D(F) = ∞ and D(G) = 9. The smallest is B, so B joins N′ with final cost 2. B’s unsettled neighbor is G, joined by a link of cost 5. Going through B costs D(B) + c(B, G) = 2 + 5 = 7. That beats the old 9 (through C), so D(G) becomes 7 and p(G) becomes B. B’s other neighbors, A and C, are already settled.

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:

DestinationLeast-cost pathCostSend out of link
BA → B2(A, B)
CA → C1(A, C)
EA → B → G → E9(A, B)
FA → B → G → E → F12(A, B)
GA → B → G7(A, B)
Do not confuse these
A D value is only final once its node joins N′. D(G) was 9 after step 1 and dropped to 7 after step 2. Notice too that A reaches E for 9 through B and G, even though C has a direct link to E: that route costs 1 + 10 = 11. Cheapest is not always the fewest hops.

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”.

Worked example

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.

  1. 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.
  2. Every router reruns Dijkstra. The bottom is cheaper, so all the traffic moves there. Now the bottom costs 10 and the top costs 0.
  3. 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.

One sentence to carry into the exam

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.

  1. 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.
  2. 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).
  3. 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?
  4. 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?
  5. 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?
  6. Explain why link costs that follow traffic load can make routes oscillate, and give one fix.

Solutions#

Best study method

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.

  1. Centralized (global). Each router runs it itself, but only after link-state broadcast has given it the complete map: every link and its cost.
  2. 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.
  3. 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.
  4. 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.
  5. 210. There are n = 20 nodes besides the source, so the total is n(n + 1) / 2 = 20 × 21 / 2 = 210.
  6. 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.
One last rule

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.

Suggest a change

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 →