Routing Algorithms for NSCT
Routing-algorithm questions give you a small network with link costs and ask for a shortest path, a distance-vector update, the order Dijkstra settles nodes, or what happens after a link fails. Others ask which protocol (RIP, OSPF or BGP) fits a situation. This page works each type in full, going deeper than the routing section of the Network Layer topic page.
Computer Networks and Cloud Computing carries 10% of the NSCT, according to HEC's Areas of Competencies. Candidates averaged 39.6% accuracy in the area in 2026, per the HEC NSCT report and APP. Shortest-path traces are slow the first time and fast after a few practice runs.
The method, step by step
- Draw the graph from the cost list before doing anything else.
- Link state (Dijkstra): set the source to 0 and everything else to infinity. Repeatedly make the closest tentative node permanent and relax its neighbours (lower their cost if the new route is cheaper).
- Distance vector (Bellman-Ford): for each neighbour v, add the link cost c(x,v) to v's reported distance, then take the minimum.
- After a failure: remove the link and recompute. In distance vector, check whether a neighbour's stale route runs back through you (count-to-infinity).
- Protocol questions: RIP = hop count, max 15. OSPF = cost from bandwidth, Dijkstra. BGP = path vector between autonomous systems (AS), shortest AS_PATH after policy.
Distance vector vs link state
| Distance vector | Link state | |
|---|---|---|
| Each router knows | Distances its neighbours report | Full map of the area |
| Sends | Its whole distance table to neighbours | Its own links to every router (flooding) |
| Algorithm | Bellman-Ford | Dijkstra |
| Convergence | Slow; may count to infinity | Fast after flooding |
| Examples | RIP (RIPv2 is classless) | OSPF, IS-IS |
Dijkstra's algorithm (link state)
Worked example. Links: A-B 2, A-C 5, B-C 1, B-D 4, C-D 1, C-E 6, D-E 2, D-F 7, E-F 1. Find shortest paths from A. Each row shows the tentative costs after making that node permanent.
| Step | Made permanent | B | C | D | E | F |
|---|---|---|---|---|---|---|
| 1 | A (0) | 2 | 5 | ∞ | ∞ | ∞ |
| 2 | B (2) | – | 3 (via B) | 6 (via B) | ∞ | ∞ |
| 3 | C (3) | – | – | 4 (via C) | 9 (via C) | ∞ |
| 4 | D (4) | – | – | – | 6 (via D) | 11 (via D) |
| 5 | E (6) | – | – | – | – | 7 (via E) |
| 6 | F (7) | – | – | – | – | – |
Final routing table at A:
| Destination | Cost | Path | Next hop |
|---|---|---|---|
| B | 2 | A-B | B |
| C | 3 | A-B-C | B |
| D | 4 | A-B-C-D | B |
| E | 6 | A-B-C-D-E | B |
| F | 7 | A-B-C-D-E-F | B |
Note that the direct link A-C (5) loses to A-B-C (3). The routing table stores only the next hop, which is B for every destination here.

