Sub topic 1
Weighted Undirected Graphs and Minimum Spanning Trees
Prerequisites
This chapter assumes you know graph terms such as vertex, edge, path and cycle. You should also be able to read Big-O notation and basic Python lists and tuples.
Weighted Undirected Graphs and Their Representations
A weighted undirected graph gives every edge a number called its weight. An edge {u, v} can be followed in either direction. The weight usually stands for a cost, a distance or a delay.
There are two common ways to store such a graph. An edge list stores (weight, u, v) tuples, which suits algorithms that sort edges. An adjacency list stores, for each vertex, its (neighbor, weight) pairs, which suits algorithms that explore outward from a vertex. In an undirected graph, each edge appears twice in the adjacency list.
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]
n = 4
adj = [[] for _ in range(n)]
for w, u, v in edges:
adj[u].append((v, w))
adj[v].append((u, w))
print(adj)
# [[(1, 1), (2, 4)], [(0, 1), (2, 3), (3, 2)], [(0, 4), (1, 3), (3, 5)], [(1, 2), (2, 5)]]Spanning Trees: Connecting Every Vertex with V-1 Edges
A spanning tree of a connected graph is a subset of its edges that connects all V vertices and contains no cycle. Every spanning tree has exactly V − 1 edges. If you remove one of its edges, the graph splits into two parts. If you add any other edge, you create exactly one cycle.
The Minimum Spanning Tree Problem
A minimum spanning tree (MST) is a spanning tree whose total edge weight is as small as possible. In the example graph, the MST uses edges 0–1, 1–3 and 1–2, for a total weight of 6. A graph can have more than one MST, but every MST has the same total weight.
The Cut Property: Why the Lightest Crossing Edge Is Safe
A cut divides the vertices into two non-empty groups. An edge crosses the cut if its two ends lie in different groups. The cut property says that for any cut, the lightest crossing edge belongs to some MST. If that edge is strictly lighter than every other crossing edge, it belongs to every MST.
The reason is an exchange argument. Suppose an MST skips that edge. Adding the edge creates a cycle, and the cycle must cross the cut a second time through a heavier edge. Swapping the heavier edge out for the lighter one keeps the tree connected and does not raise its total weight.
The Cycle Property: Which Edges Can Never Be in an MST
The cycle property says that if an edge is strictly the heaviest edge on some cycle, it is in no MST. In the example graph, edge 0–2 (weight 4) is the heaviest on the cycle 0–1–2, and edge 2–3 (weight 5) is the heaviest on the cycle 1–2–3. Both are excluded, which leaves the MST of weight 6.
Exercise
In the example graph, take the cut {0, 1} versus {2, 3}. Which edges cross it, and which one does the cut property guarantee is in the MST?
Show answer
The crossing edges are 0–2 (weight 4), 1–2 (weight 3) and 1–3 (weight 2). Edge 1–3 is strictly the lightest, so it is in every MST.
Exercise
A connected graph has 10 vertices. How many edges does each of its spanning trees have?
Show answer
9, because a spanning tree always has V − 1 edges.