Sub topic 2
Modeling a network as a weighted graph
This chapter assumes you know basic IP addressing and subnetting, the difference between a router and a switch, how a routing table uses longest-prefix match, and the graph terms node, edge, weight, and shortest path. It builds the model that every later chapter on distance-vector and link-state routing uses.
Why routing protocols need a model
A routing protocol must pick a next hop for every destination. To reason about "best" and "loop-free", it needs a simple abstraction of the network. Both algorithm families in this tutorial use the same one: a weighted graph.
The model deliberately throws away detail. It ignores cable types, vendors, and physical layout. It keeps only what the path computation needs: which routers can reach each other directly, and how much it costs to cross each link.
Nodes, edges, and weights
The mapping from network to graph is direct.
| Network element | Graph element | Notes |
|---|---|---|
| Router | Node (vertex) | Identified by a router ID, such as `1.1.1.1` |
| Point-to-point link between two routers | Edge | Usually undirected, with a cost in each direction |
| Cost of using a link | Edge weight | Set by metric rules or by an administrator |
| Attached subnet (LAN, stub network) | Destination attached to a node | Often modeled as a leaf, or as a pseudonode for shared segments |
| Path | Sequence of edges | Its cost is the sum of the edge weights |
Destinations in IP routing are prefixes, not routers. A prefix such as 10.1.2.0/24 hangs off the router or routers that attach to it. Routing to a prefix means finding the cheapest path to a router that attaches to it.
Directed edges and asymmetric costs
A physical link is bidirectional, but the cost is a property of an interface. Router A's cost to send out toward B can differ from B's cost to send toward A. For example, an administrator may set different OSPF costs on the two ends. The correct model is therefore a directed graph with two edges per link, one in each direction.
In practice most links have the same cost both ways, and examples often draw one undirected edge. Asymmetric costs are legal, and they can make the forward and return paths between two hosts differ. This matters when you debug with traceroute, because the return path is not shown.
A worked example
Consider five routers, A to E, with these link costs:
The cheapest path from A to E is A, B, C, D, E with cost 1 + 2 + 1 + 2 = 6. The alternative A, B, C, E costs 1 + 2 + 3 = 6, so the two paths tie. The direct-looking A to C edge costs 4 and loses to A, B, C at 3. The path with the fewest hops is not always the cheapest path.
A router does not need the whole path to forward. It needs only the first hop. From A, the next hop for every destination in this example is B, because every cheapest path begins A to B.
Representing the graph in code
The adjacency-dictionary form below is the same structure the Dijkstra example later in this tutorial uses. Each node maps to its neighbors and the cost to reach them.
graph = {
"A": {"B": 1, "C": 4},
"B": {"A": 1, "C": 2, "D": 5},
"C": {"A": 4, "B": 2, "D": 1, "E": 3},
"D": {"B": 5, "C": 1, "E": 2},
"E": {"C": 3, "D": 2},
}
def path_cost(graph, path):
"""Sum the edge weights along a path given as a list of nodes."""
total = 0
for here, there in zip(path, path[1:]):
total += graph[here][there]
return total
print(path_cost(graph, ["A", "B", "C", "D", "E"])) # 6
print(path_cost(graph, ["A", "B", "C", "E"])) # 6
print(path_cost(graph, ["A", "C", "E"])) # 7Because each link appears under both endpoints, the dictionary stores two directed edges per link. To model an asymmetric link, give the two entries different values.
Who knows what: the difference between the families
The graph is the same in both families. What differs is how much of it each router holds.
- In link-state routing, every router builds the complete graph and computes paths itself.
- In distance-vector routing, a router knows only its own links and what neighbors report. It never sees the graph, only distances to destinations through each neighbor.
Keep this contrast in mind. It explains why link-state protocols converge quickly and need more memory, and why distance-vector protocols are simple and can loop.
Properties of the model that affect protocols
- Non-negative weights. Dijkstra's algorithm requires non-negative costs. Routing metrics are always positive, so this holds. Bellman-Ford tolerates negative weights in general, but a negative cycle would make "shortest" meaningless, and routing never uses them.
- Connectivity changes. Links and routers fail and return. The graph is not fixed. A routing protocol is a mechanism for keeping each router's view of the graph, or its distances, current as edges appear and disappear.
- Shared segments. An Ethernet segment with several routers is not a single edge. Link-state protocols model it as an extra node (a pseudonode) connected to every router on the segment. OSPF elects a designated router to play this role. A later chapter covers this.
- Ties. Several paths may have equal cost. Many protocols can install all of them (equal-cost multipath). Others pick one by a tie-break rule. Your model must say which behavior applies.
- Stable versus real network. The graph is only a model. A router may be up while its control plane is overloaded, or a link may pass hello packets but drop data. The graph then claims an edge that does not really work.
Common errors
- Treating hop count as cost. Hop count is one possible metric (RIP uses it). OSPF uses a bandwidth-based cost by default. Always check which metric the protocol uses before predicting a path.
- Assuming symmetric paths. Costs are per direction, so the return path can differ.
- Confusing the router with the destination. Routes are for prefixes. Several routers can originate the same prefix, and the cheapest one wins.
- Forgetting the first-hop reduction. The forwarding table stores a next hop, not the full path.
Exercise
In the five-router graph above, what is the cheapest cost from B to E, and what is the next hop B installs for E? If the C to D cost rose from 1 to 4, how would the answer change?
Show answer
With C to D at 1, B to C to D to E costs 2 + 1 + 2 = 5, and B to C to E costs 2 + 3 = 5. The cost is 5, with next hop C in both cases. If C to D rises to 4, B to C to D to E costs 8 and B to C to E still costs 5. The B to D to E path costs 5 + 2 = 7. The cheapest remains B to C to E at cost 5, next hop C.
Exercise
Why does a routing protocol model one physical link as two directed edges?
Show answer
The cost belongs to an interface and the direction of travel, so each end of a link can have a different cost. Two directed edges let the model represent asymmetric costs, which can make forward and return paths differ.