Experiment No: 8
1.
Experiment vision:
Write a program to study and implement minimum spanning tree using Prim's algorithm.
2.
OBJECTIVE: Program to implement minimum spanning
tree from prim’s algorithm and
connected node to each other in such way that not become a cycle.
3. THEORY:
Prim's algorithm is an algorithm in graph theory that
finds a minimum spanning tree for a connected weighted graph. This means it
finds a sub set of the edges that forms a tree that includes every vertex,
where the total weight of all the edges in the tree is minimized. If the graph
is not connected, then it will only find a minimum spanning tree for one of the
connected components.
It works as follows:
•
create a tree containing a single vertex, chosen arbitrarily from the graph
•
create a set containing all the edges in the graph
• loop
until every edge in the set connects two vertices in the tree
Ø remove from the set an edge with minimum weight
that connects a vertex in the tree with
a vertex not in the tree
Ø Add that edge to the tree
4. PROGRAM:
#include<stdio.h>
int a,b,u,v,n,i,j,ne=1;
int
visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency
matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun
cost=%d",mincost);
}
5. OUTPUT:
------------------------------------------
------------------------------------------
6. conclusion:
Thus a
C program for prim’s algorithm has been implemented
and executed successfully.
7. REMARK:
In this algorithm, we
find the minimum path from source to destination and measure that path. Source
path must be start with first node of that graph.
8. Discussion Questions:
1) what
is prims algorithm?
_________________________________________________________________________________________________________________________________________________________
2) what is the difference between prims
algorithm and kruskal algorithm.
________________________________________________________________________________________________________________________________________________________