Minimum Spanning Tree Ready
vertices 0
edges 0
tree weight 0
  • Current
  • Looking at
  • Done
  • In the tree
  • Rejected (cycle)

Ready

Comparisons 0 Shifts 0
Ready

Edges are written A-B:4 (letters A–H, weights 1–99) · the graph must be connected · Keyboard: ← → step · Space play / pause · R reset

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Cheapest way to connect everything

A SPANNING TREE of a connected, undirected, weighted graph is a set of V − 1 edges that connects all the vertices with no cycle. The MINIMUM spanning tree is the one with the smallest total weight. Both algorithms are GREEDY: they build the answer one cheapest-safe choice at a time. Kruskal looks at the edges (it needs a way to ask "are these two already connected?"), Prim looks at the vertices (it keeps key[v], the cheapest edge reaching v).

struct Edge { int u, v, w; };

// Kruskal: disjoint sets answer "already connected?"
int parent[V];                         // parent[x] = x : x is the leader of its set
int find(int x) {                      // the leader of x's set
    while (parent[x] != x) x = parent[x];
    return x;
}

// Prim: the cheapest edge from the tree to every vertex
int key[V];                            // key[v] = cheapest edge found so far
int parent[V];                         // parent[v] = the tree vertex it joins
int inTree[V];

Kruskal's algorithm

  1. Sort all edges by weight, lightest first.
  2. Give every vertex its own set.
  3. Take the next edge: if its ends are in DIFFERENT sets, keep it and merge the sets.
  4. Same set = it would close a cycle: skip it. Stop at V − 1 edges.

Prim's algorithm

  1. Start the tree at any vertex (key 0); all other keys are ∞.
  2. Add the vertex outside the tree with the smallest key.
  3. For each of its neighbours outside the tree: if the edge is lighter than its key, update key and parent.
  4. Repeat until every vertex is in the tree.

Why does greedy work?

  1. Cut property: for any split of the vertices into two groups, the lightest edge across the split is in some MST.
  2. Prim uses the split "tree | everything else".
  3. Kruskal's lightest safe edge always crosses some split of two different sets.
  4. So taking the cheapest safe edge never ruins the optimum.

Detecting a cycle

  1. Kruskal keeps disjoint sets of vertices.
  2. find(u) = find(v) means u and v are already connected.
  3. Adding the edge u–v would then close a cycle.
  4. Different sets: no cycle, so union the two sets.

Kruskal or Prim?

  1. Sparse graph (few edges): Kruskal is simple and fast.
  2. Dense graph (many edges): Prim with an array is O(V²).
  3. Both give a tree of the same minimum weight.
  4. With equal weights the TREE may differ, but never the total.

Where it is used

  1. Laying cable, pipes or roads at least cost.
  2. Network design: connect all offices cheaply.
  3. Clustering: cut the heaviest MST edges to split data into groups.
  4. A building block for approximate travelling-salesman routes.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 KRUSKAL(G)
2 sort the edges by weight
3 for every vertex v: MAKE-SET(v)
4 tree ← empty
5 for each edge (u, v, w) in sorted order
6 if FIND(u) ≠ FIND(v) then
7 add (u, v) to tree
8 UNION(u, v)
9 return tree
Complexity

How fast is each operation?

V = vertices, E = edges. Kruskal is dominated by sorting the edges. Prim with a simple array scans for the minimum V times (O(V²)); with a binary heap it is O(E log V).

Operation Best Average Worst Speed
Kruskal (sort + disjoint sets) O(E log E) O(E log E) O(E log E) Grows with n
Prim (array) O(V²) O(V²) O(V²) Grows with n
Prim (binary heap) O(E log V) O(E log V) O(E log V) Grows with n
Find / Union (with path compression) O(1) almost O(1) O(log V) Instant

Kruskal vs Prim

Kruskal Prim
Grows A forest that merges into one tree One tree from a start vertex
Works with Edges (sorted by weight) Vertices (key[v])
Cycle check Disjoint sets (find / union) Never needed: only vertices outside the tree are added
Time O(E log E) O(V²) with an array, O(E log V) with a heap
Better for Sparse graphs Dense graphs
Disconnected graph Gives a spanning FOREST Only reaches the start's part

Spanning tree facts

Fact Because
Every spanning tree has exactly V − 1 edges Fewer edges leave a vertex out; more edges close a cycle
A connected graph has at least one spanning tree Keep removing an edge from a cycle until none is left
If all weights are different, the MST is unique There is never a tie to break differently
The MST is NOT the shortest path tree It minimises the total weight, not the distance from one vertex
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 8 correct
Question 1

A connected graph has 9 vertices. How many edges does any spanning tree of it have?

Question 2

In Kruskal's algorithm, when is an edge rejected?

Question 3

In which order does Kruskal's algorithm look at the edges?

Question 4

In Prim's algorithm, what does key[v] store?

Question 5

Prim and Kruskal are both run on the same connected graph. What is certain about their results?

Question 6

Which algorithm is usually the better choice for a graph with very FEW edges?

Question 7

Kruskal's algorithm can stop as soon as…

Question 8

The minimum spanning tree gives the shortest path from the start vertex to every other vertex.

Assessment

Graphs quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.