Learning path

Full curriculum

Full curriculum

Unit content

Prim's minimum-spanning-tree algorithm

Prim's algorithm constructs a minimum spanning tree by growing one connected tree.

Start from any vertex. Repeatedly choose the lightest edge that connects a vertex already in the tree to a vertex outside it, then add that edge and vertex.

At every step, the current tree defines a cut between included and excluded vertices. Choosing a lightest crossing edge is safe by the cut property.

A priority queue can track the cheapest known connection for each outside vertex. With adjacency lists and a binary heap, a common running-time bound is

$$O((|V|+|E|)\log |V|).$$

Unlike Kruskal's edge-oriented process, Prim maintains one growing connected component throughout the algorithm.