-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim_MST.cpp
497 lines (472 loc) · 11.5 KB
/
Prim_MST.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
#define HEAP_SIZE 1000000
class edge
{
public:
int to;
int wei;
edge()
{
this->to = -1;
this->wei = -1;
}
edge(int a, int b)
{
this->to = a;
this->wei = b;
}
};
class Node
{
public:
edge data;
Node *next;
Node(edge data)
{
this->data = data;
this->next = NULL;
}
};
class LinkedList
{
private:
Node *LinkedListHead;
int LengthOfLinkedList;
void partition(Node *head, Node **front, Node **back)
{
Node *fast, *slow;
if (head == NULL || head->next == NULL)
{
*front = head; // &a
*back = NULL; //&b
}
else
{
slow = head;
fast = head->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*front = head; // &a
*back = slow->next; //&b
slow->next = NULL;
}
}
Node *mergeLists(Node *a, Node *b)
{
Node *mergedList = NULL;
if (a == NULL)
{
return b;
}
else if (b == NULL)
{
return a;
}
if (a->data.wei <= b->data.wei)
{
mergedList = a;
mergedList->next = mergeLists(a->next, b);
}
else
{
mergedList = b;
mergedList->next = mergeLists(a, b->next);
}
return mergedList;
}
void mergeSort(Node **source)
{
Node *head = *source;
Node *a = NULL, *b = NULL;
if (head == NULL || head->next == NULL)
return;
partition(head, &a, &b);
mergeSort(&a);
mergeSort(&b);
*source = mergeLists(a, b);
}
public:
LinkedList()
{
LinkedListHead = NULL;
LengthOfLinkedList = 0;
}
int length()
{
return this->LengthOfLinkedList;
}
void push_back(edge data)
{
int position = length();
Node *newNode = new Node(data);
Node *temp = LinkedListHead;
if (LinkedListHead == NULL)
{
LinkedListHead = newNode;
newNode->next = NULL;
}
else
{
for (int i = 0; i < position - 1; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("Invalid Position!\n");
return;
}
}
newNode->next = temp->next;
temp->next = newNode;
}
LengthOfLinkedList++;
}
void printlist()
{
Node *temp = LinkedListHead;
if (LinkedListHead == NULL)
{
cout << "List is empty!" << endl;
return;
}
while (temp != NULL)
{
cout << temp->data.wei;
temp = temp->next;
}
cout << "\n";
}
void linkedlist_sort()
{
mergeSort(&this->LinkedListHead);
}
edge delete_front()
{
if (LinkedListHead == NULL)
{
return edge(-1, -1);
}
Node *temp = LinkedListHead;
LinkedListHead = temp->next;
LengthOfLinkedList--;
return temp->data;
}
bool is_empty()
{
if (LengthOfLinkedList == 0 && LinkedListHead == NULL)
return true;
else
return false;
}
};
class Heap
{
edge *arr;
int capacity; // Capacity
int heap_size; // Current Number of Node
int get_parent(int i)
{
return (i - 1) / 2;
}
int left_child(int i)
{
return 2 * i + 1;
}
int right_child(int i)
{
return 2 * i + 2;
}
void swap(edge *x, edge *y)
{
edge temp = *x;
*x = *y;
*y = temp;
}
public:
Heap(int cap)
{
this->capacity = cap;
heap_size = 0;
arr = new edge[cap];
}
edge top_heap()
{
return arr[0];
}
edge pop_heap()
{
if(heap_size<= 0) return {-1,-1};
if (heap_size==1){
heap_size--;
return arr[0];
}
edge root = arr[0];
arr[0]=arr[heap_size-1];
heap_size--;
Minheeapify(0);
return root;
}
void Minheeapify(int i){
int l = left_child(i);
int r= right_child(i);
int smallest= i;
if(l<heap_size && arr[l].wei<arr[i].wei ){
smallest=l;
}
if(r<heap_size && arr[r].wei<arr[smallest].wei){
smallest=r;
}
if(smallest!=i){
swap(&arr[i], &arr[smallest]);
Minheeapify(smallest);
}
}
void push_heap(edge given)
{
if (heap_size == capacity)
{
cout << "\nOverflow: Could not insertKey\n";
return;
}
heap_size++;
int i = heap_size - 1;
arr[i] = given;
while(i != 0 && arr[get_parent(i)].wei > arr[i].wei)
{
swap(&arr[i], &arr[get_parent(i)]);
i = get_parent(i);
}
}
void printheap(){
for(int i=0; i<heap_size; i++){
cout<<arr[i].wei<<endl;
}
cout<<endl;
}
bool isempty(){
if(heap_size<=0) return true;
else return false;
}
};
class Graph
{
// adjlist
vector<edge> *adjl;
// Number Of Vertex
int nver;
void quickSort(edge arr[], int start, int end)
{
if (start < end)
{
int pIndex = partition(arr, start, end);
quickSort(arr, start, pIndex - 1);
quickSort(arr, pIndex + 1, end);
}
}
int partition(edge arr[], int start, int end)
{
int pIndex = start;
edge pivot = arr[end];
int i;
for (i = start; i < end; i++)
{
if (arr[i].wei >= pivot.wei)
{
swap(&arr[i], &arr[pIndex]);
pIndex++;
}
}
swap(&arr[end], &arr[pIndex]);
return pIndex;
}
void swap(edge *x, edge *y)
{
edge t = *x;
*x = *y;
*y = t;
}
public:
Graph(int n)
{
this->nver = n;
adjl = new vector<edge>[n];
}
void addedge(int x, int y, int w)
{
adjl[x].push_back(edge(y, w));
adjl[y].push_back(edge(x, w));
}
int prim_mst_linkedlist()
{
// Container To Keep The Active Edges
LinkedList container = LinkedList();
// Visited Array that denotes whether a node has been in MST or not
bool *visited = new bool[nver]{0};
int ans = 0;
container.push_back(edge(0, 0));
while (!container.is_empty())
{
container.linkedlist_sort();
edge temp = container.delete_front();
int to = temp.to;
int wei = temp.wei;
if (visited[to])
{
continue;
}
// otherwise take the current edge
ans += wei;
visited[to] = 1;
for (auto x : adjl[to])
{
if (visited[x.to] == 0)
{
container.push_back(x);
}
}
}
return ans;
}
int prim_mst_array()
{
// Container To Keep The Active Edges
edge container[HEAP_SIZE];
int containerlen = 0;
// Visited Array that denotes whether a node has been in MST or not
bool *visited = new bool[nver]{0};
int ans = 0;
container[containerlen] = edge(0, 0);
containerlen++;
while (containerlen > 0)
{
quickSort(container, 0, containerlen - 1);
edge temp = container[containerlen - 1];
containerlen--;
int to = temp.to;
int wei = temp.wei;
if (visited[to])
{
continue;
}
// otherwise take the current edge
ans += wei;
visited[to] = 1;
for (auto x : adjl[to])
{
if (visited[x.to] == 0)
{
container[containerlen] = x;
containerlen++;
}
}
}
return ans;
}
int prim_mst_bin_min_heap(){
// Container To Keep The Active Edges
Heap container= Heap(HEAP_SIZE);
// Visited Array that denotes whether a node has been in MST or not
bool *visited = new bool[nver]{0};
int ans = 0;
container.push_heap(edge(0, 0));
while (!container.isempty())
{
edge temp = container.pop_heap();
int to = temp.to;
int wei = temp.wei;
if (visited[to])
{
continue;
}
// otherwise take the current edge
ans += wei;
visited[to] = 1;
for (auto x : adjl[to])
{
if (visited[x.to] == 0)
{
container.push_heap(x);
}
}
}
return ans;
}
int prim_mst_fib_min_heap(){
return 0;
}
int prim_mst_stl_priority_Queue(){
//most important stuff
//Init a Min Heap
priority_queue<pair<int,int>, vector<pair<int,int> > , greater<pair<int,int> > > Q;
//another array
//visited array that denotes whether a node has been included in MST or Not
bool * vis = new bool[nver]{0};
int ans = 0;
//begin
Q.push({0,0}); // weight, node
while(!Q.empty()){
//pick out the edge with min weight
auto best = Q.top();
Q.pop();
int to = best.second;
int weight = best.first;
if(vis[to]){
//discard the edge, and continue
continue;
}
//otherwise take the current edge
ans += weight;
vis[to] = 1;
//add the new edges in the queue
for(auto x:adjl[to]){
if(vis[x.to]==0){
Q.push({x.wei,x.to});
}
}
}
return ans;
}
};
int main()
{
freopen("inp.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int v, e, a, b, c;
cin >> v >> e;
Graph g1 = Graph(v);
for (int i = 0; i < e; i++)
{
cin >> a >> b >> c;
g1.addedge(a, b, c);
}
auto start1 = high_resolution_clock::now();
cout << g1.prim_mst_stl_priority_Queue() << endl;
auto stop1 = high_resolution_clock::now();
auto duration1 = duration_cast<microseconds>(stop1 - start1);
cout<< "STL Priority Queue "<< duration1.count()<<" microseconds"<<endl;
auto start2 = high_resolution_clock::now();
cout << g1.prim_mst_bin_min_heap() << endl;
auto stop2 = high_resolution_clock::now();
auto duration2 = duration_cast<microseconds>(stop2 - start2);
cout<< "Binary Heap "<< duration2.count()<<" microseconds"<<endl;
auto start3 = high_resolution_clock::now();
cout << g1.prim_mst_linkedlist() << endl;
auto stop3 = high_resolution_clock::now();
auto duration3 = duration_cast<microseconds>(stop3 - start3);
cout<< "Linked List "<< duration3.count()<<" microseconds"<<endl;
auto start4 = high_resolution_clock::now();
cout << g1.prim_mst_array() << endl;
auto stop4 = high_resolution_clock::now();
auto duration4 = duration_cast<microseconds>(stop4 - start4);
cout<< "Array "<< duration4.count()<<" microseconds"<<endl;
}