Sub topic 17
Distance-vector routing and Bellman-Ford
This chapter assumes you know binary and decimal conversion, the layered network model, what a router is, and basic weighted-graph terms (nodes, edges, paths). It also assumes you have met the difference between forwarding and routing. Forwarding is the per-packet table lookup. Routing is the process that builds the table. Distance-vector routing is one way to build it.
The working model
In distance-vector routing, each router keeps a table with one row per destination. Each row holds the best known cost to that destination and the neighbour (the next hop) to send through. A router does not know the whole network. It knows only:
- The cost of the links to its direct neighbours.
- The distance vectors its neighbours send it. A distance vector is the list of (destination, cost) pairs a router currently believes.
Each router repeats this cycle:
- Send its own distance vector to every neighbour. It does this periodically and when its table changes.
- When a neighbour's vector arrives, compute, for every destination, the cost of going through that neighbour.
- Keep the cheapest option and record the neighbour that gave it as the next hop.
This is "routing by rumour". A router trusts what its neighbours say about the rest of the network and adds the cost of reaching them.
The Bellman-Ford equation
Let d_x(y) be the least cost from router x to destination y. Let c(x, v) be the cost of the direct link from x to neighbour v. Then:
d_x(y) = min over all neighbours v of { c(x, v) + d_v(y) }The neighbour v that achieves the minimum becomes the next hop for y. The distance from a router to itself is 0. Distributed Bellman-Ford runs this equation asynchronously across routers until no table changes. That state is called convergence.
Worked example
Take four routers with these undirected link costs:
| Link | Cost |
|---|---|
| A-B | 2 |
| A-C | 5 |
| B-C | 1 |
| B-D | 4 |
| C-D | 1 |
Router A starts knowing only its neighbours: B at 2 and C at 5. After B and C share their vectors, A learns more.
- To C: through C directly costs 5. Through B costs 2 + 1 = 3. A keeps 3 with next hop B.
- To D: through B costs 2 + 4 = 6. Through C, using C's own vector with its original D cost of 1, costs 5 + 1 = 6. In the next round, C's vector says C reaches D at 1 and B reaches C at 1, so A's route to D through B, now using B's improved path via C, costs 2 + 1 + 1 = 4.
Final distances from A are B = 2, C = 3, D = 4. The next hop for all three is B.
The code below runs the same relaxation centrally. Each pass over the edges is like one round of neighbour exchanges. After n - 1 passes on n nodes, every shortest path has been found, because a shortest path has at most n - 1 edges.
def bellman_ford(nodes, edges, source):
dist = {n: float("inf") for n in nodes}
dist[source] = 0
for _ in range(len(nodes) - 1):
for u, v, w in edges:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
if dist[v] + w < dist[u]:
dist[u] = dist[v] + w
return dist
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}.
How information spreads
After round k of synchronous exchanges, each router knows the best path that uses at most k hops. So a network whose longest shortest path has h hops converges in about h rounds. News travels one hop per exchange.
Properties and costs
| Aspect | Distance-vector behaviour |
|---|---|
| Information held | Only neighbours' vectors and own table |
| Messages sent | To direct neighbours only |
| Computation per router | Light |
| Convergence speed | Slow, proportional to network diameter and timers |
| Typical protocol | RIP |
Per exchange, a router sends a vector with up to N entries to each neighbour. This is cheap in memory and CPU, which is why the method suits small networks.
Common errors
- Forgetting to add the link cost. The candidate cost is
c(x, v) + d_v(y), not justd_v(y). - Using stale vectors. In a round-based exercise, use the vectors from the previous round, not values updated earlier in the same round. GATE questions usually say "after one exchange" or "after convergence". Read which one.
- Mixing up next hop and cost. The table stores both. The next hop is the neighbour, not the final destination.
- Assuming a router knows the topology. Distance-vector routers never see the map. Only link-state routers do.
- Treating a lost link as instant news. A failure spreads one hop per exchange, and this slowness causes the count-to-infinity problem covered in a later chapter.
Exam shortcuts
- To find a router's final table in a small graph, run Dijkstra or inspect shortest paths by hand. Convergence gives the same answer as shortest paths.
- To find the table after exactly
kexchanges, allow only paths of at mostkhops. - Bellman-Ford's centralised running time is O(V·E). It handles negative edge weights, which Dijkstra does not, but routing costs are positive.
Exercise
Router X has neighbours P (link cost 3) and Q (link cost 7). P's vector says its cost to destination Z is 9. Q's vector says its cost to Z is 4. What cost and next hop does X record for Z?
Show answer
Through P: 3 + 9 = 12. Through Q: 7 + 4 = 11. X records cost 11 with next hop Q.
Exercise
A chain of routers R1-R2-R3-R4-R5 has all link costs 1. Starting from only direct-neighbour knowledge, after how many synchronous exchanges does R1 learn a route to R5, and what is its cost?
Show answer
R1 learns about R5 after 4 exchanges, because information travels one hop per exchange and R5 is 4 hops away. The cost is 4.