12 · Distance vector (Bellman-Ford), link cost changes, LS vs DV ★
Slides 5-14 → 5-23 · K&R 5.2.2 · HW6 P5, P7 · Sample midterm Problem 3
Bellman-Ford equation (5-14)
dx(y) = min over all neighbors v of x { c(x,v) + dv(y) }
^^^^^^ ^^^^^
cost to neighbor v cost from neighbor v to destination y
- dx(y) = cost of the least-cost path from x to y.
- Idea (dynamic programming): to reach y, x must first go to some neighbor v, then take v's best path. Try every neighbor and keep the cheapest.
- The neighbor that achieves the minimum is the next hop, and it goes in the forwarding table (5-15).
Slide example (5-15)
Graph of 5-6. u's neighbors: v (cost 2), x (cost 1), w (cost 5). Given dv(z) = 5, dx(z) = 3, dw(z) = 3:
du(z) = min { c(u,v) + dv(z), c(u,x) + dx(z), c(u,w) + dw(z) }
= min { 2 + 5, 1 + 3, 5 + 3 }
= min { 7, 4, 8 } = 4 → next hop x
What each node stores (5-16, 5-17)
- Dx(y) = x's estimate of the least cost from x to y. Distance vector Dx = [Dx(y) : y ∈ N].
- x knows the cost to each neighbor v: c(x,v).
- x keeps its neighbors' distance vectors Dv = [Dv(y) : y ∈ N] for each neighbor v.
- Key idea: from time to time each node sends its own DV to its neighbors. When x gets a new DV from a neighbor, it updates with B-F:
Dx(y) ← minv{ c(x,v) + Dv(y) }for each y ∈ N. - Under minor, natural conditions the estimates Dx(y) converge to the true least cost dx(y).
The DV loop (5-18)
each node: wait for (change in local link cost or DV message from a neighbor) recompute estimates (B-F for every destination) if DV to any destination changed → notify neighbors (loop)
- Iterative, asynchronous: each local iteration is triggered by a local link cost change or a DV update from a neighbor. Nodes don't run in lockstep.
- Distributed: a node notifies neighbors only when its DV changes. Those neighbors notify their neighbors if necessary.
- Self-terminating: when no DV changes, no messages are sent and the algorithm goes quiet.
Slide walk-through: x, y, z (5-19, 5-20)
Links: c(x,y) = 2, c(y,z) = 1, c(x,z) = 7. Each table: rows = "from" (whose vector), columns = "cost to" x y z.
Round 0: each node knows only its own links
node x node y node z x y z x y z x y z x 0 2 7 x ∞ ∞ ∞ x ∞ ∞ ∞ y ∞ ∞ ∞ y 2 0 1 y ∞ ∞ ∞ z ∞ ∞ ∞ z ∞ ∞ ∞ z 7 1 0
Each node sends its own row to its neighbors.
Round 1: each node copies in its neighbors' rows and recomputes its own
x: Dx(y) = min{c(x,y)+Dy(y), c(x,z)+Dz(y)} = min{2+0, 7+1} = 2
Dx(z) = min{c(x,y)+Dy(z), c(x,z)+Dz(z)} = min{2+1, 7+0} = 3 ← changed (was 7)
y: Dy(x) = min{2+0, 1+7} = 2; Dy(z) = min{1+0, 2+7} = 1 ← no change
z: Dz(x) = min{c(z,y)+Dy(x), c(z,x)+Dx(x)} = min{1+2, 7+0} = 3 ← changed (was 7)
Dz(y) = min{1+0, 7+2} = 1
node x node y node z
x y z x y z x y z
x 0 2 3 x 0 2 7 x 0 2 7
y 2 0 1 y 2 0 1 y 2 0 1
z 7 1 0 z 7 1 0 z 3 1 0
x and z changed, so they send their new vectors. y did not change, so y sends nothing new.
Round 2: everyone has the final vectors
node x node y node z x y z x y z x y z x 0 2 3 x 0 2 3 x 0 2 3 y 2 0 1 y 2 0 1 y 2 0 1 z 3 1 0 z 3 1 0 z 3 1 0
Recomputing gives no change anywhere, so no more messages. Converged. Note x reaches z via y (cost 3), not over the direct 7 link.
Link cost changes (5-21, 5-22)
Topology on both slides: c(x,y) = 4, c(y,z) = 1, c(x,z) = 50. Before the change: Dy(x) = 4 (direct), Dz(x) = 5 (via y).
Good news travels fast (5-21): c(x,y) 4 → 1
t0: y detects the change. Dy(x) = min{1+0, 1+5} = 1. DV changed → y informs neighbors.
t1: z gets y's DV. Dz(x) = min{c(z,y)+Dy(x), c(z,x)} = min{1+1, 50} = 2. Changed → z sends its DV.
t2: y gets z's DV. Dy(x) = min{1, 1+2} = 1. No change → y does NOT send to z. Done.
Bad news travels slow, "count to infinity" (5-22): c(x,y) 4 → 60
t0: y detects 60. Dy(x) = min{60+0, c(y,z)+Dz(x)} = min{60, 1+5} = 6 ← via z!
But z's 5 was a path THROUGH y. Routing loop: y → z → y.
t1: z gets Dy(x)=6. Dz(x) = min{50, 1+6} = 7
t2: y gets Dz(x)=7. Dy(x) = min{60, 1+7} = 8
t3: z: min{50, 1+8} = 9
... each pair of messages adds 2 ...
end: z's cost via y finally exceeds 50, so z switches to its direct link.
Final: Dz(x) = 50 (direct), Dy(x) = 51 (via z).
- 44 iterations before the algorithm stabilizes (slide, K&R). If c(x,z) were huge, it would count far longer: count to infinity.
- Why: y uses z's old estimate, not knowing z's path goes through y itself.
Poisoned reverse (5-22)
- Rule: if Z routes through Y to get to X, Z tells Y that Z's distance to X is ∞, so Y won't route to X via Z.
- On the 60 example: z advertises Dz(x) = ∞ to y. y picks the direct 60. z then computes min{50, 1+60} = 50 (direct) and now tells y the true 50. y computes min{60, 1+50} = 51 via z, and poisons back to z. Stable in a few messages.
- Does it fully solve count to infinity? No. It fixes loops between two neighbors, but loops through 3 or more nodes are not detected (K&R 5.2.2).
LS vs DV (5-23)
| Link state (LS) | Distance vector (DV) | |
|---|---|---|
| Message complexity | n routers, O(n²) messages sent | Exchange between neighbors only; convergence time varies |
| Speed of convergence | O(n²) algorithm, O(n²) messages; may have oscillations | Convergence time varies; may have routing loops; count-to-infinity problem |
| Robustness (router malfunctions or is compromised) | Router can advertise an incorrect link cost; each router computes only its own table, so damage is limited | Router can advertise an incorrect path cost ("I have a really low cost path to everywhere"): black-holing; each router's table is used by others, so errors propagate through the network |
How to solve: one round of a DV update (sample midterm P3)
- List N1's link cost to each neighbor: c(N1,v).
- List each neighbor's reported cost to the destination: Dv(dest).
- For each neighbor compute
c(N1,v) + Dv(dest). - New cost = the minimum. Next hop = the neighbor that gave it.
- Recompute from scratch. Don't keep the old table value just because it was smaller: it was built from old reports.
Worked: sample midterm Problem 3
Q (sample midterm P3): The following tables are used in a distance-vector routing algorithm (the cost may be asymmetric). Node 1's routing table before the update (destination: cost, next node) is: 1: 0, –; 2: 2, 2; 3: 5, 3; 4: 1, 4; 5: 6, 3; 6: 7, 4 (shown below). Node 1 has three neighbors N2, N3, and N4. After one moment, N1 receives: N2 tells N1 its cost to N6 has changed to 7; N3 tells N1 its cost to N6 has changed to 3; N4 tells N1 its cost to N6 has changed to 9. N1 updates its routing table. After one round of the algorithm, in the new table, what is the cost from N1 to N6, and which node should be the next node to achieve that cost?
| Dest | Cost | Next |
|---|---|---|
| 1 | 0 | – |
| 2 | 2 | 2 |
| 3 | 5 | 3 |
| 4 | 1 | 4 |
| 5 | 6 | 3 |
| 6 | 7 | 4 |
Neighbors N2, N3, N4 with costs from N1 of 2, 5, 1 (rows 2, 3, 4). New reports for N6: N2 → 7, N3 → 3, N4 → 9.
via N2: c(1,2) + D2(6) = 2 + 7 = 9
via N3: c(1,3) + D3(6) = 5 + 3 = 8 ← min
via N4: c(1,4) + D4(6) = 1 + 9 = 10
D1(6) = min{9, 8, 10} = 8, next hop N3
Answer: cost 8, next node N3. The old entry (7 via N4) is gone: it was 1 + 6 from N4's old report, and N4 now says 9.
- Why add the link cost? The neighbor reports the cost from itself to N6. N1 must first pay to reach that neighbor. That is exactly B-F:
c(x,v) + Dv(y). "The cost may be asymmetric" means you use the cost from N1 to the neighbor. - Why not just pick the smallest report (N3's 3)? Here it happens to win, but a neighbor with a small report behind an expensive link can lose. Always add first, then take the min.
- What if a reported value was a path through N1 itself? Plain DV can't tell. N1 would use it anyway and could form a loop (N1 → neighbor → N1 …), which is the count-to-infinity setup. With poisoned reverse, a neighbor that routes to N6 through N1 reports ∞ to N1, so that option drops out of the min.
Worked: HW6 P5 (z's table from start to stable)
Q (HW6 P5): Consider the network with nodes u, v, x, y, z and undirected link costs u–v 1, u–y 2, v–x 3, v–z 6, x–y 3, x–z 2. Assume each node initially knows the costs to each of its neighbors. Using the distance-vector algorithm, show the distance table entries at node z. Give z's table entries at each step of the algorithm, from initialization until it stabilizes.
| Link costs (HW6 figure) |
|---|
| u–v 1, u–y 2, v–x 3, v–z 6, x–y 3, x–z 2 |
z's neighbors are v (cost 6) and x (cost 2), so z's table has rows v, x, z. Every z entry is Dz(d) = min{ 6 + Dv(d), 2 + Dx(d) }.
Snapshot 1: initialization
| from \ cost to | u | v | x | y | z |
|---|---|---|---|---|---|
| v | ∞ | ∞ | ∞ | ∞ | ∞ |
| x | ∞ | ∞ | ∞ | ∞ | ∞ |
| z | ∞ | 6 | 2 | ∞ | 0 |
z knows only its own link costs. u and y are not neighbors → ∞. Neighbors' rows unknown → ∞.
Snapshot 2: z receives v's and x's initial vectors
| from \ cost to | u | v | x | y | z |
|---|---|---|---|---|---|
| v | 1 | 0 | 3 | ∞ | 6 |
| x | ∞ | 3 | 0 | 3 | 2 |
| z | 7 | 5 | 2 | 5 | 0 |
v's initial row: its own links u 1, x 3, z 6; y not adjacent → ∞
x's initial row: its own links v 3, y 3, z 2; u not adjacent → ∞
Dz(u) = min{6+1, 2+∞} = 7
Dz(v) = min{6+0, 2+3} = 5 (via x beats the direct 6)
Dz(x) = min{6+3, 2+0} = 2
Dz(y) = min{6+∞, 2+3} = 5
Snapshot 3: v and x have updated too and send new vectors
| from \ cost to | u | v | x | y | z |
|---|---|---|---|---|---|
| v | 1 | 0 | 3 | 3 | 5 |
| x | 4 | 3 | 0 | 3 | 2 |
| z | 6 | 5 | 2 | 5 | 0 |
v learned from u (u–y 2): Dv(y) = 1+2 = 3; from x (x–z 2): Dv(z) = 3+2 = 5
x learned from v (v–u 1): Dx(u) = min{3+1, 3+2 (via y)} = 4
z recomputes:
Dz(u) = min{6+1, 2+4} = 6 (now via x)
Dz(v) = min{6+0, 2+3} = 5
Dz(x) = min{6+3, 2+0} = 2
Dz(y) = min{6+3, 2+3} = 5
Snapshot 4: stable
Identical to snapshot 3. No vector changes, so no more messages. z's final DV: u 6, v 5, x 2, y 5, z 0, all via x.
Worked: HW6 P7 (when does x tell its neighbors?)
Q (HW6 P7): Consider a network fragment where x has only two attached neighbors, w and y, with link costs c(x,w) = 2, c(w,y) = 2, c(x,y) = 5. w has a minimum-cost path to destination u (not shown) of 5, and y has a minimum-cost path to u of 6. The complete paths from w and y to u (and between w and y) are not shown. All link costs in the network are strictly positive integers.
- (a) Give x's distance vector for destinations w, y, and u.
- (b) Give a link-cost change for either c(x,w) or c(x,y) such that x will inform its neighbors of a new minimum-cost path to u as a result of running the distance-vector algorithm.
- (c) Give a link-cost change for either c(x,w) or c(x,y) such that x will not inform its neighbors of a new minimum-cost path to u as a result of running the distance-vector algorithm.
a. x's distance vector
Dx(w) = min{c(x,w), c(x,y)+Dy(w)} = min{2, 5+2} = 2
Dx(y) = min{c(x,y), c(x,w)+Dw(y)} = min{5, 2+2} = 4 (via w)
Dx(u) = min{c(x,w)+Dw(u), c(x,y)+Dy(u)} = min{2+5, 5+6} = 7 (via w)
Dx(w) = 2, Dx(y) = 4, Dx(u) = 7.
b. A change that makes x inform its neighbors (official)
- c(x,y) changes: the path via y costs c(x,y) + 6 ≥ 1 + 6 = 7, so x's cost to u stays at least 7. With c(x,y) ≥ 1, no new cost to u. (Only δ < 1, impossible for integers, would give δ + 6.)
- c(x,w) changes: if c(x,w) = ε ≤ 1, the path still goes via w and costs 5 + ε (e.g. 6). New cost → x informs its neighbors.
- If c(x,w) = δ > 6, via w costs δ + 5 > 11, so the best path moves to y with cost 11. New cost → x informs.
- In general Dx(u) = min{c(x,w) + 5, 11}, so any change to c(x,w) changes Dx(u) away from 7.
c. A change that does NOT make x inform its neighbors
Any change in c(x,y) (keeping c(x,y) ≥ 1). Example: c(x,y) = 1 gives via y = 1 + 6 = 7, a tie with via w: Dx(u) stays 7. No new least cost to u, so no message about u.
• Add the link cost to the neighbor before taking the min. The neighbor's report alone is not your cost.
• Recompute from the latest reports. If your current next hop reports a worse cost, your cost goes up, even if the old table value was lower.
• The min is only over neighbors. A non-neighbor's vector never appears in your table.
• HW6 P7: Dx(u) via y uses c(x,y) = 5 + 6 = 11, not Dx(y) = 4 + 6. B-F uses the direct link cost to the neighbor.
• A node sends only if its own DV changed. Receiving a message does not by itself trigger sending.
• Good news (cost drops) converges in a couple of messages; bad news (cost rises) can count up slowly. Know which is which.
• Poisoned reverse does not solve loops of 3 or more nodes.
Quick check
1. Write the Bellman-Ford equation and say what each term is.
dx(y) = minv{ c(x,v) + dv(y) }, min over all neighbors v of x. c(x,v) = cost to neighbor v; dv(y) = v's least cost to y. The v that wins is the next hop.2. When does a DV node send its vector to its neighbors?
Only when its own DV changed, after a local link cost change or a neighbor's DV update.3. Sample P3 variant: same N1 table and link costs (N2 2, N3 5, N4 1). Reports for N6: N2 → 4, N3 → 6, N4 → 8. New cost and next hop?
via N2: 2 + 4 = 6 ← min via N3: 5 + 6 = 11 via N4: 1 + 8 = 9Cost 6, next hop N2.
4. Sample P3 variant: reports for N6: N2 → 9, N3 → 6, N4 → 8. The old entry was 7 via N4. New cost and next hop?
via N2: 2 + 9 = 11 via N3: 5 + 6 = 11 via N4: 1 + 8 = 9 ← minCost 9, next hop N4. It is worse than the old 7, but the old 7 was built on N4's old report (6). N1 must use the current reports.
5. Sample P3 variant with different link costs: c(N1,N2) = 3, c(N1,N3) = 4, c(N1,N4) = 6. Reports for N6: N2 → 10, N3 → 8, N4 → 3. New cost and next hop?
via N2: 3 + 10 = 13 via N3: 4 + 8 = 12 via N4: 6 + 3 = 9 ← minCost 9, next hop N4.
6. Count to infinity: c(x,y) = 3, c(y,z) = 1, c(x,z) = 20. Converged, then c(x,y) jumps to 40. Trace Dy(x) and Dz(x) to the end.
Before: Dy(x) = 3 (direct), Dz(x) = min{20, 1+3} = 4 via y.y: min{40, 1+4} = 5 (via z: loop)
z: min{20, 1+5} = 6
y: 7 z: 8 y: 9 z: 10 y: 11 z: 12
y: 13 z: 14 y: 15 z: 16 y: 17 z: 18
y: 19 z: min{20, 1+19} = 20
y: min{40, 1+20} = 21
z: min{20, 1+21} = 20 (direct) → no change, stop
Final: Dz(x) = 20 (direct link), Dy(x) = 21 (via z). y updated 9 times (5, 7, …, 21) because each exchange only adds 2.7. Same slide topology (4, 1, 50). With poisoned reverse, c(x,y) goes 4 → 60. What happens?
z routes to x via y, so z had told y Dz(x) = ∞. y: min{60, 1+∞} = 60 direct. z: min{50, 1+60} = 50 direct, and now tells y the truth (50). y: min{60, 1+50} = 51 via z, and poisons back to z. z stays 50. Done in a few messages, no count to infinity.8. Slide topology (4, 1, 50), c(x,y) drops 4 → 2. Trace the messages.
t0: y: Dy(x) = min{2, 1+5} = 2, changed → sends. t1: z: Dz(x) = min{50, 1+2} = 3, changed → sends. t2: y: min{2, 1+3} = 2, no change → no message. Converged after 2 messages ("good news travels fast").9. Slide 5-15 graph, but c(u,x) rises from 1 to 6 (dv(z) = 5, dx(z) = 3, dw(z) = 3 unchanged). du(z)?
min{2+5, 6+3, 5+3} = min{7, 9, 8} = 7, next hop v.10. Does poisoned reverse solve count to infinity completely?
No. It stops loops between two neighbors only. Loops involving 3 or more nodes are not detected.11. Robustness: what can a bad router do in LS vs DV?
LS: advertise a wrong link cost; each router still computes only its own table. DV: advertise a wrong path cost ("really low cost to everywhere") → black-holing, and the error propagates because every router's table is used by others.12. What do "iterative, asynchronous, distributed" mean for DV?
Iterative/asynchronous: each local iteration is caused by a local link cost change or a neighbor's DV message, not a global clock. Distributed: each node computes from neighbor info and notifies neighbors only when its DV changes.13. In the x/y/z walk-through (2, 1, 7), why doesn't y send anything after round 1?
Its vector stayed[2 0 1]: min{2+0, 1+7} = 2 and min{1+0, 2+7} = 1. No change → no message.← prev · all topics · next →