-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimBhuvan.c
38 lines (38 loc) · 1.01 KB
/
primBhuvan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<stdlib.h>
void main()
{
int min,no_of_edges=0,x,y,v,i,j,s=0;
printf("\nEnter the number of vertices: ");
scanf("%d",&v);
int selected[v],G[v][v];
for(i=0;i<v;i++)
selected[i]=0;
selected[0]=1;
printf("\nEnter the cost adjacency matrix: ");
for(i=0;i<v;i++)
for(j=0;j<v;j++)
scanf("%d",&G[i][j]);
while (no_of_edges<v-1)
{
min=10000;
x=0,y=0;
for(i=0;i<v;i++)
if(selected[i]==1)
{
for(j=0;j<v;j++)
if(selected[j]==0 && G[i][j]!=0)
if(min>G[i][j])
{
min=G[i][j];
x=i;
y=j;
}
}
printf("%d - %d : %d \n",x,y,G[x][y]);
s=s+G[x][y];
selected[y]=1;
no_of_edges++;
}
printf("\nThe minimum cost is %d",s);
}