-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
300 lines (244 loc) · 7.61 KB
/
prim.cpp
File metadata and controls
300 lines (244 loc) · 7.61 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
// C / C++ program for Prim's MST for adjacency list representation of graph
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include<algorithm>
//a node in neighbourhood list
struct BrotherNode
{
int vertex;
double weight;
struct BrotherNode* next;
};
// a neighbourhood list
struct BrotherList
{
struct BrotherNode *head; // pointer to head node of list
};
// the graph stores the array of brotherhood lists.
// and V is the number of vertices
struct Graph
{
int V;
struct BrotherList* array;
};
// create a new brother list node
struct BrotherNode* newBrotherNode(int vertex, double weight)
{
struct BrotherNode* newNode =
(struct BrotherNode*) malloc(sizeof(struct BrotherNode));
newNode->vertex = vertex;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}
// make a graph of V vertices
struct Graph* makenewGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// making array of brotherhood lists. length of array=V
graph->array = (struct BrotherList*) malloc(V * sizeof(struct BrotherList));
for (int i = 0; i < V; ++i) // set the head of every list as NULL
graph->array[i].head = NULL;
return graph;
}
// if you want to add a new edge to your graph
void makenew_edge(struct Graph* graph, int point1, int point2, double weight)
{
// The edge is going between point 1 & point2. point 2 is added as brother of point 1
struct BrotherNode* newNode = newBrotherNode(point2, weight);
newNode->next = graph->array[point1].head;
graph->array[point1].head = newNode;
// similarliy point 1 is also brother of point 2
newNode = newBrotherNode(point1, weight);
newNode->next = graph->array[point2].head;
graph->array[point2].head = newNode;
}
// structure of a node in heap
struct nodeofheap
{
int value;
int key;
};
// making a min heap
struct MinHeap
{
// at any general time, number of elements in the heap
int total;
int max; // the maximum limit of heap
int *location; //in order to change the key value
struct nodeofheap **array;
};
// making a fresh node of heap
struct nodeofheap* freshnodeofheap(int value, int key)
{
struct nodeofheap* nodeofheap =(struct nodeofheap*) malloc(sizeof(struct nodeofheap));
nodeofheap->value = value;
nodeofheap->key = key;
return nodeofheap;
}
// making a fresh heap
struct MinHeap* makefreshheap(int max)
{
struct MinHeap* minHeap =(struct MinHeap*) malloc(sizeof(struct MinHeap));
minHeap->location = (int *)malloc(max * sizeof(int));
minHeap->total = 0;
minHeap->max = max;
minHeap->array =(struct nodeofheap**) malloc(max * sizeof(struct nodeofheap*));
return minHeap;
}
// helping function for Heapify, swaps two nodes
void swapnodeofheap(struct nodeofheap** a, struct nodeofheap** b)
{
struct nodeofheap* temp = *a;
*a = *b;
*b = temp;
}
// A standard function to heapify at given nodeindex
// This function also updates locationition of nodes when they are swapped.
// locationition is needed for updatekey()
void Heapifyfunc(struct MinHeap* minHeap, int nodeindex)
{
int minimum, left, right;
minimum = nodeindex;
left = 2 * nodeindex + 1;
right = 2 * nodeindex + 2;
if ((left < minHeap->total) &&(minHeap->array[left]->key < minHeap->array[minimum]->key) )
minimum = left;
if ((right < minHeap->total) &&(minHeap->array[right]->key < minHeap->array[minimum]->key) )
minimum = right;
if (minimum != nodeindex)
{
// in the following manner, nodes will be swapped
nodeofheap *minimumNode = minHeap->array[minimum];
nodeofheap *nodeindexNode = minHeap->array[nodeindex];
minHeap->location[minimumNode->value] = nodeindex;
minHeap->location[nodeindexNode->value] = minimum;
swapnodeofheap(&minHeap->array[minimum], &minHeap->array[nodeindex]);
Heapifyfunc(minHeap, minimum);
}
}
// check if the heap is empty or not
int checkifempty(struct MinHeap* minHeap)
{
return (minHeap->total == 0);
}
// find the mininum node
struct nodeofheap* getminNode(struct MinHeap* minHeap)
{
if (checkifempty(minHeap)==1)
return NULL;
struct nodeofheap* root = minHeap->array[0];
struct nodeofheap* lastNode = minHeap->array[minHeap->total - 1];
minHeap->array[0] = lastNode;
minHeap->location[root->value] = minHeap->total-1;
minHeap->location[lastNode->value] = 0;
// decrease the number of nodes in the heap
--minHeap->total;
Heapifyfunc(minHeap, 0);
return root;
}
// updating key of any given vertex v
void updatekey(struct MinHeap* minHeap, int value, int key)
{
// find index of v in the array
int i = minHeap->location[value];
minHeap->array[i]->key = key;
// do until the heap is settled , if child is less than parent , swap them
while (i && minHeap->array[i]->key < minHeap->array[(i - 1) / 2]->key)
{
minHeap->location[minHeap->array[i]->value] = (i-1)/2;
minHeap->location[minHeap->array[(i-1)/2]->value] = i;
swapnodeofheap(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
// check if node is in min heap or not
int isInMinHeap(struct MinHeap *minHeap, int value)
{
if (minHeap->location[value] < minHeap->total)
return 1;
return 0;
}
struct make_mstedge{
int u,v; double w;
};
typedef struct make_mstedge mstedge;
struct compare{
bool operator()(const mstedge & lhs, const mstedge &rhs){return lhs.w < rhs.w;}
};
// the function that we will use finally to print our answer
void printMST(mstedge mst[], double sum, int n)
{
printf("%.02lf\n",sum);
for (int i = 1; i < n; ++i)
{
printf("%d %d %.02lf\n",mst[i].u,mst[i].v, mst[i].w);
}
}
// applying the prims algorithm
void primalgo(struct Graph* graph)
{
int V = graph->V;
int parent[V]; // Array to store our minimum spanning tree
double key[V];
double sum=0;
struct MinHeap* minHeap = makefreshheap(V);
// except root , all vertices get the key as infinite
for (int v = 1; v < V; ++v)
{
parent[v] = -1;
key[v] = INT_MAX;
minHeap->array[v] = freshnodeofheap(v, key[v]);
minHeap->location[v] = v;
}
// root key is 0
key[0] = 0;
minHeap->array[0] = freshnodeofheap(0, key[0]);
minHeap->location[0] = 0;
minHeap->total = V;
while (!checkifempty(minHeap))
{
// get the vertex which has minimum key
struct nodeofheap* nodeofheap = getminNode(minHeap);
int u = nodeofheap->value;
// check and update all neighbours of u
struct BrotherNode* random = graph->array[u].head;
while (random != NULL)
{
int v = random->vertex;
//if the vertex v has not been considered, take and update it
if (isInMinHeap(minHeap, v) && random->weight < key[v])
{
key[v] = random->weight;
parent[v] = u;
updatekey(minHeap, v, key[v]);
}
random = random->next;
}
}
mstedge mst[V];
for(int i=1;i<V;i++)
{
mst[i].v = i;
mst[i].u = parent[i];
mst[i].w = key[i];
sum+=key[i];
}
std::sort(mst+1, mst+V,compare());
// print edges of MST
printMST(mst,sum,V);
}
// Driver program to test above functions
int main()
{
// Let us create the graph given in above fugure
int V;
scanf("%d",&V);
struct Graph* graph = makenewGraph(V);
int u,v;double w;
while (!feof(stdin) && scanf("%d %d %lf",&u,&v,&w)){ makenew_edge(graph, u, v, w);}
primalgo(graph);
return 0;
}