Prim’s algorithms

Comprehensive study notes, diagrams, and exam preparation for Prim’s algorithms.

Prim’s Algorithms

Definition

Prim’s algorithm is a greedy graph algorithm that constructs a minimum spanning tree for a connected, weighted, undirected graph by starting from any vertex and repeatedly selecting the minimum-weight edge that connects a vertex in the current tree to a vertex outside the tree, without forming a cycle.

In simple words, it grows a tree by always choosing the cheapest valid next edge.


Main Content

1. Minimum Spanning Tree (MST)

  • A spanning tree is a subgraph that:
  • includes all vertices of the graph,
  • is connected,
  • contains no cycles.
  • A minimum spanning tree is a spanning tree whose total edge weight is as small as possible.

For example, consider a graph with vertices A, B, C, D and weighted edges:

  • A-B = 1
  • A-C = 4
  • B-C = 2
  • B-D = 5
  • C-D = 3

One possible MST would choose:

  • A-B = 1
  • B-C = 2
  • C-D = 3

Total weight = 1 + 2 + 3 = 6

This is smaller than any other spanning tree for the same graph.

Prim’s algorithm is specifically designed to find such a tree efficiently.


2. Greedy Strategy in Prim’s Algorithm

  • Prim’s algorithm uses the greedy approach, meaning it makes the best local choice at each step.
  • At every stage, it selects the minimum-cost edge that connects:
  • one vertex already in the growing tree, and
  • one vertex not yet in the tree.

This works because:

  • the algorithm never allows a cycle,
  • it always expands the tree using the cheapest available connection,
  • it eventually covers all vertices.

Example idea

Suppose the current tree contains vertex A. The available edges from A are:

  • A-B = 2
  • A-C = 6

Prim’s algorithm chooses A-B because 2 < 6.

Now suppose the tree contains A, B, and the outgoing edges are:

  • A-C = 6
  • B-C = 3
  • B-D = 5

It chooses B-C = 3 next.

This repeated “choose the cheapest edge that expands the tree” rule is the core of Prim’s algorithm.


3. Graph Representation and Data Structures

  • Prim’s algorithm can be implemented using different graph representations:
  • Adjacency matrix
  • Adjacency list
  • The choice of representation affects efficiency.

With adjacency matrix

  • Easy to understand and implement.
  • Best for dense graphs.
  • Time complexity is usually O(V^2).

With adjacency list + priority queue

  • More efficient for sparse graphs.
  • Uses a min-priority queue to quickly select the smallest edge.
  • Time complexity can be O(E log V).

Common supporting data structures

  • key[]: stores the minimum edge weight needed to connect each vertex to the MST.
  • parent[]: stores the predecessor of each vertex in the MST.
  • mstSet[]: marks whether a vertex is already included in the MST.
  • Priority queue: helps repeatedly extract the vertex with the smallest key.

Small visual example

For the graph:

A --1-- B --2-- C
|      /         |
4     3          5
|   /            |
D ------------- E
       6

Prim’s algorithm may start at A, then choose the smallest edge from the tree outward at each step until all vertices are connected.


Working / Process

1. Start with any one vertex

  • Mark this vertex as part of the MST.
  • Set its key value to 0 so it is chosen first.
  • Set all other vertices’ keys to infinity initially.

2. Choose the minimum edge repeatedly

  • From all edges connecting the current tree to unvisited vertices, pick the one with the smallest weight.
  • Add the corresponding vertex and edge to the MST.
  • Update the keys of neighboring vertices if a cheaper connection is found.

3. Continue until all vertices are included

  • Repeat the selection and update process until every vertex is part of the MST.
  • The result is a spanning tree with minimum total cost.

Step-by-step example

Graph edges:

  • A-B = 2
  • A-C = 3
  • B-C = 1
  • B-D = 4
  • C-D = 5

Start from A:

  • Include A
  • Compare edges from A: choose A-B = 2
  • Tree now has A, B
  • From A, B, outgoing edges are:
  • A-C = 3
  • B-C = 1
  • B-D = 4
  • Choose B-C = 1
  • Tree now has A, B, C
  • Outgoing edges to D:
  • B-D = 4
  • C-D = 5
  • Choose B-D = 4

Final MST edges:

  • A-B = 2
  • B-C = 1
  • B-D = 4

Total weight = 7

ASCII view of the chosen MST

    A
    |
    2
    |
    B
   / \
  1   4
 /     \
C       D

Advantages / Applications

  • Finds the minimum spanning tree, which minimizes total connection cost.
  • Useful in network design, such as connecting computers, roads, pipelines, or electrical grids with minimum expense.
  • Efficient and practical for both small and large graphs when implemented with the right data structures.

Additional applications

Telecommunication networks

  • : to connect routers/cities at minimum cost.

Transportation planning

  • : to design roads or railway links efficiently.

Circuit design

  • : to reduce wiring length.

Clustering and image processing

  • : MST-based methods are used in data grouping and segmentation.

Approximation algorithms

  • : MSTs help build solutions for harder optimization problems.

Summary

  • Prim’s algorithm is a greedy method for building a minimum spanning tree.
  • It grows the tree by repeatedly adding the smallest edge that connects a new vertex.
  • It is useful for minimizing total cost in connected weighted graphs.

Important terms to remember

  • spanning tree, minimum spanning tree, greedy algorithm, edge weight, priority queue, adjacency list, adjacency matrix