Routing algorithms: link state and distance vector

Intermediate

Routers find paths by talking to each other. Think of the network as a graph: routers are nodes, links are edges, and each link has a cost (its delay, or a value set by the operator). Two families of algorithms find the cheapest paths.

Link state (OSPF)

Every router floods a small message to all others: "I am A; my links go to B with cost 1 and to C with cost 5". Soon every router has the whole map, and each one runs Dijkstra's algorithm to find the cheapest path to every destination. OSPF, used inside most company and ISP networks, works this way. (See Dijkstra step by step in the DSA Lab.)

Distance vector (RIP)

Here nobody has the map. Each router keeps a distance vector: its best known cost to every destination. Again and again, every router sends its vector to its neighbours, and updates its own with the Bellman-Ford rule:

cost to D = the smallest, over neighbours N, of (link cost to N + N's cost to D)

cost = {"A": {"B": 1, "C": 5}, "B": {"A": 1, "C": 2}, "C": {"A": 5, "B": 2, "D": 1}, "D": {"C": 1}}
routers = ["A", "B", "C", "D"]
INF = 99

# At first each router knows only its own links.
dist = {r: {d: 0 if r == d else cost[r].get(d, INF) for d in routers} for r in routers}

changed, rounds = True, 0
while changed:
    changed, rounds = False, rounds + 1
    new = {r: dict(dist[r]) for r in routers}
    for r in routers:
        for n, c in cost[r].items():          # every neighbour's vector
            for d in routers:
                if c + dist[n][d] < new[r][d]:
                    new[r][d] = c + dist[n][d]
                    changed = True
    dist = new
    print("after round", rounds, "A knows:", dist["A"])
Open in compiler

After a few rounds nothing changes: the tables have converged. Each router only ever talks to its neighbours and never knows the whole map.

Bad news travels slowly

Good news (a new cheap link) spreads fast. Bad news does not. Suppose the link from B to C breaks. B no longer reaches C directly, but A still says "I can reach C at cost 2" (through B itself!). B believes it and uses cost 3 via A; then A updates to 4 via B, and so on: the count-to-infinity problem. RIP limits it by treating 16 as infinity, and with split horizon: never tell a neighbour about a route you learned from that neighbour.

FeatureLink stateDistance vector
each router knowsthe whole maponly its neighbours' vectors
algorithmDijkstraBellman-Ford
messagesflooded to everyonesent to neighbours
after a failureconverges quicklymay count to infinity
exampleOSPFRIP

Between the networks of different companies, the internet uses BGP, which also sends routes to neighbours but chooses them by policy (business agreements) as well as by length.

Common mistakes

  • Thinking distance-vector routers know the full path. They only know a cost and the next hop.
  • Using Dijkstra with negative costs. Link costs are never negative, so the problem does not arise in routing.
  • Thinking the rounds are in a fixed order. Real routers send updates whenever they like; the final tables are the same.

Quick check

1. Which algorithm does a link-state router run on its map?
2. B's link to C costs 4, and A tells B it reaches C at cost 1. B's link to A costs 2. What is B's best cost to C?
3. Which problem belongs to distance-vector routing?
4. What does split horizon do?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.