Sub topic 5
The Bellman-Ford update equation
This chapter assumes you know what a routing table is and what a weighted graph is. It also assumes you have read the previous chapter on the distance-vector principle.
The equation
Each router keeps its best-known cost to every destination. The Bellman-Ford equation says how to compute it. Let c(x,v) be the cost of the link from router x to its neighbor v. Let D_v(y) be the cost to destination y that neighbor v advertises. Then:
D_x(y) = min over all neighbors v of [ c(x,v) + D_v(y) ]
In words, router x tries each neighbor as the next hop. It adds the cost of reaching that neighbor to the cost the neighbor claims for the destination. The smallest total wins, and that neighbor becomes the next hop.
Applying one advertisement
A router rarely sees all neighbors at once. Advertisements arrive one at a time, so the code applies the equation incrementally. It follows two rules:
- Accept a route if its cost is lower than the current one.
- Always accept news from the current next hop, even if the cost is higher. That neighbor is the one whose path you are using, so a worse cost from it is the new truth.
INFINITY = 16
def dv_update(me, link_cost, table, neighbor, advertised):
"""table: dest -> (cost, next_hop). advertised: dest -> cost as sent by neighbor."""
changed = False
for dest, adv in advertised.items():
if dest == me:
continue
new = min(link_cost + adv, INFINITY)
cur = table.get(dest)
if cur is None:
if new < INFINITY:
table[dest] = (new, neighbor)
changed = True
elif new < cur[0] or cur[1] == neighbor:
if (new, neighbor) != cur:
table[dest] = (new, neighbor)
changed = True
return changed
table = {}
print(dv_update("A", 1, table, "B", {"A": 1, "C": 2}), table)
print(dv_update("A", 1, table, "D", {"C": 1}), table)
print(dv_update("A", 1, table, "B", {"C": 5}), table)
print(dv_update("A", 1, table, "D", {"C": 16}), table)Tracing the run
Router A has two neighbors, B and D, and both links cost 1.
- B advertises C at cost 2. A has no route to C, so it installs
(3, B). B's advertisement for A is skipped. - D advertises C at cost 1. The new cost is 2, which is lower than 3, so A switches to
(2, D). - B advertises C at cost 5. The new cost is 6. It is not lower, and B is not the next hop, so A ignores it.
- D advertises C at cost 16. D is the next hop, so A must accept it. The cost is capped at 16 and the route becomes
(16, D), which means unreachable.
The cap at INFINITY matters. Without it, costs could grow without bound, which the later chapter on count to infinity explains.
Common errors
- Ignoring a cost increase from the current next hop. The table then keeps advertising a path that no longer exists.
- Forgetting to cap the sum at the maximum metric.
- Installing routes at
INFINITYwhen no route exists yet.
Exercise
Router A has the route (4, B) to destination Z. Its link to B costs 1. B now advertises Z at cost 9. Another neighbor E (link cost 2) advertises Z at cost 3. After A processes both advertisements, in the order B then E, what is A's route to Z?
Show answer
B is the current next hop, so A accepts its news: 1 + 9 = 10, giving (10, B). Then E gives 2 + 3 = 5, which is lower than 10, so the final route is (5, E).