Bellman-Ford (distance vector)
Worked example: one update. Router X has links to A (cost 2), B (cost 7) and C (cost 3). They report distances to Y of 9, 2 and 5.
| Via | c(X, v) | D_v(Y) | Total |
|---|---|---|---|
| A | 2 | 9 | 11 |
| B | 7 | 2 | 9 |
| C | 3 | 5 | 8 |
X installs Y at cost 8 via C. B has the best reported distance but the most expensive link.
Worked example: convergence in rounds. Links: W-X 1, X-Y 2, W-Y 7, Y-Z 1. Routers start knowing only their own links and exchange vectors in synchronous rounds. W's table:
| Round | To X | To Y | To Z |
|---|---|---|---|
| 0 (own links) | 1 | 7 | ∞ |
| 1 | 1 | 3 (via X) | 8 (via Y) |
| 2 | 1 | 3 | 4 (via X) |
| 3 | 1 | 3 | 4 (no change: converged) |
News travels one hop per round, so W needs two rounds to learn the best route to Z, which is three hops away.
Count-to-infinity and its fixes
Worked example. Routers form a line A-B-C, each link cost 1, with no split horizon. A reaches C at 2 via B. The B-C link fails, and B then hears A's stale advertisement of distance 2.
| Update | B's distance to C | A's distance to C |
|---|---|---|
| Before failure | 1 | 2 |
| B hears A's 2 | 3 | 2 |
| A hears B's 3 | 3 | 4 |
| B hears A's 4 | 5 | 4 |
| … | … | … |
| A hears B's 15 | 15 | 16 |
| B hears A's 16 | 16 | 16 |
The two routers bounce the route back and forth, adding 1 each time, until both reach RIP's infinity of 16: 15 updates in all. With RIP's 30-second updates this can take minutes.
Fixes: - Split horizon: do not advertise a route back out of the interface it came from. - Poison reverse: advertise it back with metric 16 (infinity). Both fix two-router loops but not loops of three or more. - Triggered updates and hold-down timers speed up or damp the bad news.
RIP, OSPF and BGP
| RIP | OSPF | BGP | |
|---|---|---|---|
| Type | Distance vector | Link state | Path vector |
| Scope | Inside one AS (IGP) | Inside one AS (IGP) | Between ASes (EGP) |
| Metric | Hop count (16 = unreachable) | Cost = reference bandwidth / link bandwidth | Policy, then AS_PATH length and other attributes |
| Carried by | UDP port 520 | IP protocol 89 | TCP port 179 |
| Updates | Every 30 seconds | On change, plus periodic refresh | Incremental, on change |
Worked example: OSPF cost. With the default reference bandwidth of 100 Mbps, a 10 Mbps link costs 10, a 100 Mbps link costs 1, and a 1 Gbps link also costs 1, because cost cannot go below 1. That is why networks with gigabit links raise the reference bandwidth. With a 1 Gbps reference:
| Link | Cost |
|---|---|
| 10 Mbps | 100 |
| 100 Mbps | 10 |
| 1 Gbps | 1 |
Worked example: BGP path choice. A router in AS 100 learns a prefix through two neighbours with equal local preference. One AS_PATH is 200 300 400 and the other is 500 400. BGP prefers 500 400 because it crosses fewer autonomous systems. A route arriving with AS_PATH 300 100 400 is rejected outright, because 100 is the router's own AS: accepting it would create a loop.
Speed tricks
- Dijkstra: once a node is permanent, never revisit it. Only relax the neighbours of the newly permanent node.
- Bellman-Ford: write a three-column table (link cost, reported distance, total) and circle the minimum.
- Synchronous distance vector needs as many rounds as the hop count of the best path.
- RIP route with metric 15 from a neighbour becomes 16, which is unreachable.
- "Shortest AS_PATH" counts ASes, not routers.
Common mistakes
- Choosing the neighbour with the smallest reported distance and forgetting to add the link cost.
- Trusting a direct link without checking two-hop alternatives.
- Thinking split horizon or poison reverse fixes every loop.
- Saying OSPF uses hop count, or that BGP picks the highest-bandwidth path.
- Mixing up forwarding (per packet, using the table) with routing (building the table).
Practice set
These are different from the quiz questions below.
- A router's link costs to three neighbours are 4, 2 and 5. They report 3, 6 and 1 to a destination. What is the router's cost?
- What is the largest hop count RIP can still use?
- With the 100 Mbps default reference, what OSPF cost does a 1 Gbps link get?
- Which transport protocol and port does BGP use?
- Links X-Y 2, Y-Z 2 and X-Z 5. What is X's shortest distance to Z?
- A router in AS 65001 receives a route with AS_PATH 65010 65001 65020. What does it do?
- Which algorithm does OSPF run on its link-state database?
- In the A-B-C count-to-infinity example, what metric does B advertise right after hearing A's 4?
Show answers
- 6: totals 7, 8 and 6; the minimum is via the third neighbour.
- 15: 16 means unreachable.
- 1: 100 / 1,000 rounds up to the minimum of 1.
- TCP, port 179.
- 4: via Y (2 + 2) beats the direct 5.
- Rejects it: its own AS number is already in the path.
- Dijkstra's shortest-path-first algorithm.
- 5: 1 + 4.
Practise
The quiz below has 20 routing questions, mostly traces and calculations, which you can filter by tier and difficulty. Every shortest path and update was checked by running Dijkstra and Bellman-Ford in Python.
Practise: 20 questions
FAQs
Is Dijkstra or Bellman-Ford better?
Neither is better in every case. Dijkstra is faster on a full map but needs non-negative costs, which routing always has. Bellman-Ford works with only neighbour information, which makes it natural for distance-vector routing, but convergence across a network is slower.
Why does RIP stop at 15 hops?
A small infinity (16) limits how long count-to-infinity can run. The price is that RIP cannot route across networks wider than 15 hops, which is one reason large networks use OSPF.
Is BGP a distance-vector protocol?
It is usually called a path-vector protocol. Like distance vector, it exchanges routes with neighbours, but each route carries the full list of autonomous systems it has crossed. That list prevents loops and lets operators apply policy.
Next step
Practise the addressing that routing tables use in subnetting practice, revise the Network Layer MCQs, return to the Networks & Cloud hub, or try a timed NSCT mock test.