Problem Solving

Prim’s Algorithm

A student extends a connected wooden network by one link.

Prim’s Algorithm definition

Prim’s algorithm grows a minimum spanning tree one vertex at a time by choosing a lightest edge from the current tree to a new vertex.

In Spanish
algoritmo de Prim
Topic
Graph algorithms and networks

Prim’s Algorithm in a picture

Prim’s Algorithm diagramAfter starting at A and adding B, the next choice may connect B to C even if A also connects to C.Start with AIts lightest outgoing edge is A–B of weight 2Add BNow compare edges leaving {A, B}Choose B–C of weight 1The resulting tree has weight 3
After starting at A and adding B, the next choice may connect B to C even if A also connects to C.

How it works

Start from any vertex of a connected weighted undirected graph. At each step, compare edges with one endpoint in the growing tree and the other outside. Add one with minimum weight, along with its outside endpoint, until all vertices are included.

Worked examples

A–B weighs 3 and A–C weighs 5. Starting at A, which is chosen first?

A–B.

If B–C weighs 2, which edge is next?

B–C, the lighter edge crossing from {A, B} to C.

What can happen if two crossing edges tie?

Either can be chosen. The minimum total weight remains the same, though the tree may differ.

Common mistake

Compare individual crossing edge weights. Dijkstra instead compares tentative path totals from a start.

Quick check

Which edge is eligible at a typical step?

Can the start vertex be chosen freely?

Show answer

Yes, in a connected graph.

How many accepted edges connect n vertices?

Show answer

n − 1.

Sources

Flashcard

Prim’s Algorithm

Reveal the definition

Prim’s algorithm grows a minimum spanning tree one vertex at a time by choosing a lightest edge from the current tree to a new vertex.

Effortless Math. “Prim’s Algorithm.” Math Dictionary.