Professional Documents
Culture Documents
Experiment No. 5
Aim: Find Minimum Cost Spanning Tree of a given undirected graph using Prims
algorithm.
Required Software/ Software Tool
- Windows Operating System
-C/C++
Theory:
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the
greedy approach. Prim's algorithm shares a similarity with the shortest path first
algorithms.Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a
single tree and keeps on adding new nodes to the spanning tree from the given graph.
Remove all loops and parallel edges from the given graph. In case of parallel
edges, keep the one which has the least cost associated and remove all others.
Mahavir Education Trust's
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with weight 7
and 8, respectively. We choose the edge S,A as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out
from it. We select the one which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will
check all the edges again. However, we will choose only the least cost edge. In this
case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4,etc.
Mahavir Education Trust's
After adding node D to the spanning tree, we now have two edges going out of it
having the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the
next step will again yield edge 2 as the least cost. Hence, we are showing a spanning
tree with both edges included.
We may find that the output spanning tree of the same graph using two different
algorithms is same.
Implementing the Solution Writing Source Code:
//Tanishka Mayekar int min_dist, v1, v2,total=0;
# include<stdio.h> for (i=0 ; i<nodes ; i++)
# include<conio.h> // Initialize the selected vertices list
# define SIZE 20 select[i] = 0;
# define INFINITY 32767 printf("\n\n The Minimal Spanning Tree
Is :\n"); select[0] = 1;
/* This function finds the minimal
spanning tree by Prim's Algorithm */ for (k=1 ; k<nodes ; k++)
void Prim(int G[][SIZE], int nodes) {
{ min_dist = INFINITY;
int select[SIZE], i, j, k; for (i=0 ; i<nodes ; i++) // Select an
edge such that one vertex is
Mahavir Education Trust's
{ nodes=4;
// selected and other is not and the edge n=4;
for (j=0 ; j<nodes ; j++)
for (i=0 ; i<nodes ; i++)
// has the least weight.
// Initialize the graph
{
for (j=0 ; j<nodes ; j++)
if (G[i][j] && ((select[i] && !select[j])
G[i][j] = 0; G[0][1]=G[1][0]=1;
|| (!select[i] && select[j])))
G[0][2]=G[2][0]=5;
{
G[0][3]=G[3][0]=2;
if (G[i][j] <min_dist)
G[2][3]=G[3][2]=3;
//obtained edge with minimum wt
printf("\n Graph is created");
{
printf("\n\t");
min_dist = G[i][j]; v1 = i;
Prim(G,nodes);
v2 = j; //picking up those vertices
return(0);
}
}
}
}
}
printf("\n Edge (%d %d )and weight =
%d",v1,v2,min_dist);
select[v1] = select[v2] = 1;
total =total+min_dist;
}
printf("\n\n\t Total Path Length Is =
%d",total);
}
int main()
{
int G[SIZE][SIZE], nodes;
int v1, v2, length, i, j, n;
printf("\n\t Prim'S Algorithm\n");
Mahavir Education Trust's
Conclusion:
In this experiment we have calculated the Minimum Cost Spanning Tree of a given
undirected graph using Prim’s algorithm.
.