Routing is the process of selecting the best path for data packets to travel from source to destination across a network.
A Routing Algorithm determines how a router builds its routing table and decides the optimal path based on specific metrics such as hop count, cost, bandwidth, or delay.
Routing algorithms are broadly classified into two categories:
- Distance Vector Routing
- Link State Routing
Before examining algorithms, it is important to understand the criteria used to determine the "best" path:
| Metric | Description |
|---|---|
| Hop Count | Number of routers a packet passes through |
| Cost | Administrative weight assigned to a link |
| Bandwidth | Capacity of the link |
| Delay | Time taken for a packet to traverse a link |
| Reliability | Probability of successful transmission |
| Load | Current traffic on a link |
In Distance Vector Routing, each router maintains a routing table (distance vector) that contains:
- The destination network
- The distance (cost/hop count) to that destination
- The next hop router to reach it
Routers share their entire routing table with their directly connected neighbors at periodic intervals.
- Each router initializes its table with directly connected neighbors (distance = 1) and sets all other destinations to infinity.
- Routers periodically broadcast their distance vectors to neighbors.
- Upon receiving a neighbor's table, a router updates its own table using the Bellman-Ford equation.
- This process repeats until all routing tables converge (no more changes occur).
D(x, y) = min{ c(x, v) + D(v, y) }
for all neighbors v of x
Where:
D(x, y)= estimated cost from router x to destination yc(x, v)= cost of direct link from x to neighbor vD(v, y)= neighbor v's estimated cost to reach y
Network topology:
A ---4--- B ---3--- C
| |
2 1
| |
D --------5-------- E
Router A's initial table:
| Destination | Cost | Next Hop |
|---|---|---|
| A | 0 | - |
| B | 4 | B |
| D | 2 | D |
| C | ∞ | - |
| E | ∞ | - |
After exchanging with neighbors B and D, Router A learns better paths through them and updates accordingly.
A critical issue in distance vector routing:
When a link fails, incorrect routing information can propagate through the network, causing routers to endlessly increment hop counts, believing the destination is reachable through each other.
Example:
- A and B are connected. A reaches C through B (cost 2).
- If the A-B link fails:
- B hears from A that A can reach C at cost 2 (via old info)
- B updates: cost to C = 3 (A's 2 + 1)
- A then updates: cost to C = 4 (B's 3 + 1)
- Counts keep incrementing until infinity threshold (typically 16 for RIP)
Solutions:
- Split Horizon: Do not advertise a route back to the neighbor from which it was learned.
- Poison Reverse: Advertise a failed route with an infinite metric back to the source.
- Hold-down timers: Ignore updates about a failed route for a set duration.
| Feature | Description |
|---|---|
| Information shared | Full routing table with neighbors |
| Knowledge | Local (only knows neighbor info) |
| Convergence | Slow |
| Complexity | Low |
| Scalability | Poor for large networks |
| Examples | RIP, IGRP, EIGRP |
The Bellman-Ford Algorithm is a shortest path algorithm used in distance vector routing. It can handle negative edge weights and can detect negative cycles.
Unlike Dijkstra's algorithm, Bellman-Ford works even when some link costs are negative (which can occur in certain routing metrics).
Given: A graph with V vertices and E edges, and a source node S.
- Initialize distance to source = 0, all others = ∞
- Repeat (V - 1) times:
- For each edge (u, v) with weight w:
- If
dist[u] + w < dist[v], updatedist[v] = dist[u] + w
- If
- For each edge (u, v) with weight w:
- Check for negative cycles:
- For each edge (u, v) with weight w:
- If
dist[u] + w < dist[v], a negative cycle exists
- If
- For each edge (u, v) with weight w:
Network:
Nodes: A, B, C, D, E
Edges: A→B(6), A→D(7), B→C(5), B→D(8), B→E(-4),
C→B(-2), D→E(9), D→C(-3), E→C(7), E→A(2)
Source: A
Initialization:
dist = {A:0, B:∞, C:∞, D:∞, E:∞}
After (V-1) = 4 iterations, the algorithm relaxes all edges and finds shortest paths.
| Case | Complexity |
|---|---|
| Time | O(V × E) |
| Space | O(V) |
| Feature | Bellman-Ford | Dijkstra |
|---|---|---|
| Negative weights | Handled | Not supported |
| Negative cycles | Detects them | Cannot detect |
| Time Complexity | O(V × E) | O(V^2) or O(E log V) |
| Approach | Dynamic programming | Greedy |
| Use in routing | Distance Vector | Link State (OSPF) |
In Link State Routing, each router has complete knowledge of the entire network topology.
Every router:
- Discovers its direct neighbors and the cost of each link
- Broadcasts this information (called a Link State Advertisement / LSA) to ALL routers in the network using flooding
- Each router independently runs Dijkstra's algorithm on the complete topology map to compute shortest paths to all destinations
- Builds its routing table from the computed shortest path tree
Step 1 — Hello Protocol: Each router sends "Hello" packets to discover neighbors and measure link costs.
Step 2 — LSA Flooding: Each router creates an LSA containing:
- Router ID
- List of neighbors
- Cost to each neighbor
This LSA is flooded to all routers in the network.
Step 3 — Build Link State Database (LSDB): Each router assembles a complete map of the network from received LSAs.
Step 4 — Run Dijkstra's Algorithm: Each router independently computes the shortest path tree from itself to all destinations.
Step 5 — Populate Routing Table: The shortest path tree is used to populate the router's forwarding table.
| Feature | Description |
|---|---|
| Information shared | Only local link state (to all routers) |
| Knowledge | Global (complete network topology) |
| Convergence | Fast |
| Complexity | Higher (Dijkstra computation required) |
| Scalability | Better than distance vector |
| Bandwidth Usage | Higher during flooding |
| Examples | OSPF, IS-IS |
Dijkstra's Algorithm computes the shortest path from a single source node to all other nodes in a weighted graph with non-negative edge weights.
It is the core algorithm used in Link State Routing protocols (OSPF).
Given: Graph G with vertices V and edges E, source node S.
- Initialize:
dist[S] = 0,dist[all others] = ∞ - Create a set of unvisited nodes containing all vertices
- Repeat until all nodes are visited:
- Select the unvisited node
uwith the smallestdist[u](greedy choice) - Mark
uas visited - For each unvisited neighbor
vofu:- If
dist[u] + weight(u,v) < dist[v]:- Update
dist[v] = dist[u] + weight(u,v) - Set
prev[v] = u(to reconstruct path)
- Update
- If
- Select the unvisited node
Network:
2 3
A ------B------E
| /| |
6| 1/ |4 5|
| / | |
C-- D-----
3 1
Edges: A-B(2), A-C(6), B-C(1), B-D(4), B-E(3), D-E(1)
Source: A
| Iteration | Visited | dist[A] | dist[B] | dist[C] | dist[D] | dist[E] |
|---|---|---|---|---|---|---|
| Initial | {} | 0 | ∞ | ∞ | ∞ | ∞ |
| 1 | {A} | 0 | 2 | 6 | ∞ | ∞ |
| 2 | {A,B} | 0 | 2 | 3 | 6 | 5 |
| 3 | {A,B,C} | 0 | 2 | 3 | 6 | 5 |
| 4 | {A,B,C,E} | 0 | 2 | 3 | 6 | 5 |
| 5 | All | 0 | 2 | 3 | 6 | 5 |
Shortest paths from A:
- A → B: cost 2
- A → C: cost 3 (via B)
- A → E: cost 5 (via B)
- A → D: cost 6 (via B)
| Implementation | Time Complexity |
|---|---|
| Simple array | O(V^2) |
| Binary heap (priority queue) | O(E log V) |
| Fibonacci heap | O(E + V log V) |
| Feature | Distance Vector | Link State |
|---|---|---|
| Algorithm | Bellman-Ford | Dijkstra |
| Topology knowledge | Partial (neighbor info only) | Complete (entire network) |
| Information shared | Full routing table to neighbors | LSAs flooded to all routers |
| Convergence speed | Slow | Fast |
| Bandwidth usage | Low (periodic updates) | High initially (flooding) |
| Memory usage | Low | High (stores full topology) |
| Computation | Simple | Complex (Dijkstra per router) |
| Scalability | Poor | Better |
| Loop risk | High (count-to-infinity) | Low (complete topology view) |
| Examples | RIP, IGRP | OSPF, IS-IS |
-
Distance Vector routing uses Bellman-Ford; routers share tables only with neighbors.
-
Link State routing uses Dijkstra; routers flood LSAs and compute paths independently.
-
Bellman-Ford handles negative edge weights; Dijkstra requires non-negative weights.
-
Count-to-infinity is a major problem in distance vector routing; split horizon and poison reverse are common fixes.
-
Dijkstra guarantees the shortest path from a single source in O(E log V) with a priority queue.
-
Link state converges faster than distance vector but requires more memory and computation.