Sub topic 3
Distance-vector routing and Bellman-Ford
This chapter assumes you can convert binary to decimal, know what a router does, and understand graph terms such as nodes, weighted edges and paths. Routing protocols let routers learn paths to every network. The main families are distance-vector, link-state and path-vector.
Distance-vector routing and Bellman-Ford
In distance-vector routing, each router knows only what its neighbours report. A router relaxes a route whenever a neighbour offers a cheaper path: dist[v] = min(dist[v], dist[u] + w(u, v)). Repeating this over all links is the Bellman-Ford algorithm.
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"))Output: {'A': 0, 'B': 2, 'C': 3, 'D': 4}
Count-to-infinity, split horizon and poison reverse
When a link fails, neighbours can keep passing an old route back and forth. Each pass adds one to the metric, so the metric grows toward infinity. Two fixes exist:
- Split horizon means a router never advertises a route back on the interface it learned it from.
- Poison reverse means the router advertises that route back with an infinite metric, which breaks the loop at once.
RIP: hop count and the infinity value of 16
RIP is a distance-vector protocol whose metric is hop count. RIP treats 16 as unreachable. This limits a network to 15 hops and caps how long count-to-infinity can run.
Link-state routing and Dijkstra's algorithm
Link-state routers flood their link information so every router holds the full topology. Each router then runs Dijkstra's algorithm on its own copy of the map. The result gives the same distances as Bellman-Ford in this network.
OSPF: areas, flooding and cost
OSPF is a link-state protocol. Its default cost is the reference bandwidth (100 Mbps in most vendor defaults) divided by the interface bandwidth. Areas limit flooding. Every other area must connect to Area 0, the backbone.
BGP and path-vector routing between autonomous systems
BGP carries the full AS path with each route. A router rejects any route that already contains its own AS, which prevents loops. BGP chooses routes by policy as well as by cost.
Comparing distance-vector, link-state and path-vector routing
| Property | Distance-vector (RIP) | Link-state (OSPF) | Path-vector (BGP) |
|---|---|---|---|
| Information shared | Distance table with neighbours | Full topology via flooding | Path of AS numbers |
| Algorithm | Bellman-Ford | Dijkstra | Path selection by policy |
| Scope | Small networks | Inside one AS | Between ASes |
GATE problem patterns and shortcuts
- For a shortest-path question, run Dijkstra by hand and list the distances in order.
- For RIP, count hops, and treat any route of 16 or more as unreachable.
Exercise
In the five-link network, what is the shortest distance from A to D, and which path gives it?
Show answer
The distance is 4, via A-B-C-D (2 + 1 + 1).