Minimum spanning tree
Минимальное остовное дерево
A minimum spanning tree connects every vertex of an undirected graph without forming a cycle. Its edges have the lowest total weight of any tree that connects all the vertices.
Also known as: Kruskal, Prim
What is a minimum spanning tree?
Suppose several buildings need to be connected by fiber. You have a list of possible cable runs and the cost of each one. A building can reach another through intermediate buildings, so you don't need a direct cable between every pair. If backup routes aren't required, a minimum spanning tree (MST) tells you which runs to build for the lowest total cost.
The buildings are vertices, the possible runs are undirected edges, and the costs are edge weights. A spanning tree uses only edges from the original graph, includes all V vertices, and has no cycles. It always has exactly V − 1 edges. Every spanning tree therefore uses the same number of edges; the quantity being minimized is their total weight.
Two ways to find one
- Kruskal's algorithm starts with each vertex in its own connected component. It sorts all edges from lightest to heaviest, taking an edge only when it joins two different components. Union-Find, also called a disjoint-set data structure, tracks those components. On a connected graph, sorting dominates the running time:
O(E log E)forEedges. - Prim's algorithm starts at any vertex and grows a single tree. At each step, it chooses the cheapest edge from a vertex already in the tree to one outside it. With adjacency lists and a binary heap that supports updating priorities, it runs in
O(E log V)on a connected graph.
Worked example
Consider these undirected edges:
A—B: 2 B—C: 3 A—C: 4
C—D: 5 B—D: 8
Kruskal takes A—B and B—C, then skips A—C because A and C are already connected. It takes C—D and stops: all four vertices are connected using three edges, at a total cost of 10. Notice that the tree's path from A to C costs 5, while the original graph has a direct edge costing 4. An MST minimizes the cost of the whole network, not the distance between any particular pair of vertices. That's a different problem from the one Dijkstra's algorithm solves.
Uses and edge cases
An MST models the cheapest network when connectivity is enough. It does not provide redundancy: removing any tree edge disconnects the tree. MSTs also appear in single-linkage clustering, where removing the k − 1 heaviest tree edges produces k groups. Negative edge weights work with both algorithms. Tied weights can allow multiple optimal trees, but they don't guarantee it. If the graph is disconnected, no tree spans it; running Kruskal over all the edges instead yields a minimum spanning forest, one tree per connected component.
In a coding interview
Expect to explain why a greedy choice is safe. Split the vertices into two nonempty groups: any lightest edge crossing that split belongs to at least one MST. To see why, start with an MST that doesn't contain that edge and add it. This creates a cycle with another edge crossing the same split. Remove that other edge to get a spanning tree whose cost is no higher. This is the cut property, and Prim applies it at each step using the current tree and the remaining vertices as the two groups.