You want to learn about Prim’s Algorithm— a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph, right? The MST is a subset of edges that connects all the vertices together without any cycles and with the minimum possible total edge weight. Prim’s Algorithm builds the MST incrementally, starting from a single vertex and adding the shortest edge that connects the current MST to a vertex not yet in the MST. It’s widely used for network design (e.g., laying cables, building roads) and is efficient for dense graphs (when implemented with a binary heap) or very dense graphs (with an adjacency matrix and linear search).
I. Core Principles of Prim’s Algorithm
Prim’s Algorithm follows a greedy strategy to construct the MST step by step:
- Initialize: Select an arbitrary starting vertex (e.g., vertex 0) and add it to the MST set. Initialize a
keyarray wherekey[v]is the weight of the shortest edge connecting vertexvto the MST set (set to infinity for all vertices except the start vertex, which is 0). Also, maintain aparentarray to track the parent of each vertex in the MST (for path reconstruction). - Iterate to build MST: Repeat the following steps until all vertices are included in the MST:
- Select the minimum key vertex: Pick a vertex
uthat is not in the MST set and has the smallestkeyvalue (this is the greedy choice). - Add to MST: Include
uin the MST set. - Update key values: For each neighbor
vofuthat is not in the MST set, updatekey[v]to be the minimum of its current value and the weight of the edgeu-v. Also, setparent[v] = uif the edgeu-vprovides a shorter connection to the MST.
- Select the minimum key vertex: Pick a vertex
- Terminate: Once all vertices are in the MST, the
parentarray defines the edges of the MST, and the sum of thekeyvalues gives the total weight of the MST.
Key Notes
- Greedy Choice Property: At each step, choosing the smallest edge connecting the MST to the non-MST vertices ensures the MST is built optimally (this is proven by the cut property of MSTs).
- Graph Requirements: The graph must be connected (otherwise, no MST exists) and undirected (Prim’s can be adapted for directed graphs but is rarely used for them).
- Handling Duplicate Edges: The algorithm automatically ignores redundant edges since it only keeps the minimum weight edge to each non-MST vertex.
II. Complete Implementation Code (Python)
Below are two implementations of Prim’s Algorithm:
- Naive Version: Uses linear search to find the minimum key vertex (simple, O(V²) time—good for dense graphs).
- Efficient Version: Uses a min-heap (priority queue) to find the minimum key vertex (O((V + E) log V) time—better for sparse graphs).
Both implementations include MST edge reconstruction and total weight calculation.
python
运行
import heapq
INF = float('inf')
# -------------------------- Naive Prim's Algorithm (O(V²)) --------------------------
def prim_naive(graph):
"""
Naive Prim's Algorithm for MST (adjacency matrix representation)
:param graph: V x V adjacency matrix (graph[i][j] = weight of edge i-j, INF if no edge)
:return: (mst_edges, total_weight) - list of MST edges (u, v, weight) and total MST weight
"""
V = len(graph)
# key: minimum weight edge connecting vertex to MST
key = [INF] * V
# parent: parent of each vertex in MST (for edge reconstruction)
parent = [-1] * V
# mst_set: boolean array to track vertices in MST
mst_set = [False] * V
# Start with vertex 0 (arbitrary choice)
key[0] = 0
parent[0] = -1 # Root of MST has no parent
total_weight = 0
for _ in range(V):
# Step 1: Find the vertex u with minimum key value not in MST
u = -1
min_key = INF
for i in range(V):
if not mst_set[i] and key[i] < min_key:
min_key = key[i]
u = i
# Add u to MST and accumulate weight
mst_set[u] = True
total_weight += key[u]
# Step 2: Update key values for neighbors of u
for v in range(V):
# Update key[v] if v not in MST and edge u-v exists and weight < key[v]
if not mst_set[v] and graph[u][v] != INF and graph[u][v] < key[v]:
key[v] = graph[u][v]
parent[v] = u
# Reconstruct MST edges
mst_edges = []
for v in range(1, V): # Skip root (parent[0] = -1)
u = parent[v]
mst_edges.append((u, v, graph[u][v]))
return mst_edges, total_weight
# -------------------------- Efficient Prim's Algorithm (O((V+E)logV)) --------------------------
def prim_heap(graph):
"""
Efficient Prim's Algorithm for MST (adjacency list representation)
:param graph: Adjacency list (dict: vertex -> list of (neighbor, weight))
:return: (mst_edges, total_weight) - list of MST edges (u, v, weight) and total MST weight
"""
# Get all vertices (assume vertices are numbered 0 to V-1)
V = len(graph)
key = {v: INF for v in graph}
parent = {v: -1 for v in graph}
mst_set = {v: False for v in graph}
# Min-heap: (key_value, vertex)
heap = []
# Start with vertex 0
start = 0
key[start] = 0
heapq.heappush(heap, (0, start))
total_weight = 0
while heap:
# Step 1: Extract vertex u with minimum key value
current_key, u = heapq.heappop(heap)
# Skip if u is already in MST (duplicate entry in heap)
if mst_set[u]:
continue
# Add u to MST and accumulate weight
mst_set[u] = True
total_weight += current_key
# Step 2: Update key values for neighbors of u
for v, weight in graph[u]:
if not mst_set[v] and weight < key[v]:
key[v] = weight
parent[v] = u
heapq.heappush(heap, (key[v], v))
# Reconstruct MST edges
mst_edges = []
for v in graph:
if parent[v] != -1: # Skip root
u = parent[v]
mst_edges.append((u, v, key[v]))
return mst_edges, total_weight
# -------------------------- Test Cases --------------------------
if __name__ == "__main__":
# Test 1: Dense graph (adjacency matrix for naive Prim's)
# Nodes: 0,1,2,3
graph_matrix = [
[0, 2, INF, 3],
[2, 0, 1, INF],
[INF, 1, 0, 4],
[3, INF, 4, 0]
]
mst_naive_edges, mst_naive_weight = prim_naive(graph_matrix)
print("=== Naive Prim's (Adjacency Matrix) ===")
print("MST Edges (u, v, weight):", mst_naive_edges)
print("Total MST Weight:", mst_naive_weight)
# Expected edges: [(0,1,2), (1,2,1), (0,3,3)] | Total weight: 6
# Test 2: Sparse graph (adjacency list for heap Prim's)
# Same graph as above (adjacency list format)
graph_list = {
0: [(1, 2), (3, 3)],
1: [(0, 2), (2, 1)],
2: [(1, 1), (3, 4)],
3: [(0, 3), (2, 4)]
}
mst_heap_edges, mst_heap_weight = prim_heap(graph_list)
print("\n=== Efficient Prim's (Adjacency List) ===")
print("MST Edges (u, v, weight):", mst_heap_edges)
print("Total MST Weight:", mst_heap_weight)
# Expected same as naive version
III. Key Code Explanations
1. Naive Implementation (Adjacency Matrix)
- Graph Representation: Uses a
V x Vadjacency matrix wheregraph[i][j]is the weight of the edge betweeniandj(INF if no edge). This is efficient for dense graphs (whereE ≈ V²). - Minimum Key Search: A linear scan over all vertices finds the minimum key vertex not in the MST (O(V) per iteration, leading to O(V²) total time).
- Key Update: For each neighbor
vofu(checked via the adjacency matrix), the key value is updated if the edgeu-vis shorter than the current key forv.
2. Efficient Implementation (Adjacency List + Min-Heap)
- Graph Representation: Uses an adjacency list (dictionary) where each vertex maps to a list of
(neighbor, weight)tuples—ideal for sparse graphs (whereE << V²). - Min-Heap Usage: The heap stores
(key_value, vertex)pairs and allows extracting the minimum key vertex in O(log V) time. Duplicate entries in the heap are skipped (sincemst_setmarks processed vertices). - Key Update: When a shorter edge to a non-MST vertex is found, the new key value is pushed to the heap (old, larger entries are ignored when popped).
3. MST Reconstruction
- The
parentarray/ dictionary tracks the parent of each vertex in the MST. For each vertex (excluding the root), the edge(parent[v], v)is part of the MST, with weight equal tokey[v]. - The total weight of the MST is the sum of all
keyvalues (since eachkey[v]is the weight of the edge connectingvto the MST).
IV. Algorithm Performance Analysis
| Metric | Naive Prim’s (Adjacency Matrix) | Efficient Prim’s (Adjacency List + Heap) | Explanation |
|---|---|---|---|
| Time Complexity | O(V²) | O((V + E) log V) | – Naive: V iterations, each with O(V) search + O(V) update.- Heap: Each edge/vertex is processed once, with O(log V) heap operations. |
| Space Complexity | O(V²) (matrix) + O(V) (arrays) | O(V + E) (list) + O(V) (heap/arrays) | – Naive: Dominated by the adjacency matrix.- Heap: Dominated by the adjacency list (sparse graphs use less space). |
| Best For | Dense graphs (E ≈ V²) | Sparse graphs (E << V²) | – Naive: Linear search is faster than heap for dense graphs (no heap overhead).- Heap: Avoids O(V²) time for sparse graphs. |
Cut Property (Why Prim’s Works)
The cut property states that for any cut (partition of vertices into two sets) in a connected graph, the minimum weight edge crossing the cut is part of the MST. Prim’s Algorithm repeatedly applies this property: the MST set and non-MST set form a cut, and the minimum key vertex is the minimum edge crossing the cut.
V. Prim’s vs. Kruskal’s Algorithm (MST Algorithms)
| Feature | Prim’s Algorithm | Kruskal’s Algorithm |
|---|---|---|
| Strategy | Builds MST from a single vertex (grows a tree) | Builds MST by adding edges in order of increasing weight (avoids cycles) |
| Data Structure | Adjacency matrix (naive) / adjacency list + heap (efficient) | Adjacency list + Union-Find (Disjoint Set Union, DSU) |
| Time Complexity | O(V²) (naive) / O((V+E)logV) (heap) | O(E log E) (dominated by edge sorting) |
| Best For | Dense graphs | Sparse graphs |
| Cycle Handling | Automatically avoids cycles (only connects to non-MST vertices) | Uses Union-Find to detect and skip cycles |
| Graph Type | Undirected, connected | Undirected, connected (works for disconnected graphs to find MST forest) |
VI. Practical Applications
- Network Design: Designing minimum-cost networks (e.g., laying fiber-optic cables, electrical grids, or water pipelines) to connect multiple locations with minimal total cost.
- Cluster Analysis: Used in machine learning for clustering data points— the MST is used to identify clusters by removing the largest edges in the MST.
- Image Segmentation: In computer vision, Prim’s Algorithm helps segment images by grouping pixels with similar features (e.g., color, intensity) into regions via an MST.
- Road/Transportation Planning: Finding the minimum set of roads to pave to connect all cities in a region with minimal construction cost.
- Circuit Design: Optimizing the layout of electrical circuits by connecting components with the shortest possible wires (minimizing material cost and signal delay).
Summary
Comparison to Kruskal’s: Prim’s is better for dense graphs, while Kruskal’s excels with sparse graphs and can handle disconnected graphs (MST forests).
Prim’s Algorithm is a greedy algorithm that constructs the MST of a connected, undirected, weighted graph by incrementally adding the shortest edge connecting the current MST to non-MST vertices.
Two main implementations:
Naive (O(V²)): Uses adjacency matrix and linear search—ideal for dense graphs.
Efficient (O((V+E)logV)): Uses adjacency list and min-heap—better for sparse graphs.
Core principle: Relies on the cut property of MSTs, making the greedy choice of the minimum crossing edge optimal.
Key applications: Network design, clustering, image segmentation, and transportation planning.
- High-Performance Waterproof Solar Connectors
- Durable IP68 Waterproof Solar Connectors for Outdoor Use
- High-Quality Tinned Copper Material for Durability
- High-Quality Tinned Copper Material for Long Service Life
- Y Branch Parallel Solar Connector for Enhanced Power
- 10AWG Tinned Copper Solar Battery Cables
- NEMA 5-15P to Powercon Extension Cable Overview
- Dual Port USB 3.0 Adapter for Optimal Speed
- 4-Pin XLR Connector: Reliable Audio Transmission
- 4mm Banana to 2mm Pin Connector: Your Audio Solution
- 12GB/s Mini SAS to U.2 NVMe Cable for Fast Data Transfer
- CAB-STK-E Stacking Cable: 40Gbps Performance
- High-Performance CAB-STK-E Stacking Cable Explained
- Best 10M OS2 LC to LC Fiber Patch Cable for Data Centers
- Mini SAS HD Cable: Boost Data Transfer at 12 Gbps
- Multi Rate SFP+: Enhance Your Network Speed
- Best 6.35mm to MIDI Din Cable for Clear Sound
- 15 Pin SATA Power Splitter: Solutions for Your Device Needs
- 9-Pin S-Video Cable: Enhance Your Viewing Experience
- USB 9-Pin to Standard USB 2.0 Adapter: Easy Connection
- 3 Pin to 4 Pin Fan Adapter: Optimize Your PC Cooling
- S-Video to RCA Cable: High-Definition Connections Made Easy
- 6.35mm TS Extension Cable: High-Quality Sound Solution
- BlackBerry Curve 9360: Key Features and Specs






















Leave a comment