Finding the minimum spanning tree (MST) of a graph is a fundamental problem in computer science with applications in network design, clustering, and more. Prim’s algorithm provides an efficient way to solve this problem. This post will explain Prim’s algorithm and demonstrate its implementation in Python.
Understanding Prim’s Algorithm
Prim’s algorithm is a greedy algorithm that builds the MST one edge at a time. It starts with a single vertex and iteratively adds the shortest edge connecting a vertex in the MST to a vertex outside the MST until all vertices are included.
The algorithm can be summarized as follows:
- Initialization: Start with an arbitrary vertex and include it in the MST.
- Iteration: While there are vertices not in the MST:
- Find the edge with the minimum weight connecting a vertex in the MST to a vertex not in the MST.
- Add this edge and its corresponding vertex to the MST.
- Termination: The algorithm terminates when all vertices are part of the MST.
Python Implementation
We’ll represent the graph using an adjacency matrix. Each element graph[i][j]
represents the weight of the edge between vertex i
and vertex j
. A value of float('inf')
indicates no edge.
import sys
def prim_mst(graph):
"""
Finds the minimum spanning tree using Prim's algorithm.
Args:
graph: An adjacency matrix representing the graph.
Returns:
A list of tuples representing the edges in the MST,
or None if the graph is not connected.
"""
= len(graph)
num_vertices = []
mst = [False] * num_vertices
visited = [sys.maxsize] * num_vertices
min_edge = [-1] * num_vertices
parent
# Start with an arbitrary vertex (vertex 0 in this case)
0] = 0
min_edge[
for _ in range(num_vertices):
= sys.maxsize
min_weight = -1
min_index
# Find the vertex with the minimum edge weight not yet in MST
for v in range(num_vertices):
if min_edge[v] < min_weight and not visited[v]:
= min_edge[v]
min_weight = v
min_index
if min_index == -1: #Graph is not connected
return None
= True
visited[min_index]
# Add the edge to the MST
if parent[min_index] != -1:
mst.append((parent[min_index], min_index))
# Update minimum edge weights for adjacent vertices
for v in range(num_vertices):
if graph[min_index][v] < min_edge[v] and not visited[v]:
= graph[min_index][v]
min_edge[v] = min_index
parent[v]
return mst
#Example Usage
= [
graph 0, 2, float('inf'), 6, float('inf')],
[2, 0, 3, 8, 5],
[float('inf'), 3, 0, float('inf'), 7],
[6, 8, float('inf'), 0, 9],
[float('inf'), 5, 7, 9, 0]
[
]
= prim_mst(graph)
mst if mst:
print("Edges in the Minimum Spanning Tree:")
for u, v in mst:
print(f"({u}, {v})")
else:
print("Graph is not connected.")
This code efficiently finds the MST using Prim’s algorithm. The use of an adjacency matrix simplifies the implementation, though other graph representations are possible. Remember that the algorithm assumes a connected graph; handling disconnected graphs would require additional checks. This implementation showcases the core logic of Prim’s algorithm, providing a clear and understandable solution for finding the minimum spanning tree.