SEN2008 · Week 6 · Network Layer Concepts (II): Routing Protocols OSPF, BGP, multicasting

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.

routing algorithmlink-stateDijkstra's algorithmdistance vectorBellman-Ford equationleast-cost pathforwarding tablenext hopcount-to-infinitypoisoned reversecontrol plane

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:

  1. At each step Dijkstra adds the node outside N’ with the smallest distance. Whether it is a neighbour does not matter.
  2. Update the neighbours of the added node: D(v)=min⁡(D(v), D(w)+c(w,v))D(v) = \min(D(v),\ D(w) + c(w,v)).
  3. The forwarding table keeps only the first hop (next hop).
  4. Bellman-Ford: Dx(y)=min⁡v{c(x,v)+Dv(y)}D_x(y) = \min_v \{c(x,v) + D_v(y)\}, where vv ranges over neighbours.
  5. 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)
InformationEvery router knows the whole topologyOnly its neighbours’ vectors
AlgorithmDijkstraBellman-Ford
MessagesFlooding to everyoneOnly to neighbours
ComputationCentralized (each router on its own)Distributed, iterative, asynchronous
ProblemOscillation with load-dependent costsCount-to-infinity, slow convergence
Example protocolOSPF, IS-ISRIP

Each router spreads its link states (link-state packets) to the whole network. Everyone has the same map and runs Dijkstra by itself.

Notation: D(v)D(v) is the lowest cost known so far from the source to vv, p(v)p(v) is the predecessor node on that path, and N′N' 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, 1+2=3<41 + 2 = 3 < 4, and the predecessor became C. In step 2, B (3) was added: for D, 3+5=8<93 + 5 = 8 < 9. In step 3, D (8): for E, 8+2=10<118 + 2 = 10 < 11. In step 4, E (10): for F, 10+3=13<1410 + 3 = 13 < 14.

The result and A’s forwarding table:

DestinationCostPathNext hop
B3A-C-BC
C1A-CC
D8A-C-B-DC
E10A-C-B-D-EC
F13A-C-B-D-E-FC

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: O(n2)O(n^2). 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 O(nE)O(nE) 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:

Dx(y)=min⁡v∈neighbors(x){ c(x,v)+Dv(y) }D_x(y) = \min_{v \in \text{neighbors}(x)} \{\, c(x,v) + D_v(y) \,\}

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 1+3+2=61 + 3 + 2 = 6. 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 DB(A)=2D_B(A) = 2, DC(A)=3D_C(A) = 3 (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 DC(A)=∞D_C(A) = \infty. B immediately gets min⁡(40,1+∞)=40\min(40, 1 + \infty) = 40 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 ideaCorrect
In Dijkstra, adding the neighbour firstThe node with the smallest D value is added
The direct cost in the first row is finalIt is final when the node enters N’
Next hop = the destination’s predecessorTrace the chain back to the source’s neighbour
In Bellman-Ford, choosing the cheapest linkThe smallest of the sums c(x,v)+Dv(y)c(x,v) + D_v(y)
In DV every router knows the topologyIt knows only its neighbours’ vectors
DV converges in one roundA k-hop path may need k rounds
Count-to-infinity happens in LSIt is a DV problem; LS’s problem is oscillation
Poisoned reverse solves every loopOnly loops of two nodes

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1medium

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 2
Show answer
8

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.

Question 2medium

In the same network, in what order are the nodes added to the set N' by Dijkstra's algorithm (starting from S)?

  1. S, T, U, V, W
  2. S, T, V, U, W
  3. S, U, T, V, W
  4. S, T, U, W, V
Show answer
A) S, T, U, V, W

At each step the node outside N' with the smallest DD 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.

Question 3medium

In the same network, what is the next hop in S's forwarding table for destination W?

  1. T
  2. U
  3. V
  4. W
Show answer
A) T

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.

Question 4easy

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
7

Dx(y)=min⁡v{c(x,v)+Dv(y)}D_x(y) = \min_v \{c(x,v) + D_v(y)\}.
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.

