Sub topic 13
Distance-Vector Routing and Bellman-Ford
Assume you know IPv4 addresses, forwarding, weighted graphs, and the difference between a router and a host. This chapter explains how routers learn paths when each router knows only its neighbors and the cost of reaching them.
Working model
Distance-vector routing is a distributed shortest-path method. Each router stores a distance vector, which lists its current best distance to every destination and the next hop for that destination.
A router initially knows:
- Its distance to itself is 0.
- Its distance to each directly connected neighbor is the link cost.
- Its distance to every other destination is infinity.
Routers periodically send their vectors to neighbors. When router \(x\) receives neighbor \(v\)'s estimate for destination \(y\), it evaluates:
\[ Dx(y) = \minv \{c(x,v) + D_v(y)\} \]
Here, \(c(x,v)\) is the cost of the link from \(x\) to \(v\). This is the Bellman-Ford recurrence.
Worked example
Consider this undirected network:
Starting at router A:
- To B, the direct cost is 2.
- To C, the path A-B-C costs \(2+1=3\), which is better than the direct cost 5.
- To D, the path A-B-C-D costs \(2+1+1=4\).
The resulting distances are:
| Destination | Minimum cost | Next hop from A |
|---|---|---|
| A | 0 | — |
| B | 2 | B |
| C | 3 | B |
| D | 4 | B |
Bellman-Ford repeatedly relaxes every link. Relaxation means replacing a known distance when a cheaper path is discovered. In a network with \(V\) routers, \(V-1\) full passes are sufficient for shortest paths when costs are nonnegative.
def bellman_ford(nodes, edges, source):
distance = {node: float("inf") for node in nodes}
distance[source] = 0
for _ in range(len(nodes) - 1):
for u, v, cost in edges:
if distance[u] + cost < distance[v]:
distance[v] = distance[u] + cost
if distance[v] + cost < distance[u]:
distance[u] = distance[v] + cost
return distance
nodes = ["A", "B", "C", "D"]
edges = [
("A", "B", 2),
("A", "C", 5),
("B", "C", 1),
("B", "D", 4),
("C", "D", 1),
]
print(bellman_ford(nodes, edges, "A"))The output is {'A': 0, 'B': 2, 'C': 3, 'D': 4}.
Exam points and limitations
Distance-vector routing is simple and uses less topology information than link-state routing. However, bad news can travel slowly. Routers may repeatedly increase a route's distance when a failed destination is reachable through one another. This is the count-to-infinity problem.
Split horizon prevents a router from advertising a route back through the interface from which it learned that route. Poison reverse advertises that route back with an infinite metric. These techniques reduce routing loops but do not remove every failure mode.
Exercise
In the example network, what is the minimum cost from A to D, and which path provides it?
Show answer
The minimum cost is 4, provided by A-B-C-D: 2 + 1 + 1 = 4.