1. Home
  2. Computer Networks
  3. Routing Algorithms (Distance Vector)

Routing Algorithms (Distance Vector)

How routers learn the best path to every network without seeing the whole map. Watch distance-vector tables update round by round in 3D.

Interactive 3DIntermediate13 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Five routers connected by links with costs. In distance-vector routing (used by RIP), each router only talks to its neighbours, sharing its table of distances. Press Run and watch the tables fill in.

Pseudocode

  1. each router starts knowing only its direct neighbours
  2. repeat every round:
  3. send my distance vector to my neighbours
  4. for each destination y:
  5. D(x, y) ← min over neighbours v of [ cost(x, v) + Dv(y) ] // Bellman–Ford
  6. until no table changes (converged)
Read this model as text · 17 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Five routers connected by links with costs. In distance-vector routing (used by RIP), each router only talks to its neighbours, sharing its table of distances. Press Run and watch the tables fill in.
  2. At the start each router only knows the cost to its direct neighbours (from its own links). Everything else is ∞.
  3. Round 1, router A reads the vectors of its neighbours B, C and updates: to C: 3 via B; to D: 5 via B; to E: 11 via C.
  4. Round 1, router B reads the vectors of its neighbours A, C, D and updates: to D: 3 via C; to E: 7 via D.
  5. Round 1, router C reads the vectors of its neighbours A, B, D, E and updates: to A: 3 via B; to E: 4 via D.
  6. Round 1, router D reads the vectors of its neighbours B, C, E and updates: to A: 5 via B; to B: 3 via C.
  7. Round 1, router E reads the vectors of its neighbours C, D and updates: to A: 11 via C; to B: 7 via D; to C: 4 via D.
  8. End of round 1.
  9. Round 2, router A reads the vectors of its neighbours B, C and updates: to D: 4 via B; to E: 8 via B.
  10. Round 2, router B reads the vectors of its neighbours A, C, D and updates: to E: 6 via C.
  11. Round 2, router D reads the vectors of its neighbours B, C, E and updates: to A: 4 via C.
  12. Round 2, router E reads the vectors of its neighbours C, D and updates: to A: 8 via D; to B: 6 via D.
  13. End of round 2.
  14. Round 3, router A reads the vectors of its neighbours B, C and updates: to E: 7 via B.
  15. Round 3, router E reads the vectors of its neighbours C, D and updates: to A: 7 via D.
  16. End of round 3.
  17. Round 4: nothing changed — the network has converged. Every router now knows the cheapest cost and next hop to every destination, without ever seeing the whole map.

Try this in the 3D model

  • Run distance vector and watch router A learn about E. In which round does it happen?
  • Find the cell "A to D". It first goes via C — does it ever change?
  • Press Raise cost of C–D to 9. Which routers have to change their routes?

The routing problem

When a packet arrives, a router must decide which neighbour to forward it to so that it reaches its destination along a good path (fewest hops, lowest delay, cheapest cost). Each router keeps a routing table: for every destination, the best known cost and the next hop.

How do routers fill these tables without a central map? Two classic families:

  • Distance vector — talk only to your neighbours (RIP).
  • Link state — learn the whole map and run Dijkstra (OSPF, IS-IS).

Distance-vector routing

Each router x keeps a vector D(x, y) = its best known cost to every destination y.

  1. Start: x knows the cost to itself (0) and to its direct neighbours. Everything else is ∞.
  2. Every round, each router sends its vector to its neighbours.
  3. Each router updates with the Bellman–Ford equation:
D(x, y) = min over neighbours v of [ cost(x, v) + D_v(y) ]

“My best route to y is: pick the neighbour v for which (cost to reach v) + (v’s distance to y) is smallest — and use v as my next hop.”

  1. Repeat until no table changes — the network has converged.

In the 3D model, the table on the right has one row per router. Watch the ∞ entries disappear round by round as good news spreads.

Good news travels fast, bad news slowly

When a link gets cheaper or a new route appears, it spreads in a few rounds. When a link gets worse or fails, routers may keep believing stale routes learned from each other, increasing their costs step by step — the count-to-infinity problem. Try Raise cost of C–D to 9 to see routers re-learn their routes.

Fixes used in practice:

  • Split horizon: don’t advertise a route back to the neighbour you learned it from.
  • Poison reverse: advertise it back with cost ∞.
  • Maximum hop count: RIP treats 16 as infinity.

Every router floods information about its own links to all routers, so everyone builds the complete map and runs Dijkstra’s algorithm locally.

Distance vector Link state
Knows Only neighbours’ vectors The whole topology
Algorithm Bellman–Ford (distributed) Dijkstra (local)
Convergence Slower, count-to-infinity risk Fast
Memory / CPU Low Higher
Protocols RIP OSPF, IS-IS

Between organisations on the internet, BGP (a path-vector protocol) is used.

Code

INF = float("inf")
links = {("A","B"): 1, ("B","C"): 2, ("A","C"): 5, ("C","D"): 1,
         ("B","D"): 4, ("D","E"): 3, ("C","E"): 6}
routers = sorted({r for l in links for r in l})
cost = lambda a, b: 0 if a == b else links.get((a, b), links.get((b, a), INF))
nbrs = {x: [v for v in routers if v != x and cost(x, v) < INF] for x in routers}

D = {x: {y: cost(x, y) for y in routers} for x in routers}       # round 0
for rnd in range(1, len(routers)):
    new = {x: {y: 0 if x == y else min(cost(x, v) + D[v][y] for v in nbrs[x])
               for y in routers} for x in routers}
    if new == D:
        break
    D = new
print(D["A"])   # {'A': 0, 'B': 1, 'C': 3, 'D': 4, 'E': 7}

Common mistakes

  • Using the new vectors of other routers within the same round (real routers use what they received last).
  • Forgetting to record the next hop — the cost alone doesn’t tell you where to send packets.
  • Confusing distance vector (neighbours only) with link state (everyone floods).

Complexity at a glance

Case / operationTimeWhy
One round (V routers, E links)O(V · E)Every router combines its neighbours' vectors.
Rounds to converge≤ V − 1The longest shortest path in hops.
Extra spaceO(V) per router

Quick check

Test yourself — pick an answer to see if you got it.

1. In distance-vector routing, a router shares its table with…

2. Which equation does each router use?

3. Which protocol uses distance-vector routing?

4. What is the "count-to-infinity" problem?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Routing Algorithms (Distance Vector). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.