Sub topic 14
Applications: Network Design and Single-Linkage Clustering
This chapter assumes you have read the earlier chapters on Kruskal's algorithm and the disjoint-set structure, and that you can read Python classes and Big-O notation. It covers two common uses of MSTs. The first is building the cheapest network. The second is grouping data points into clusters.
Network Design
The textbook use of an MST is connecting sites at the lowest total cost. The sites can be offices, houses on a power grid, or servers in a data center. Model each site as a vertex. Model each possible link as an edge, weighted by what it costs to build. The MST is the cheapest set of links that still reaches every site.
An MST only solves this exact problem. Small changes to the requirements need a different algorithm:
| Requirement | Is an MST the right tool? | Better fit |
|---|---|---|
| Minimum total build cost, all sites connected | Yes | MST |
| Shortest route from one hub to each site | No | Shortest-path tree (Dijkstra) |
| Network must survive any single link failure | No | 2-edge-connected spanning subgraph |
| You may add new junction points between sites | No | Steiner tree (NP-hard; MST gives a 2-approximation) |
Every edge of a tree is a bridge, so a single failed link disconnects the network. Real designs often start from the MST and then add backup links.
Single-Linkage Clustering
Suppose you have n points and the distance between every pair, and you want to split the points into k groups. Single-linkage clustering defines the distance between two clusters as the distance between their closest pair of points. It then keeps merging the two nearest clusters.
That merge rule matches Kruskal's algorithm. Kruskal's processes edges from shortest to longest and joins two components whenever an edge connects them. To get k clusters, run Kruskal's and stop when exactly k components remain. You get the same result by building the full MST and deleting its k − 1 heaviest edges.
The clustering is optimal in a precise sense. Its spacing is the shortest distance between points in different clusters. Among all ways to split the points into k clusters, this one has the largest possible spacing. The spacing equals the next edge Kruskal's would have added.
The code below is self-contained. It returns the clusters and their spacing.
class DisjointSet:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, x):
while self.parent[x] != x:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x
def union(self, a, b):
ra, rb = self.find(a), self.find(b)
if ra == rb:
return False
if self.rank[ra] < self.rank[rb]:
ra, rb = rb, ra
self.parent[rb] = ra
if self.rank[ra] == self.rank[rb]:
self.rank[ra] += 1
return True
def single_linkage(n, edges, k):
# edges: list of (distance, u, v); returns (clusters, spacing)
ds = DisjointSet(n)
components = n
spacing = None
for w, u, v in sorted(edges):
if ds.find(u) == ds.find(v):
continue
if components == k:
spacing = w # first edge that would merge two of the k clusters
break
ds.union(u, v)
components -= 1
clusters = {}
for x in range(n):
clusters.setdefault(ds.find(x), []).append(x)
return list(clusters.values()), spacing
edges = [(1, 0, 1), (2, 1, 2), (9, 2, 3), (1, 3, 4),
(3, 4, 5), (10, 0, 5), (8, 1, 4)]
print(single_linkage(6, edges, 2)) # ([[0, 1, 2], [3, 4, 5]], 8)Single linkage has a known weakness called chaining. A thin line of points between two dense groups can merge them into one cluster. If your data has noisy bridges like this, use other linkage rules (average or complete) or density-based methods.
Exercise
Using the edge list in the code above, what clusters and spacing does single_linkage(6, edges, 3) return?
Show answer
Kruskal's adds (0,1), (3,4), and (1,2), which leaves 3 components: {0,1,2}, {3,4}, and {5}. The next edge, (4,5) with weight 3, joins two different clusters, so the result is ([[0, 1, 2], [3, 4], [5]], 3).
Exercise
A company wants to link 50 branch offices with leased lines so that the total monthly cost is as low as possible. It also requires that any single line can fail without cutting off a branch. Is the MST a valid final answer? Why or why not?
Show answer
No. The MST gives the lowest-cost connected network, but every edge in a tree is a bridge, so losing any one line disconnects some branches. The company needs a 2-edge-connected design. The MST can serve as a starting point that backup links are added to.