Module 21 of 39

Minimum spanning trees: Kruskal, Prim, and safe edges

Build a minimum spanning tree and understand the cut argument behind Kruskal's and Prim's methods.

Suppose several buildings need network cable. Each possible connection has a cost. We want every building connected, directly or through others, while minimizing the total cost of chosen connections. We do not need every possible cable. If costs are attached to an undirected, connected graph, the answer is a minimum spanning tree (MST): a set of V − 1 edges that reaches all V vertices without a cycle and has the least total weight.

Background for this page. An undirected graph represents two-way connections. A spanning tree connects every vertex without a cycle. Edge weights are costs, and the objective adds the costs of selected edges. Related explanations: Disjoint sets: union-find and path compression · Greedy algorithms: activity selection and exchange arguments.

This is not a shortest-path problem. A shortest-path tree minimizes distances from one chosen starting building. An MST minimizes the sum of all selected edge weights. Those objectives can choose different edges.

A triangle has edge AB cost 1, BC cost 2, and AC cost 2.5. The minimum spanning tree takes AB and BC, while the shortest A to C route uses AC.

Kruskal’s algorithm sorts edges by weight and takes each next edge unless it joins vertices already connected by chosen edges. A disjoint-set structure answers that connectivity question. The result never has a cycle, and after enough safe edges it spans the graph. Sorting dominates the usual running time, giving O(E log E), which is also O(E log V) for a simple graph.

Prim’s algorithm begins at one vertex and repeatedly takes a light edge from the growing tree to a vertex outside it. A priority queue helps find the next candidate. With adjacency lists and a binary heap, a common implementation takes O(E log V) time for a connected graph. Either method can produce different valid MSTs when weights tie.

Why is taking a light crossing edge safe? Imagine a cut that divides vertices into two groups. If an edge crossing that cut has minimum weight among crossing edges, there is an MST containing it. If an MST uses a heavier crossing edge instead, swapping in the light edge keeps the vertices connected without increasing total cost. This exchange argument supports both algorithms; each chooses an edge that is light across an appropriate cut.

The model has boundaries. It assumes each possible connection’s cost is known and that connecting all places is the goal. Real cable plans may have capacity, reliability, legal, or geographical constraints that need a richer model. An MST gives the optimum for the stated graph problem, not for every real installation problem.

Why the cheapest crossing edge is safe

Split vertices into two groups so no already-chosen edge crosses between them. Pick a cheapest crossing edge e. Consider an optimum spanning tree consistent with the choices so far. If it omits e, adding e creates a cycle. That cycle contains another edge crossing the split. Remove that edge. Since e costs no more, the new tree is still optimal and includes e.

This exchange argument explains the safe choices behind Prim and Kruskal. It requires a split that respects the current chosen forest; choosing the globally cheapest remaining edge without checking cycles is insufficient.

If the graph is disconnected, no spanning tree reaches every vertex. Running the method across components produces a minimum spanning forest instead.

Practice. Three places A, B, and C have connection costs AB = 1, BC = 2, and AC = 2.5. Which edges form an MST? Does that answer give the shortest route from A to C?

Answer. Choose AB and BC, total cost 3. The route from A to C in that tree also costs 3, but the direct edge AC costs only 2.5. The MST minimizes total network cost, not the cost of every individual route.

Try another case. Do equal edge costs imply the algorithm is incorrect or the optimum is unique?

Reasoning. Neither. Ties can produce several different minimum spanning trees with the same total cost.

View the full course and choose another topic.

Image preview

Fit