← Course home

Graphs · Lesson 39 of 52

Minimum spanning trees

Trace Kruskal's algorithm and use union-find to avoid cycles.

Full lessonAbout 10 min

You will learn to trace Kruskal's edge choices and explain cycle checks and forest behavior.

Recommended first: Union-find, Graph representations.

On this page 6 sections

The idea

Weights 1, 2, and 4. Both algorithms return total weight 3 and reject 4.

A minimum spanning tree connects every vertex of an undirected weighted graph and has the smallest total edge weight. Prim and Kruskal both return 3 on the triangle A-B weight 1, B-C weight 2, and A-C weight 4. Kruskal sorts edges by weight and adds an edge when its ends lie in different components. The weight-4 edge finds A and C already connected through B, so the union-find cycle check rejects it. Prim grows one tree from A. It adds the cheapest edge that leaves the visited set. That is A-B, then B-C. The edge back to A is ignored because A is already visited. The two algorithms can pick different edges when weights tie. They do not pick different totals on this graph.

A worked trace

StepStateWhat changed
taketotal = 1Take weight 1. The total is 1.
taketotal = 3Take weight 2. The total is 3.
rejecttotal = 3Reject weight 4. The total is 3.

Why it is correct

Kruskal's accepted edges never form a cycle because union refuses an edge whose ends already share a root. A set of n - 1 such edges on n vertices is a tree. It is spanning because every vertex in an edge was merged. The cut property says the cheapest edge leaving a set of visited vertices is safe to add. Prim always adds such an edge. Replacing it with a heavier edge across the same cut would not reduce the total. The rejected weight-4 edge is the unique heaviest edge on the only cycle, which is why both algorithms drop it.

A common mistake

Adding the rejected edge closes the only cycle. A tree on n vertices has n - 1 edges.

What it costs

Kruskal sorts the edges, which is E log E, and then runs a union-find almost-constant find per edge. Prim with a binary heap is proportional to vertices plus edges, times a log. Extra memory is the parent array or the heap, both linear in the vertices for a connected graph. Use either algorithm when the graph is undirected and you want a cheapest connection, not a shortest path from one source. Kruskal is O(E log E) time and uses O(V) extra memory.

Run it

python
from bitsandbytes.graphs.algorithms import UndirectedGraph, prim_minimum_spanning_weightgraph = UndirectedGraph()graph.add_edge('A', 'B', 1)graph.add_edge('B', 'C', 2)graph.add_edge('A', 'C', 4)print(prim_minimum_spanning_weight(graph, 'A'))# prints: 3 

Practice and next step

Open Two-pointer patterns next. Two pointers scan a sorted array from both ends and move only one index at a time.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.graphs.algorithms

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Skip the edge that closes a cycle

Undirected edges of weight 1, 2, and 4. Prim and Kruskal both return total weight 3. The weight-4 edge is the one that closes a cycle.

total
1

State

  1. 01chosen
  2. 11chosen

Take weight 1. The total is 1.

Practice feedback is loading. The lesson explanation remains available above.