codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

26 / 65 topics
25Kruskal's Algorithm26Prim's Algorithm
Tutorials/Data Structures & Algorithms/Prim's Algorithm
🧮Data Structures & Algorithms

Prim's Algorithm

Updated 2026-04-20
3 min read

Prim's Algorithm

Introduction

Prim's algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected graph. The MST is a subset of the edges that connects all vertices together, without any cycles and with the minimum possible total edge weight. Prim's algorithm is particularly useful for graphs where the number of vertices is much larger than the number of edges.

Understanding Minimum Spanning Trees

Before diving into Prim's algorithm, let's understand what a Minimum Spanning Tree (MST) is:

  • Connected: All vertices are connected to each other.
  • Undirected: Edges have no direction.
  • No Cycles: The tree structure ensures there are no cycles.
  • Minimum Weight: The sum of the weights of the edges in the MST is minimized.

Prim's Algorithm Overview

Prim's algorithm builds the MST by starting with an arbitrary vertex and repeatedly adding the minimum-weight edge that connects a vertex in the growing tree to a vertex outside the tree. This process continues until all vertices are included in the MST.

Steps of Prim's Algorithm

  1. Initialization: Choose any vertex as the starting point. Initialize a set MST with this vertex and a priority queue (min-heap) with edges connected to this vertex.
  2. Iteration:
    • Extract the edge with the smallest weight from the priority queue.
    • If the destination vertex of this edge is not in MST, add it to MST.
    • Add all edges from this new vertex that are not already in the priority queue.
  3. Termination: Repeat the iteration until all vertices are included in MST.

Implementation

Let's implement Prim's algorithm using Python. We'll use a min-heap to efficiently get the minimum-weight edge.

Code Example

import heapq

def prim(graph, start):
    # Initialize the priority queue with edges from the start vertex
    pq = [(weight, start, neighbor) for neighbor, weight in graph[start].items()]
    heapq.heapify(pq)
    
    # Set to keep track of vertices included in MST
    mst_set = set([start])
    mst_edges = []
    
    while pq:
        weight, u, v = heapq.heappop(pq)
        
        # If the vertex is already in MST, skip it
        if v in mst_set:
            continue
        
        # Add the edge to MST
        mst_edges.append((u, v, weight))
        mst_set.add(v)
        
        # Add all edges from this new vertex that are not already in the priority queue
        for neighbor, weight in graph[v].items():
            if neighbor not in mst_set:
                heapq.heappush(pq, (weight, v, neighbor))
    
    return mst_edges

# Example usage
graph = {
    'A': {'B': 2, 'C': 3},
    'B': {'A': 2, 'D': 1},
    'C': {'A': 3, 'D': 4},
    'D': {'B': 1, 'C': 4}
}

mst = prim(graph, 'A')
print("Minimum Spanning Tree edges:", mst)

Explanation

  • Graph Representation: The graph is represented as an adjacency list where each key is a vertex and the value is a dictionary of neighboring vertices with their edge weights.
  • Priority Queue: We use a min-heap to efficiently get the minimum-weight edge. Python's heapq module provides this functionality.
  • MST Set: A set keeps track of vertices included in the MST to avoid cycles.
  • Edge Addition: For each vertex added to the MST, we add all its edges to the priority queue if they lead to a vertex not yet in the MST.

Best Practices

  1. Graph Representation: Use an adjacency list for efficient access to neighboring vertices and their weights.
  2. Priority Queue: Use a min-heap to efficiently extract the minimum-weight edge. This ensures that the algorithm runs in \(O(E \log V)\) time complexity, where \(E\) is the number of edges and \(V\) is the number of vertices.
  3. Initialization: Choose an arbitrary starting vertex. The choice does not affect the final MST but can impact performance slightly.

Real-World Applications

Prim's algorithm has several real-world applications:

  • Network Design: Designing efficient networks such as telephone networks, computer networks, and transportation networks.
  • Cluster Analysis: Grouping data points into clusters based on similarity.
  • Image Processing: Segmenting images by grouping similar pixels.

Conclusion

Prim's algorithm is a powerful tool for finding the Minimum Spanning Tree of a graph. Its simplicity and efficiency make it suitable for a wide range of applications. By understanding its principles and implementation, you can effectively use Prim's algorithm to solve complex problems involving network optimization and data clustering.

This comprehensive guide provides a detailed explanation of Prim's algorithm, including its steps, implementation, best practices, and real-world applications.


PreviousKruskal's AlgorithmNext Dynamic Programming Basics

Recommended Gear

Kruskal's AlgorithmDynamic Programming Basics