Routing algorithms: Dijkstra and Bellman-Ford
Filling in the link-state (Dijkstra) table step by step, updating distance-vector (Bellman-Ford) vectors round by round, count-to-infinity and poisoned reverse. The table-filling question on the midterm comes from here.
On the previous page we saw that a router looks at its forwarding table and forwards the packet. But who fills in that table? Routing algorithms. This is the network layer’s control plane: seeing the network as a graph and finding the least-cost path to every destination. The exam has two table-filling questions: the Dijkstra table and a distance-vector update. Golden rules:
- At each step Dijkstra adds the node outside N’ with the smallest distance. Whether it is a neighbour does not matter.
- Update the neighbours of the added node: .
- The forwarding table keeps only the first hop (next hop).
- Bellman-Ford: , where ranges over neighbours.
- In DV, bad news spreads slowly (count-to-infinity).
The network as a graph
Routers are nodes, links are edges. Each edge has a cost: delay, inverse bandwidth, or an administrator’s decision. The cost of a path is the sum of the edge costs.
| Link-state (LS) | Distance-vector (DV) | |
|---|---|---|
| Information | Every router knows the whole topology | Only its neighbours’ vectors |
| Algorithm | Dijkstra | Bellman-Ford |
| Messages | Flooding to everyone | Only to neighbours |
| Computation | Centralized (each router on its own) | Distributed, iterative, asynchronous |
| Problem | Oscillation with load-dependent costs | Count-to-infinity, slow convergence |
| Example protocol | OSPF, IS-IS | RIP |
Link-state: Dijkstra’s algorithm
Each router spreads its link states (link-state packets) to the whole network. Everyone has the same map and runs Dijkstra by itself.
Notation: is the lowest cost known so far from the source to , is the predecessor node on that path, and is the set of finalized nodes.
Start: N' = {source}; for each v, D(v) = c(source,v) (infinite if not a neighbour)
Loop : pick the smallest D(w) outside N', add it to N'
for each neighbour v of w outside N':
D(v) = min( D(v), D(w) + c(w,v) )
End : all nodes are in N'
Example: source A. Edges:
A-B 4 A-C 1 B-C 2 B-D 5 C-D 8
C-E 10 D-E 2 D-F 6 E-F 3
Each cell in the table has the form D,p. * shows that the node is finalized.
step N' B C D E F
0 A 4,A 1,A inf inf inf
1 AC 3,C * 9,C 11,C inf
2 ACB * * 8,B 11,C inf
3 ACBD * * * 10,D 14,D
4 ACBDE * * * * 13,E
5 ACBDEF * * * * *
In step 1, C (1) was added: for B, , and the predecessor became C. In step 2, B (3) was added: for D, . In step 3, D (8): for E, . In step 4, E (10): for F, .
The result and A’s forwarding table:
| Destination | Cost | Path | Next hop |
|---|---|---|---|
| B | 3 | A-C-B | C |
| C | 1 | A-C | C |
| D | 8 | A-C-B-D | C |
| E | 10 | A-C-B-D-E | C |
| F | 13 | A-C-B-D-E-F | C |
PITFALL · the direct link is not always the shortest. A-B directly is 4, but A-C-B is 3. While filling in the table, don’t take the values in the first row as “final”; a node is finalized only when it is added to N’.
TIP · finding the next hop. Trace the predecessor chain backward: F’s predecessor is E, E’s is D, D’s is B, B’s is C, C’s is A. The last node before reaching the source (C) is the next hop.
Complexity. For n nodes there are n - 1 rounds, and each round scans to find the smallest: . Oscillation: if the cost depends on traffic load, everyone switches to the cheap path at the same time, that path becomes expensive, and everyone switches back. The fix: randomly shift the routers’ computation times.
TIP · filling the table quickly on the exam. In each row do three things: (1) circle the smallest value outside N’, (2) write that node into the set N’, (3) update only the cells of its neighbours and copy the other cells unchanged. If there is a tie, the one you pick does not change the result, but be consistent (usually alphabetical). Trying to recompute a cell that doesn’t change is a waste of time.
Message complexity. In LS each router spreads its own link state to the whole network; for n routers and E links that is messages. In DV, messages go only between neighbours, but the convergence time varies and loops can form. In terms of robustness LS is better: since each router does its own computation, a router’s error stays limited.
Distance-vector: Bellman-Ford
Each router keeps only its distance vector: the estimated cost to each destination. When it receives vectors from its neighbours, it renews its own vector with the Bellman-Ford equation:
If the vector changed, it sends it to its neighbours. If it did not, it stays silent. That is why the algorithm is iterative, asynchronous and distributed.
Example: four nodes, synchronous rounds. Edges: W-X 1, X-Y 3, W-Y 6, Y-Z 2, X-Z 9. Rows are the node’s vector, columns are destinations W X Y Z.
t=0 (direct links only)
D_W 0 1 6 inf
D_X 1 0 3 9
D_Y 6 3 0 2
D_Z inf 9 2 0
t=1
D_W 0 1 4 8 Y: min(6, 1+3)=4 Z: min(inf, 1+9, 6+2)=8
D_X 1 0 3 5 Z: min(9, 3+2)=5
D_Y 4 3 0 2
D_Z 8 5 2 0
t=2
D_W 0 1 4 6 Z: min(1+5, 6+2)=6 (X's new vector)
D_X 1 0 3 5
D_Y 4 3 0 2
D_Z 6 5 2 0
t=3: no vector changed, converged
W’s best path to Z is W-X-Y-Z, with cost . This information arrived in two rounds, because it is a three-hop path.
Count-to-infinity and poisoned reverse
When a cost decreases, the news spreads quickly. When it increases, there is trouble. Example: A-B 2, B-C 1, A-C 30. In the converged state , (C goes to A via B). Now the A-B cost becomes 40.
B: min(40, 1 + D_C(A)=3) = 4 B doesn't know that C's path goes through B itself
C: min(30, 1 + 4) = 5
B: min(40, 1 + 5) = 6
C: 7, B: 8, ... grows by 2 each round
...
B: 30, C: min(30, 31) = 30 C finally switched to the direct link
B: min(40, 1 + 30) = 31 stable
The stable state was reached in 15 rounds (in each round B updates first, then C). For almost this whole time, B and C were in a loop pointing at each other. Without the A-C link, the numbers would climb forever; that is where the name comes from.
Poisoned reverse: if C’s path to A goes through B, C tells B . B immediately gets and no loop forms. However, this trick is not enough in loops of three or more nodes. This is why RIP defines infinity as 16 hops.
Hierarchical routing
In the real Internet there are hundreds of millions of routers; they can’t all run one Dijkstra. Also, each organization wants to manage its own network. The solution: autonomous systems (AS). Inside an AS an intra-AS protocol (OSPF, RIP) runs, and between ASes an inter-AS protocol (BGP). We cover this on the next page.
Common mistakes
| Wrong idea | Correct |
|---|---|
| In Dijkstra, adding the neighbour first | The node with the smallest D value is added |
| The direct cost in the first row is final | It is final when the node enters N’ |
| Next hop = the destination’s predecessor | Trace the chain back to the source’s neighbour |
| In Bellman-Ford, choosing the cheapest link | The smallest of the sums |
| In DV every router knows the topology | It knows only its neighbours’ vectors |
| DV converges in one round | A k-hop path may need k rounds |
| Count-to-infinity happens in LS | It is a DV problem; LS’s problem is oscillation |
| Poisoned reverse solves every loop | Only loops of two nodes |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Run Dijkstra's algorithm from node S on the network below. What is the least-cost path cost from S to W?
S-T 3 S-U 7 T-U 2 T-V 6
U-V 1 U-W 8 V-W 2Show answer
Start: T 3, U 7, V and W infinite.
Step 1: the smallest, T (3), is added. U = min(7, 3+2) = 5, V = 3+6 = 9.
Step 2: U (5) is added. V = min(9, 5+1) = 6, W = 5+8 = 13.
Step 3: V (6) is added. W = min(13, 6+2) = 8.
Step 4: W (8) is added. The path is S-T-U-V-W, with cost 3+2+1+2 = 8.
In the same network, in what order are the nodes added to the set N' by Dijkstra's algorithm (starting from S)?
- S, T, U, V, W
- S, T, V, U, W
- S, U, T, V, W
- S, T, U, W, V
Show answer
At each step the node outside N' with the smallest value is added: T (3), U (5), V (6), W (8). Being a direct neighbour does not affect the order; only the current distance decides.
In the same network, what is the next hop in S's forwarding table for destination W?
- T
- U
- V
- W
Show answer
The predecessor chain: p(W) = V, p(V) = U, p(U) = T, p(T) = S. Trace backward to the node right next to S: T. The forwarding table keeps only the first step; the paths to T, U and V all start from T.
Node x has neighbors a, b, c with link costs c(x,a) = 2, c(x,b) = 5, c(x,c) = 1. They advertise D_a(y) = 6, D_b(y) = 2, D_c(y) = 9. Using the Bellman-Ford equation, what is D_x(y)?
Show answer
.
Via a: 2 + 6 = 8, via b: 5 + 2 = 7, via c: 1 + 9 = 10. The smallest is 7, and the next hop is b. The cheapest link (c) does not win; the total is what matters.
In the network below every node starts with only its direct link costs and all nodes exchange vectors synchronously. What is D_P(T) after the first exchange, and after convergence?
P-Q 2 P-R 7 Q-R 1 R-T 3 Q-T 8Show answer
At t = 0, P has no path to T (infinite). The neighbours' vectors: , .
t = 1: . In the same round Q also updated: .
t = 2: P receives the new vector: . There is no change in the next round. The path is P-Q-R-T, with cost 6.
What is the worst-case time complexity of the basic (array-based) Dijkstra's algorithm on a network of n nodes?
- O(n)
- O(n log n)
- O(n^2)
- O(2^n)
Show answer
There are n - 1 rounds, and in each round all nodes outside N' are scanned: , that is . With a heap it becomes .
Which statement about link-state (LS) and distance-vector (DV) algorithms is TRUE?
- In DV, every node knows the full topology
- In LS, each node computes its own table, so an incorrect link cost advertised by one router has limited effect
- LS can suffer from count-to-infinity
- DV requires flooding link-state packets to all routers
Show answer
In LS every router knows the whole topology and computes its path itself. A faulty router can only advertise the cost of its own links incorrectly. In DV, if a router wrongly says "I can reach everywhere cheaply", the error spreads from neighbour to neighbour. Count-to-infinity is a DV problem; flooding is LS's method.
Which problem is the distance-vector algorithm known for when a link cost increases?
- Routing oscillation due to load-sensitive costs
- Count-to-infinity
- Fragmentation
- Broadcast storm
Show answer
Good news (a cost decrease) spreads fast, bad news (an increase) slowly. Two routers take each other for the old path and raise the cost a little in every round. Load-sensitive oscillation is an LS problem.
Links: A-B = 2, B-C = 1, A-C = 30. All tables have converged. Then the cost of A-B rises to 40. Without poisoned reverse, what value does D_B(A) finally settle at?
Show answer
Before the change, and (C goes to A via B). When the link becomes 40, B trusts C's old value of 3: . C looks at this and says 5, B says 6, C says 7... It grows by 2 in every round. It stops when C's value reaches the direct link (30). Final state: , . In our round model (B first, then C) this takes 15 rounds.
With poisoned reverse, if C's least-cost path to A goes through B, what does C advertise to B as D_C(A)?
- Its real cost D_C(A)
- 0
- Infinity
- The cost of link B-C
Show answer
If C goes to A via B, it tells B "my distance to A is infinity". B can then no longer choose a looping path that goes back to C. In the example above, B immediately gets . In loops of three or more nodes, poisoned reverse may not be enough.
Why does Dijkstra's algorithm require non-negative link costs?
- Because routers cannot store negative numbers
- Because once a node is added to N' its cost is assumed final, and a negative edge found later could make a cheaper path
- Because Bellman-Ford also fails on negative costs
- Because negative costs create count-to-infinity
Show answer
Dijkstra's greedy step rests on the assumption that "the distance inside N' is final". A negative edge breaks this assumption. Since costs in networks are positive anyway, it is not a problem in practice.
Verification: All Dijkstra tables and DV vectors were taken from the output of Dijkstra and synchronous Bellman-Ford implementations written in node (verify/SEN2008/06.mjs).