Question 5hard

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 8
Show answer
10 after one exchange, 6 after convergence

At t = 0, P has no path to T (infinite). The neighbours' vectors: DQ(T)=8D_Q(T) = 8, DR(T)=3D_R(T) = 3.
t = 1: DP(T)=min⁡(2+8,7+3)=10D_P(T) = \min(2 + 8, 7 + 3) = 10. In the same round Q also updated: DQ(T)=min⁡(8,1+3)=4D_Q(T) = \min(8, 1 + 3) = 4.
t = 2: P receives the new vector: min⁡(2+4,7+3)=6\min(2 + 4, 7 + 3) = 6. There is no change in the next round. The path is P-Q-R-T, with cost 6.

Question 6easy

What is the worst-case time complexity of the basic (array-based) Dijkstra's algorithm on a network of n nodes?

  1. O(n)
  2. O(n log n)
  3. O(n^2)
  4. O(2^n)
Show answer
C) O(n^2)

There are n - 1 rounds, and in each round all nodes outside N' are scanned: n+(n−1)+⋯≈n(n+1)/2n + (n-1) + \dots \approx n(n+1)/2, that is O(n2)O(n^2). With a heap it becomes O((n+m)log⁡n)O((n + m) \log n).

Question 7medium

Which statement about link-state (LS) and distance-vector (DV) algorithms is TRUE?

  1. In DV, every node knows the full topology
  2. In LS, each node computes its own table, so an incorrect link cost advertised by one router has limited effect
  3. LS can suffer from count-to-infinity
  4. DV requires flooding link-state packets to all routers
Show answer
B) In LS, each node computes its own table, so an incorrect link cost advertised by one router has limited effect

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.

Question 8easy

Which problem is the distance-vector algorithm known for when a link cost increases?

  1. Routing oscillation due to load-sensitive costs
  2. Count-to-infinity
  3. Fragmentation
  4. Broadcast storm
Show answer
B) Count-to-infinity

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.

Question 9hard

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
31

Before the change, DB(A)=2D_B(A) = 2 and DC(A)=3D_C(A) = 3 (C goes to A via B). When the link becomes 40, B trusts C's old value of 3: min⁡(40,1+3)=4\min(40, 1 + 3) = 4. 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: DC(A)=30D_C(A) = 30, DB(A)=min⁡(40,1+30)=31D_B(A) = \min(40, 1 + 30) = 31. In our round model (B first, then C) this takes 15 rounds.

Question 10medium

With poisoned reverse, if C's least-cost path to A goes through B, what does C advertise to B as D_C(A)?

  1. Its real cost D_C(A)
  2. 0
  3. Infinity
  4. The cost of link B-C
Show answer
C) Infinity

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 min⁡(40,∞)=40\min(40, \infty) = 40. In loops of three or more nodes, poisoned reverse may not be enough.

Question 11hard

Why does Dijkstra's algorithm require non-negative link costs?

  1. Because routers cannot store negative numbers
  2. Because once a node is added to N' its cost is assumed final, and a negative edge found later could make a cheaper path
  3. Because Bellman-Ford also fails on negative costs
  4. Because negative costs create count-to-infinity
Show answer
B) Because once a node is added to N' its cost is assumed final, and a negative edge found later could make a cheaper path

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

Review card

Control plane: COMPUTES the routing table; data plane: forwards LS (Dijkstra): everyone knows the whole topology (flooding), O(n^2) Dijkstra step: add the node outside N' with the SMALLEST D, update its neighbours D(v) = min(D(v), D(w) + c(w,v)); write p(v) if it changes next hop: trace the predecessor chain back to the source's neighbour DV (Bellman-Ford): D_x(y) = min_v { c(x,v) + D_v(y) } talks only to neighbours, distributed, asynchronous good news fast, BAD news slow → count-to-infinity poisoned reverse: if my path goes through you, I tell you "infinity" LS messages O(nE), DV only neighbours; LS is robust, DV spreads errors negative cost: Dijkstra does not work