-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskal.c
287 lines (247 loc) · 6.77 KB
/
kruskal.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Name - Rajendra singh
Roll no. 111601017
Question - Kruskal Alogrithm
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct edgenode//structure for edgenode
{
int src;
int des;
int weight;
}edgenode;
edgenode *edgearray[200];//to store input graph
edgenode *mst[200];//to store mst
typedef struct heapnode//structure for heapnode
{
int src;
int des;
int weight;
}heapnode;
heapnode *newheapnode(edgenode *);
typedef struct heap//heap structure
{
int currentsize; //current size of heap
int maxsize; // maximum size of heap of min heap
struct heapnode **edgeheaparray; //ptr to the array of address(ptr to node) of heapnode in the array
}heap;
int V; //no. of vertices
int E; //no. of edges
int start;
int parent[200];
int rank[200];
//FUNTION DECLARATION
void init();
void readfile();
void insertadjver(int, int ,int , int );
heap *Buildheap(int );
void swap(heapnode **, heapnode **);
heapnode *extractMin(heap *);
void minHeapify(heap *, int );
void makeset(int);
int find(int );
int Union(int,int);
void kruskal();
edgenode *newedgenode(int , int , int );
//MAIN FUNCTION
int main()/////////////////////////////////////////////////////////////////////////////////////////
{
int i;
init();//INITIALISATION
readfile();//READ FILE FROM TERMINAL
kruskal();//Kruskal FUNCTION CALLED
printf("\n\nMST IS: \n" );//PRINTING MST
int weightsum =0;
for (i = 0; i < V-1; i++)
{
weightsum = weightsum + mst[i]->weight;
printf("edge = (%d,%d), edge weight = %d \n",mst[i]->src, mst[i]->des,mst[i]->weight);
}
printf("\nAND WEIGHT OF MST IS : %d\n**************************************\n", weightsum);
return 0;
}
//Kruskal FUNCTION
void kruskal()///////////////////////////////////////////////////////////////////
{
int i, u;
heap *h = Buildheap(E);//building heap with 0 element adding then node 1 by 1 by copying form edgenode
for (i = 0; i < V; i++)//makeset for all vertices
makeset(i);
int e = 1;
while (e < V)//extractMin AND Union until we didn't get V-1 edges in MST
{
heapnode *p = extractMin(h);
if(Union(p->src, p->des) == 1)
{
mst[e-1] = newedgenode(p->src, p->des, p->weight);//e-1, since e starting from 1
e++;
}
}
}
heap *Buildheap(int maxsize)///////////////////////////////////////////////////////////////////////////////////
{
int i;
heap *h = (heap*)malloc(sizeof(heap));
h->currentsize = 0;
h->maxsize = maxsize;
h->edgeheaparray = (heapnode **)malloc(maxsize*sizeof(heapnode *));
for(i = 0; i<E; i++)
{
h->edgeheaparray[i]= newheapnode(edgearray[i]);
h->currentsize++;
}
for (i = (E-1)/2; i >= 0; i--)
{
minHeapify(h,i); //minHeapify at all node having child staring form bottom
}
return(h);
}
void minHeapify(heap *h, int now)//heapify all element contected below now///////////////////////////////////////////////////////////////////////////////////
{
int child;//child node position
while((now*2+2) <= (h->currentsize))//while right child index is less or equal to heapsize
{
child = now*2 + 1;
if (child != h->currentsize && (h->edgeheaparray[child+1])->weight < (h->edgeheaparray[child])->weight) //gives the minimum child of now
{
child++;
}
if((h->edgeheaparray[now])->weight > (h->edgeheaparray[child])->weight)//if heap[now] is greater(i.e unfit) than minimum child
{
swap(&h->edgeheaparray[now], &h->edgeheaparray[child] );
}
else
{
return;
}
now = child;
}
}
void makeset(int u)//////////////////////////////////////////////////////////////////////////////////////
{
parent[u] = u;
rank[u] = 0;
}
int find(int u)//////////////////////////////////////////////////////////////////////////////////////
{
if(parent[u] != u)
{
u = parent[u];
parent[u] = find(u);/////////////////////??????
}
return(u);
}
int Union(int u, int v)////////////////////////////////////////////////////////
{
int x,y;
x = find(u);
y = find(v);
//printf("x = %d,y = %d\n", x, y);
if(x == y)
{
return(0);
}
if(rank[x] > rank[y])
{
parent[y] = x;
}
else if(rank[x] < rank[y])
{
parent[x] = y;
}
else//when both rank equal
{
parent[y] = x;
rank[x]++;
}
return(1);
}
//FUNTION FOR INITIALISATION
void init()//////////////////////////////////////////////////////////////////////////////////////
{
int i;
for (i = 0; i < V; i++)
{
edgearray[i] = NULL;
}
}
void readfile()///////////////////////////////////////////////////////////////////////////////////
{
int u, v, weight;
int success,r,w;
int i = 0;
printf("\n**************************************\nGRAPH IS :\n" );
scanf("%d", &V);//reading no. vertex
printf("\n%d\n",V);
scanf("%d", &E);//reading no. edge
printf("%d\n",E);
for(i=0; i<E; i++)
{
scanf("%d", &u);
scanf("%d", &v);
scanf("%d", &weight);
printf("%d %d %d\n",u,v,weight);
insertadjver(i,u,v,weight);
}
}
void insertadjver(int i, int src,int des, int weight) //src is source vertex and des is destination vertex/////////////////////////////////////////////////
{
edgearray[i] = newedgenode(src, des, weight);// (i)th edge
}
edgenode *newedgenode(int src, int des, int weight)///////////////////////////////////////////////////////////////////////////////////
{
edgenode *p;
p = (edgenode *)malloc(sizeof(edgenode));
p->src = src;
p->des = des;
p->weight = weight;
// p->next = NULL;
return(p);
}
heapnode *newheapnode(edgenode *q)///////////////////////////////////////////////////////////////////////////////////
{
heapnode *p;
p = (heapnode *)malloc(sizeof(heapnode));
p->src = q->src;
p->des = q->des;
p->weight = q->weight;
return(p);
}
void swap( heapnode**p, heapnode**q)// swap function for swaping ptr to heapnode//////////////////////////////////////////////////////////
{
heapnode *temp = *p;
*p = *q;
*q = temp;
}
heapnode *extractMin(heap *h)///////////////////////////////////////////////////////////////////////////////////
{
heapnode *minHeapnode, *lastHeapnode;
minHeapnode = h->edgeheaparray[0]; //topmost node is the minimum
lastHeapnode = h->edgeheaparray[h->currentsize-1];//last node i.e currentsize-1(since heap starting with 0)
// printf("heap bfr extractMin is:" );
// for (int i = 0; i < h->currentsize; i++) {
// printf("%d\t", h->edgeheaparray[i]->weight);
// }
// printf("\n");
h->edgeheaparray[0] = lastHeapnode;
h->currentsize--;//reducing the currentsize
minHeapify(h,0);//calling the minHeapify at starting node
// printf("heap aftr extractMin is: ");
// for (int i = 0; i < h->currentsize; i++) {
// printf("%d\t", h->edgeheaparray[i]->weight);
// }
// printf("\n");
return(minHeapnode);
}
/*
SAMPLE INPUT GRAPH :
4
5
0 1 2
0 3 7
1 2 10
1 3 3
2 3 8
*/