-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMST.c
More file actions
66 lines (56 loc) · 1007 Bytes
/
Copy pathMST.c
File metadata and controls
66 lines (56 loc) · 1007 Bytes
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
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#define inf 999
int main(){
int i,j,vc,val,count=0,min,minCost=0,a,b;
printf("Enter the number of vertices: ");
scanf("%d",&vc);
int adj[vc][vc];
int found[vc];
for (int i=0;i<vc;i++)
found[i]=-1;
for (i=0;i<vc;i++){
for (j=0;j<vc;j++)
if (i==j)
adj[i][i]=inf;
else
adj[i][j]=-1;
}
for (i=0;i<vc;i++){
for (j=0;j<vc;j++){
if (adj[i][j]==-1){
printf("Enter the Cost of %d to %d : ",i,j);
scanf("%d",&val);
if (val==0){
adj[i][j]=inf;
adj[j][i]=inf;
}
else{
adj[i][j]=val;
adj[j][i]=val;
}
}
}
}
while(count<vc-1){
for (i=0,min=inf;i<vc;i++){
for (j=0;j<vc;j++){
if (adj[i][j]<min){
min=adj[i][j];
a=i;
b=j;
}
}
}
adj[a][b]=inf;
adj[b][a]=inf;
if (found[a] == -1 || found[b]==-1){
count++;
minCost+=min;
found[a]=1;
found[b]=1;
printf("%d to %d : Cost %d \n",a,b,min);
}
}
printf("Minimum Cost is : %d ",minCost);
return 0;
}