Table of Contents
Definition of Prim's Algorithm in DAA
Prim’s Algorithm in DAA states that a spanning tree of an undirected connected graph is its connected acyclic subgraph (i.e., a tree) that contains all the vertices of the graph. If such a graph has weights assigned to its edges, a minimum spanning tree is its spanning tree of the smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges.
The minimum spanning tree problem is the problem of finding a minimum spanning tree for a given weighted connected graph.
In the above figure, T1 is the minimum spanning tree.
If we were to try constructing a minimum spanning tree by exhaustive search, we would face two main obstacles.
- First, the number of spanning trees grows exponentially with the graph size (at least for dense graphs).
- Second, generating all spanning trees for a given graph is not easy; in fact, it is more difficult than finding a minimum spanning tree for a weighted graph by using one of several efficient algorithms available for this problem.
So, Prim’s Algorithm in DAA constructs a minimum spanning tree through a sequence of expanding subtrees. The initial subtree in such a sequence consists of a single vertex selected arbitrarily from the set V of the graph’s vertices.
- On each iteration, the algorithm expands the current tree in the greedy manner by simply attaching to its nearest vertex not in that tree.
- The algorithm stops after all the graph’s vertices have been included in the tree being constructed.
Pseudocode for Prim's Algorithm
//Input: A weighted connected graph G = {V, E}
//Output: ET , the set of edges composing a minimum spanning tree of G
VT ← {v0} //the set of tree vertices can be initialized with any vertex
ET ← ∅
for i ← 1 to |V| − 1 do
find a minimum-weight edge e∗ = (v∗, u∗) among all the edges (v, u)
such that v is in VT and u is in V − VT
VT ← VT ∪ {u∗}
ET ← ET ∪ {e∗}
return ET
Here vertices are not adjacent to any of the tree vertices can be given the ∞ label indicating their "infinite" distance to the tree vertices and a null label for the name of the nearest tree vertex.
- After we have identified a vertex u∗ to be added to the tree, we need to perform two operations:
1. Move u∗ from the set V − VT to the set of tree vertices VT.
2. For each remaining vertex u in V − VT that is connected to u∗ by a shorter edge than the u’s current distance label, update its labels by u∗ and the weight of the edge between u∗ and u, respectively.
Note: In the above image the parenthesized labels of a vertex in the middle column indicate the nearest tree vertex and edge weight; notice that selected vertices and edges are shown in bold.
You might get a question here that “Does Prim’s algorithm always yield a minimum spanning tree?” The answer to this question is yes.
Proof:
Let us prove by induction that each of the subtrees Ti, i = 0, …, n − 1, generated by Prim’s algorithm in DAA is a part (i.e., a subgraph) of some minimum spanning tree.
- The basis of the induction is trivial, since T0 consists of a single vertex and hence must be a part of any minimum spanning tree.
- For the inductive step, let us assume that Ti−1 is part of some minimum spanning tree T.
- We need to prove that Ti, generated from Ti−1 by Prim’s algorithm, is also a part of a minimum spanning tree.
- We prove this by contradiction by assuming that no minimum spanning tree of the graph can contain Ti. Let ei = (v, u) be the minimum weight edge from a vertex in Ti−1 to a vertex not in Ti−1 used by Prim’s algorithm to expand Ti−1 to Ti.
- By our assumption, ei cannot belong to any minimum spanning tree, including T. Therefore, if we add ei to T, a cycle must be formed.
Visualization Tool to find the Minimum Spanning Tree (MST)
Time Complexity of Prim's Algorithm in DAA
Deletion of the smallest element from and insertion of a new element into a min-heap of size n are O(log n) operations, and so is the operation of changing an element’s priority.
- If a graph is represented by its adjacency lists and the priority queue is implemented as a min-heap, the running time of the algorithm is in O(|E| log |V|).
- This is because the algorithm performs |V| − 1 deletions of the smallest element and makes |E| verifications and, possibly, changes of an element’s priority in a min-heap of size not exceeding |V|.
- Each of these operations, as noted earlier, is a O(log |V|) operation. Hence, the running time of this implementation of Prim’s algorithm is in,
(|V| − 1 + |E|)O(log |V|) = O(|E| log |V|)
Since in a connected graph, |V| − 1 ≤ |E|.
Prim's Algorithm in C
#include
#include
int visited[10]={0}, cost[10][10], min, mincost=0;
int input(int);
int display(int);
int prims(int);
int input(int num) {
int i, j;
printf("\nEnter the adjacency matrix\n\n");
for(i=1; i<=num; i++) {
for(j=1; j<=num; j++) {
printf("value of cost[%d][%d] : ",i,j);
scanf("%d", &cost[i][j]);
}
}
return 0;
}
int display(int num) {
int i, j;
printf("\nThe cost of adjacency matrix\n\n");
for(i=1; i<=num; i++) {
for(j=1; j<=num; j++) {
printf("%d", cost[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
int prims(int num) {
int i, j, ne=1, a, b, u, v;
for(i=1; i<=num; i++) {
for(j=1; j<=num; j++) {
if(cost[i][j]==0)
cost[i][j]=999;
}
}
visited[1]=1;
while(ne < num) {
for(i=1,min=999;i<=num;i++)
for(j=1;j<=num;j++)
if(cost[i][j]< min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i;
b=v=j;
}
printf("\n Edge %d:(%d - %d) cost:%d",ne++,a,b,min);
mincost=mincost+min;
visited[b]=1;
cost[a][b]=cost[b][a]=999;
}
printf("\n\n\n Minimun cost=%d",mincost);
}
int main() {
int num;
printf("\n\t\t\tPrim's Algorithm");
printf("\n\nEnter the number of nodes= ");
scanf("%d", &num);
input(num);
display(num);
prims(num);
getch();
return 0;
}
References
GitHub: https://gist.github.com/discover
Java Point: https://www.javatpoint.com/prim-algorithm
Related Articles
- Depth First Search Algorithm
- Kruskal’s Algorithm (Minimum Spanning Tree)
- Floyd-Warshall Algorithm
- Horspool’s Algorithm in DAA
- Huffman Coding Algorithm in DAA
- Kadane’s Algorithm
- Knapsack Problem using Branch and Bound
- BMI Calculator by Age and Gender
- Classic Snake Game using Java
- Fake Coin Problem
- Advanced Meme Generator
- Whack-a-Mole Game Using JavaScript
- Karnaugh Map Solver
- Radix Sort Algorithm
- Heap Sort Algorithm
Frequently Asked Questions
Prim’s algorithm is a greedy algorithm, which finds a minimum spanning tree for a weighted undirected graph.
- The Prim’s algorithm starts by choosing the root vertex and moves closely from vertex to vertex.
- In contrast, starting from the smallest weighted edge, Kruskal’s algorithm aids in the creation of the minimum spanning tree.
- The time complexity is O(V logV + E logV) = O(E logV), making it the same as Kruskal’s algorithm. However, Prim’s algorithm can be improved using Fibonacci Heaps to O(E + logV).
- Time complexity is explained above👆 with equations.
Prim’s algorithm enables the determination of a graph’s minimum spanning tree. Because it is a greedy algorithm, it chooses the option that is currently available.
- When you specify node i, Dijkstra’s algorithm determines the shortest path between it and every other node. As a result, you receive the tree with the shortest distance from node i.
- For a given graph, the Prims algorithm yields the minimum spanning tree. a tree with all nodes connected and the lowest possible total cost.