-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathPRIMS.C
70 lines (69 loc) · 983 Bytes
/
PRIMS.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
#include<conio.h>
#define I 32767
using namespace std;
int main()
{int cost[8][8]={{I,I,I,I,I,I,I,I},
{I,I,25,I,I,I,5,I},
{I,25,I,12,I,I,I,10},
{I,I,12,I,8,I,I,I},
{I,I,I,8,I,16,I,14},
{I,I,I,I,16,I,20,18},
{I,5,I,I,I,20,I,I},
{I,I,10,I,14,18,I,I}};
int near[8]={I,I,I,I,I,I,I,I};
int t[2][6];
int i,j,u,v,k,n=7,min=I;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{if(cost[i][j]<min)
{min=cost[i][j];
u=i;
v=j;
}
}
}
t[0][0]=u;
t[1][0]=v;
near[u]=0;
near[v]=0;
for(j=1;j<=n;j++)
{if(near[j]!=0)
{
if(cost[j][u]<cost[j][v])
{near[j]=u;
}
else
{near[j]=v;
}
}
}
for(i=1;i<n-1;i++)//this is for t array that means for nodes
{
min=I;
for(j=1;j<=n;j++)
{
if(near[j]!=0 && cost[j][near[j]]<min)
{min=cost[j][near[j]];
k=j;
}
}
t[0][i]=k;
t[1][i]=near[k];
near[k]=0;
for(j=1;j<=n;j++)
{
if(near[j]!=0 && cost[j][k]<cost[j][near[j]])
{
near[j]=k;
}
}
}
for(i=0;i<n-1;i++)
{
printf("(%d,&d)\n",t[0][i],t[1][i]);
}
getch();
return 0;
}