Back to Data Structures & Algorithms
2026-02-075 min read

Prim's Algorithm (Data Structures & Algorithms)

Learn Prim's Algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Understanding Prim's Algorithm is crucial in computer science as it helps find the Minimum Spanning Tree (MST) of a graph. This knowledge can optimize network connections, reduce costs in telecommunications, and solve various real-world problems involving graphs. Familiarity with Prim's Algorithm also enhances problem-solving skills during coding interviews.

The MST is the smallest possible tree that connects all vertices of a graph while minimizing the total edge weight. By finding the MST, we can find the shortest possible way to connect all nodes in a network, which can lead to significant cost savings and improved efficiency.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of:

  1. Data Structures: Arrays, Lists, and Linked Lists
  2. Graph Theory: Graphs, Adjacency Matrix, and Adjacency List
  3. Algorithms: Depth-First Search (DFS)
  4. Python Programming: Variables, Loops, Functions, and Libraries like heapq
  5. Understanding of Priority Queues and Heaps
  6. Familiarity with Big O notation to analyze the time complexity of algorithms

Core Concept

Prim's Algorithm builds a Minimum Spanning Tree (MST) incrementally from an arbitrary starting vertex. The algorithm maintains the smallest unvisited vertices and their weights using a priority queue, which is implemented as a heap in Python.

Here are the steps involved in Prim's Algorithm:

  1. Initialize an empty graph G and a priority queue pq. Add the starting vertex to both G and pq, setting its weight as 0.
  2. While pq is not empty, perform the following steps:
  • Remove the vertex with the smallest weight from pq and add it to the MST (mark it as visited).
  • For each adjacent vertex of the removed vertex that is still unvisited:
  • Update the weight of the adjacent vertex in pq if a shorter path to the adjacent vertex is found.

In this implementation, we will use a min-heap for the priority queue, which ensures that the vertex with the smallest weight is always at the front of the heap.

Worked Example

Let's consider the following graph with weights for edges between vertices:

0 --- 4 --- 1
| | |
3 --- 9 --- 2
|
8 --- 5

Here's how Prim's Algorithm would work on this graph starting from vertex 0:

  1. Initialize an empty graph G and a priority queue pq. Add the starting vertex 0 to both G and pq, setting its weight as 0.
import heapq

G = {
'0': [(4, 1), (3, 9)],
'1': [],
'2': [],
'3': [(0, 4)],
'4': [(3, 9), (1, 5)],
'5': [(8, 5)]
}
pq = [('0', 0)]
heapq.heapify(pq)
  1. While pq is not empty:
  • Remove the vertex with the smallest weight from pq. In this case, it's vertex 0. Add it to the MST and mark it as visited.
visited = {'0': True}
MST = ['0']
  • Update the weights of adjacent unvisited vertices in pq. In this case, we have two adjacent unvisited vertices: 1 and 3. We update their weights as follows:
pq = [('1', 4), ('3', 9)]
  • Repeat the process until the MST includes all vertices.

Remove vertex 1 from pq and add it to MST

visited['1'] = True

MST += ['1']

pq = [('3', 9)]

Remove vertex 3 from pq and add it to MST

visited['3'] = True

MST += ['3']

pq = [('4', 5)]

Remove vertex 4 from pq and add it to MST (the last remaining unvisited vertex)

visited['4'] = True

MST += ['4']


Now, the minimum spanning tree `MST` is:

0 --- 4 --- 3

| | |

Common Mistakes

  1. Not initializing the priority queue correctly: Make sure to initialize it with the starting vertex and its weight set as 0.
  2. Failing to update weights of adjacent vertices in pq when a visited vertex is removed: Always remember to check for adjacent unvisited vertices and update their weights in the priority queue if a shorter path is found.
  3. Not marking visited vertices: Keep track of visited vertices to avoid revisiting them and causing infinite loops or incorrect results.
  4. Incorrect implementation of the priority queue data structure: Use an appropriate data structure for the priority queue, such as a heap, to ensure efficient performance.
  5. Neglecting to handle disconnected graphs: Prim's Algorithm only works on connected graphs. If the graph is disconnected, you should first connect them using additional edges with infinite weight before applying the algorithm.
  6. Implementing an inefficient priority queue data structure: Using an inefficient priority queue implementation can significantly degrade the performance of the algorithm. Always choose a suitable data structure like a binary heap for optimal results.

Common Mistakes (cont'd)

  1. Not properly handling negative edge weights: Prim's Algorithm assumes all edge weights are non-negative. If the graph contains negative edge weights, it may not find the MST or may find an incorrect one. In such cases, you can use Kruskal's Algorithm instead, which handles both positive and negative edge weights.
  2. Not considering self-loops: Prim's Algorithm should exclude self-loops (edges connecting a vertex to itself) from the graph before applying the algorithm. Self-loops do not contribute to the MST and can cause incorrect results if included.
  3. Implementing an inefficient method for finding adjacent vertices: Use an efficient method, such as breadth-first search (BFS), to find all adjacent vertices of a given vertex. This will help improve the performance of the algorithm.
  4. Failing to properly handle duplicate edges: If the graph contains duplicate edges between vertices, they should be removed before applying Prim's Algorithm. Duplicate edges can cause incorrect results and inefficient execution.

Practice Questions

  1. Implement Prim's Algorithm using an adjacency list representation of a graph.
  2. Given a graph with weights for edges between vertices, write a function that finds and returns the minimum spanning tree using Prim's Algorithm.
  3. How would you modify Prim's Algorithm to handle graphs with negative edge weights? (Hint: Use Kruskal's Algorithm instead.)
  4. What is the time complexity of Prim's Algorithm in the worst case scenario? (Answer: O(E log V), where E is the number of edges and V is the number of vertices.)
  5. How can you handle disconnected graphs when applying Prim's Algorithm? (Hint: Connect them using additional edges with infinite weight before applying the algorithm.)
  6. Compare and contrast Prim's Algorithm and Kruskal's Algorithm for finding minimum spanning trees. (Discuss their similarities, differences, time complexities, and handling of negative edge weights.)

FAQ

Why does Prim's Algorithm use a priority queue instead of BFS or DFS?

Prim's Algorithm uses a priority queue to efficiently find the minimum-weight vertex among all unvisited vertices at each step. This is more efficient than BFS and DFS, which visit vertices in no particular order or based on their depth, respectively.

Can Prim's Algorithm be used for directed graphs?

Yes, Prim's Algorithm can be applied to both undirected and directed graphs. However, when dealing with directed graphs, Note that that the algorithm may not find a minimum spanning tree but instead a minimum spanning arborescence (rooted tree).

Is Prim's Algorithm always guaranteed to find the minimum spanning tree?

Prim's Algorithm will find the minimum spanning tree if the input graph is connected and has no negative-weight cycles. If the graph contains negative-weight cycles, Kruskal's Algorithm may be a better choice for finding the minimum spanning tree.

Prim's Algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn