Minimum Spanning Trees: Kruskal's and Prim's Algorithms

College Depth 68 in the knowledge graph I know this Set as goal
Unlocks 25 downstream topics
mst kruskal prim greedy

Core Idea

An MST connects all vertices with minimum total edge weight. Kruskal's uses union-find to add edges in sorted order, stopping at V-1 edges; Prim's grows the tree by always adding the cheapest edge leaving the current tree. Both run in O((V + E) log V) with efficient data structures.

Explainer

A minimum spanning tree (MST) is a subset of edges in a weighted, connected, undirected graph that connects every vertex with the smallest possible total edge weight, using exactly V − 1 edges and forming no cycles. Think of it as the cheapest possible network that keeps every node reachable from every other node — like finding the least expensive way to wire every house in a neighborhood to the power grid. The existence of an MST follows from the fact that any connected graph has at least one spanning tree, and among all spanning trees, at least one has minimum total weight.

Both Kruskal's and Prim's algorithms work because of a fundamental property called the cut property: for any partition of the vertices into two non-empty sets, the lightest edge crossing the cut must belong to some MST. This greedy insight means you can safely commit to cheap edges without worrying about painting yourself into a corner. Kruskal's algorithm exploits this globally: sort all edges by weight, then iterate through them in order, adding each edge to the MST unless it would create a cycle. You detect cycles using the union-find data structure you already know — if both endpoints are in the same component, skip the edge; otherwise, union their components. After accepting V − 1 edges, you are done. The runtime is dominated by sorting the edges: O(E log E), which is O(E log V) since E ≤ V².

Prim's algorithm takes a local, vertex-centered approach that mirrors Dijkstra's shortest-path algorithm in structure. Start from any vertex, and maintain a priority queue of edges leaving the current tree. Repeatedly extract the minimum-weight edge that connects a tree vertex to a non-tree vertex, add that vertex to the tree, and push its edges onto the queue. With a binary heap, this runs in O((V + E) log V). Prim's tends to perform better on dense graphs (where E is close to V²) especially with an adjacency matrix and a Fibonacci heap bringing the cost down to O(E + V log V), while Kruskal's is simpler and often faster on sparse graphs where sorting a small edge list is cheap.

The choice between the two algorithms is a practical engineering decision. Kruskal's naturally handles disconnected graphs (it produces a minimum spanning forest), works well when edges arrive in a stream, and pairs beautifully with union-find. Prim's is the better choice when the graph is dense or when you already have an adjacency list representation and a priority queue at hand. Both are greedy algorithms — they make locally optimal choices that happen to produce a globally optimal result, which is exactly the property that the cut property guarantees.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsSystems of Equations — Graphing MethodSystems of Equations — Elimination MethodSystems of Three VariablesMatrices IntroductionGraph Representation: Matrices and ListsGraph Paths, Cycles, and ConnectivityTrees and Spanning TreesBinary TreesTree TraversalsBreadth-First Search (BFS)Topological SortDynamic ProgrammingEdit Distance: Levenshtein Distance and DP0/1 Knapsack Problem: Bounded Capacity DPGreedy AlgorithmsMinimum Spanning Trees: Kruskal's and Prim's Algorithms

Longest path: 69 steps · 353 total prerequisite topics

Prerequisites (3)

Leads To (3)