forked from Faizan-Mushtaq-Wani/Hacktoberfest-21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrims-MST.c
55 lines (55 loc) · 1.16 KB
/
Prims-MST.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<stdlib.h>
#define inf 999
void prims(int n,int cost[10][10],int s);
int main()
{
int n,i,j,source,cost[10][10];
printf("enter no of nodes: ");
scanf("%d",&n);
printf("enter cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("enter source vet: ");
scanf("%d",&source);
prims(n,cost,source);
return 0;
}
void prims(int n,int cost[10][10],int s)
{
int vis[10],vet[10],dis[10];
int sum=0,i,j,min,v;
for(i=1;i<=n;i++)
{
vis[i]=0;
dis[i]=cost[s][i];
vet[i]=s;
}
vis[s]=1;
printf("edge in mst\n");
for(i=1;i<n;i++)
{
min=inf;
for(j=1;j<=n;j++)
{
if(vis[j]==0 && dis[j]<min)
{
min=dis[j];
v=j;
}
}
vis[v]=1;
sum=sum+dis[v];
printf("Edge (%d,%d):cost=%d\n",vet[v],v,dis[v]);
for(j=1;j<=n;j++)
{
if(vis[j]==0&& cost[v][j]<dis [j])
{
dis[j]=cost[v][j];
vet[j]=v;
}
}
}
printf("\ntotal cost of mst=%d",sum);
}