Prim’s Algorithm
May 21, 2023
Prim’s algorithm is a greedy algorithm that finds a minimum spanning tree (MST) for a weighted undirected graph. The algorithm operates by building the MST one vertex at a time, starting with an arbitrary vertex. At each step, the algorithm selects the edge with the minimum weight that connects a vertex in the MST to a vertex not yet in the MST. The algorithm continues until all vertices are included in the MST.
Purpose and Usage
Prim’s algorithm is mainly used to find the minimum spanning tree of a weighted undirected graph. An MST is a subset of the edges of an undirected graph that connects all vertices and has the minimum possible total edge weight. An MST can be used to optimize network design problems, such as designing a communication network with minimum cost.
Brief History and Development
Prim’s algorithm was first described by Czech computer scientist Vojtěch Jarník in 1930. However, it was independently discovered and published by American mathematician Robert C. Prim in 1957 and by Dutch computer scientist Edsger Dijkstra in 1959. Prim’s algorithm was originally designed to solve the problem of finding the minimum-cost spanning tree in a communication network. It was later modified to solve more general problems in computer science and other fields.
Key Concepts and Principles
The following are the key concepts and principles of Prim’s algorithm:
Greedy Algorithm
Prim’s algorithm is a greedy algorithm because it always chooses the locally optimal solution at each step. The algorithm selects the edge with the minimum weight that connects a vertex in the MST to a vertex not yet in the MST.
Minimum Spanning Tree
A minimum spanning tree is a subset of the edges of a connected, undirected graph that connects all vertices and has the minimum possible total edge weight. Prim’s algorithm is used to find the minimum spanning tree of a weighted undirected graph.
Priority Queue
Prim’s algorithm uses a priority queue to keep track of the minimum-weight edges that connect vertices in the MST to vertices not yet in the MST. The priority queue is implemented using a heap data structure to efficiently find the minimum-weight edge.
Visited and Unvisited Vertices
Prim’s algorithm keeps track of which vertices are in the MST and which are not. Initially, all vertices are unvisited. As the algorithm progresses, the vertices are either added to the MST or remain unvisited.
Pseudocode and Implementation Details
The following is the pseudocode for Prim’s algorithm:
1. Initialize the MST with a single vertex, chosen arbitrarily from the graph.
2. Initialize a priority queue with all the edges that connect the selected vertex to the other vertices.
3. While the priority queue is not empty:
a. Extract the edge with the minimum weight from the priority queue.
b. If the edge connects a vertex in the MST to a vertex not yet in the MST:
i. Add the edge to the MST.
ii. Add the unvisited vertex to the MST.
iii. Add all the edges that connect the new vertex to the priority queue.
4. Return the MST.
In step 1, the MST is initialized with a single vertex, which is chosen arbitrarily from the graph. In step 2, a priority queue is initialized with all the edges that connect the selected vertex to the other vertices. The priority queue is implemented using a heap data structure to efficiently find the minimum-weight edge. In step 3, the algorithm continues until the priority queue is empty. In step 3a, the algorithm extracts the edge with the minimum weight from the priority queue. In step 3b, the algorithm checks whether the edge connects a vertex in the MST to a vertex not yet in the MST. If the edge does connect the MST to a non-MST vertex, the algorithm adds the edge to the MST, adds the unvisited vertex to the MST, and adds all the edges that connect the new vertex to the priority queue. In step 4, the algorithm returns the MST.
Examples and Use Cases
Here is an example of Prim’s algorithm in action:
Suppose we have the following weighted undirected graph:
1
A------B
|\ |
2 | \ | 3
| \ |
C------D
4
We will use Prim’s algorithm to find the minimum spanning tree of this graph. We arbitrarily choose vertex A as the starting vertex. The MST is initialized with vertex A and the priority queue is initialized with the edges (A,B,1) and (A,C,2). The algorithm proceeds as follows:
- Extract edge (A,C,2) from the priority queue.
- Add edge (A,C,2) to the MST and add vertex C to the MST.
- Add edges (C,B,3) and (C,D,4) to the priority queue.
- Extract edge (C,B,3) from the priority queue.
- Add edge (C,B,3) to the MST and add vertex B to the MST.
- Add edge (B,D,1) to the priority queue.
- Extract edge (B,D,1) from the priority queue.
- Add edge (B,D,1) to the MST and add vertex D to the MST.
- The priority queue is now empty, so the algorithm terminates.
The resulting MST is:
1
A------B
|
3 | 1
|
C------D
The total weight of the MST is 5.
Advantages and Disadvantages
The advantages of Prim’s algorithm are as follows:
- It always produces a minimum spanning tree.
- It is easy to implement.
- It has a time complexity of O(E log V), where E is the number of edges and V is the number of vertices.
The disadvantages of Prim’s algorithm are as follows:
- It does not work on disconnected graphs because it only produces a minimum spanning tree for a connected graph.
- It can be slow for graphs with a large number of edges because it uses a priority queue.
Related Algorithms or Variations
Prim’s algorithm is related to other algorithms for finding minimum spanning trees, such as Kruskal’s algorithm and Boruvka’s algorithm. Kruskal’s algorithm is another popular algorithm for finding a minimum spanning tree. It operates by sorting the edges by weight and adding them to the MST in ascending order until all vertices are included. Boruvka’s algorithm is a parallel algorithm that operates by repeatedly finding the cheapest edge for each vertex and adding it to the MST.