Sub topic 2
Modeling a network as a weighted graph
This chapter assumes you know basic IP addressing, what a routing table is, and the graph terms node, edge, weight, and shortest path. It builds the model that every later chapter uses.
Routers as nodes, links as edges
A routing protocol does not reason about cables and interfaces directly. It reasons about a graph:
- Each router is a node.
- Each point-to-point link between two routers is an edge.
- Each edge has a weight, called its cost.
The goal of routing then becomes a graph problem. For each destination, find the lowest-cost path from this node and record only the first step, the next hop. The forwarding table stores that first step and nothing more.
The direct-looking path R1, R3, R4 costs 4 + 1 = 5. The path through R2 costs 1 + 2 + 1 = 4, so R1 installs R2 as its next hop toward R4. The path with the fewest hops is not always the cheapest.
What the graph leaves out
The graph is an abstraction, so some details are deliberately dropped:
- Hosts and switches. A LAN with many hosts is usually collapsed into the subnet attached to a router.
- Subnets. Routers advertise the prefixes attached to them. The graph only describes how routers connect to each other.
- Parallel links. Two links between the same routers are treated as one edge with the cheaper cost, or as equal-cost paths.
Directed or undirected
Physically a link is usually two-way, but the cost can differ by direction. Each router sets the cost of its own outgoing interface. So the honest model is a directed graph, where R1 to R2 and R2 to R1 are separate edges. Most networks configure both ends the same way, which makes the graph behave as if it were undirected.
The next code block stores the graph as a dictionary, the same node-to-neighbor-to-cost shape used by the Dijkstra example later in the tutorial. It also checks for symmetry and sums a path.
graph = {
"R1": {"R2": 1, "R3": 4},
"R2": {"R1": 1, "R3": 2, "R4": 5},
"R3": {"R1": 4, "R2": 2, "R4": 1},
"R4": {"R2": 5, "R3": 1},
}
def is_symmetric(g):
return all(g[b].get(a) == cost
for a, nbrs in g.items()
for b, cost in nbrs.items())
def path_cost(g, path):
return sum(g[a][b] for a, b in zip(path, path[1:]))
print(is_symmetric(graph))
print(path_cost(graph, ["R1", "R2", "R3", "R4"]))
print(path_cost(graph, ["R1", "R3", "R4"]))It prints True, 4, and 5.
Common errors
- Counting hops instead of summing costs. Only RIP uses hop count as its metric. Other protocols sum the per-link costs.
- Forgetting that costs belong to interfaces. A cost of 5 on R2's side of a link does not set R4's cost for the same link.
- Treating the graph as fixed. Links fail and costs change, so each protocol must rebuild its view of this graph. The two families do that in different ways.
Exercise
In the graph above, the R2–R4 link fails. What is the cheapest path from R1 to R4, and what is its cost?
Show answer
R1, R2, R3, R4 still works because it does not use the R2–R4 link. Its cost is 1 + 2 + 1 = 4, so the answer is unchanged.