-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
1650 lines (1280 loc) · 41.3 KB
/
main.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Timeline:
Oct 12 @ 20:00 - adjacency list representation of graph is working
Oct 13 @ 01:45 - Dijkstra's using binary heaps is working for normal cases. test for the unusual ones
Oct 14 @ 03:56 - binomial heap is mostly implemented. huge problem of updating 'parent' of each node persists [cuz a node doesn't point to all kids]
parents are important in decrease-key operation where value percolates upwards
Oct 15 @ 01:56 - Dijkstra's using binomial heaps is working for normal cases. test for the unusual ones
Oct 15 @ 04:19 - Fibonacci heaps partially implemented
Oct 15 @ 20:12 - Dijkstra's using fibonacci heaps is working for normal cases. test for the unusual ones
Remaining work:
-> read graph input from file DONE
-> generate graph input using random numbers NOT NEEDED
-> display running time of each heap DONE
-> display number of operations in each heap
-> add checks for wrong input =/= error handling - stuff like wrong format, negative weights etc DONE
-> test the program for few more graphs
*/
/*
Notes:
-> Each vertex is labeled as a number - from 0 to V-1 where V is the total number of vertices
-> The graph is stored in adjacency list format
-> Need for heaps: to store the vertices not yet included in Shortest-Path-Tree (or, the vertices for which shortest distance has not been finalized yet).
-> Search operation in both binomial and fibonacci heaps is expensive
-> Link for online simulation for Dijkstra's algorithm on any input graph: http://graphonline.ru/en/
*/
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<fstream>
#include<chrono>
using namespace std;
class adjlistnode{
public:
int dest;
int weight;
class adjlistnode*next;
adjlistnode(int d, int w)
{
dest = d;
weight = w;
next = NULL;
}
adjlistnode()
{
next = NULL;
dest = weight = 0;
}
void modify(int d, int w)
{
dest = d;
weight = w;
}
}*temp; //this pointer will be used repeatedly for inserting nodes into the adjacency list
class graph{
public:
int v;
adjlistnode *adjlist;
graph(int numvertices)
{
v = numvertices;
adjlist = new adjlistnode[v]; //access each node now as adjlist[num]
creategraph();
}
void creategraph()
{
int i;
for(i=0;i<v;i++)
adjlist[i].modify(i, 0);
}
void addedge(int s, int d, int w) //source dest, weight
{
temp = new adjlistnode(d, w);
adjlistnode *travel = &adjlist[s];
while(travel->next!=NULL)
travel = travel->next;
travel->next = temp;
temp=NULL; //if you do 'delete temp', that will eradicate/release/free-up the newly create node
}
void printgraph()
{
adjlistnode *travel = NULL;
int i;
cout<<endl<<endl;
int who; //the source of the edge
for(i=0;i<v;i++)
{
who = adjlist[i].dest;
travel = adjlist[i].next;
while(travel!=NULL)
{
cout<<who<<'\t'<<travel->dest<<'\t'<<travel->weight<<endl;
travel=travel->next;
}
}
}
}*g;
//the answers are stored in the following
int *parent; //parent-array
int *dist; //shortest path's cost
int ninsert;
int nextract;
int ndecrease;
int nunion;
int nswap;
int ntrav;
void reset()
{
ninsert = 0;
nextract = 0;
ndecrease = 0;
nunion = 0;
nswap = 0;
ntrav = 0;
}
class binaryheapnode{
public:
int v;
int dist;
binaryheapnode()
{
v = 0;
dist = INT_MAX;
}
};
class binaryheap{
public:
int total; //total number of nodes in heap
int size;
int *pos;
class binaryheapnode *minheap;
binaryheap(int num) //num is the max size of heap
{
total = num;
size=0;
minheap = new binaryheapnode[num];
pos = new int[num]; //holds pos[i] gives index number of vertex i in minheap-array
createminheap();
}
void createminheap()
{
int i;
for(i=0; i<total; i++)
{
minheap[i].dist = INT_MAX;
minheap[i].v = i;
pos[i] = i;
}
}
int getmin()
{
return minheap[0].dist;
}
//heapify ripples values downwards
void heapify(int index) //if parentvalue > any childvalue
{
int left = 2*index + 1;
int right = 2*index + 2;
int smallest = index;
int p, c; //parent and child vertex
if(left<size && minheap[left].dist < minheap[smallest].dist)
smallest = left;
if(right<size && minheap[right].dist < minheap[smallest].dist)
smallest = right;
if(smallest!=index)
{
p = minheap[index].v;
if(smallest == left)
{
c = minheap[left].v;
//swap!
nswap++;
binaryheapnode swapper;
swapper = minheap[left];
minheap[left] = minheap[index];
minheap[index] = swapper;
pos[c] = index;
pos[p] = left;
}
else
{
c = minheap[right].v;
//swap!
nswap++;
binaryheapnode swapper;
swapper = minheap[right];
minheap[right] = minheap[index];
minheap[index] = swapper;
pos[c] = index;
pos[p] = right;
}
heapify(smallest);
}
}
void decreasekey(int x, int val)
{
int index = pos[x];
minheap[index].dist = val;
//ripple value upwards
int parentindex = (index-1)/2;
int p, c; //vertex labels
while(index && minheap[index].dist < minheap[parentindex].dist)
{
p = minheap[parentindex].v;
c = minheap[index].v;
//swap!
nswap++;
binaryheapnode swapper;
swapper = minheap[parentindex];
minheap[parentindex] = minheap[index];
minheap[index] = swapper;
pos[p] = index;
pos[c] = parentindex;
index = (index-1)/2;
parentindex = (index-1)/2;
}
}
binaryheapnode* extractmin()
{
//cout<<size<<endl;
if(size==0)
return NULL;
binaryheapnode* root = new binaryheapnode();
root->v = minheap[0].v;
root->dist = minheap[0].dist;
//replace root with last element in heap
minheap[0] = minheap[size-1];
pos[root->v] = INT_MAX;
pos[minheap[0].v]=0;
size--;
heapify(0);
return root;
}
bool search(int x)
{
if(pos[x]>=size)
return false;
return true;
}
//decreasekey - maintain pos[] values as you shift values up/down the tree
//extractmin - maintain pos[] values as you shift values up/down the tree
//search -> check if pos[] is greater than current number of nodes in heap
};
class binomialheapnode{
public:
int v; //vertex label
int dist;
int degree; //number of kids it has
binomialheapnode *parent;
binomialheapnode *child;
binomialheapnode *sibling;
binomialheapnode(int label, int d)
{
v = label;
dist = d;
parent = NULL;
child = NULL;
sibling = NULL;
degree = 0;
}
binomialheapnode()
{
parent = NULL;
child = NULL;
sibling = NULL;
degree = 0;
v = -1;
dist = INT_MAX;
}
}*btemp; //to create nodes for insertion
class binomialheap{
public:
binomialheapnode *head;
binomialheap()
{
head = NULL;
}
//make tree rooted at y a subtree of tree rooted at z
void link(binomialheapnode *y, binomialheapnode* z)
{
y->parent = z;
y->sibling = z->child;
z->child = y;
z->degree = z->degree + 1;
}
void insert(int v, int dist)
{
btemp = new binomialheapnode(v, dist);
heapunion(btemp);
}
//we're not explicitly doing union on two different heaps
//we do delete-min and then do union of remaining stuff, or, we do union while inserting a new node into existing heap
//so, ultimately, everything is within one heap only
//thus, we update everything in 'head' pointer itself. no need to return the resultant head from every function
//doing union and merge in one pass, like in Alan Weiss book
void heapunion(binomialheapnode *head2)
{
binomialheapnode *t1, *t2, *carry, *prev_carry,*siblingholder;
t1 = head;
t2 = head2;
carry = NULL;
siblingholder = NULL;
prev_carry = NULL;
bool flag = false; //head to result hasn't been set yet
if(t1==NULL && t2==NULL)
{
head = NULL; //redundant
return;
}
else if(t1==NULL)
{
head = t2;
return;
}
else if(t2==NULL)
{
head = t1; //redundant
return;
}
nunion++;
while(t1!=NULL && t2!=NULL)
{
if(t1->degree < t2->degree)
{
if(carry==NULL)
{
carry = t1;
prev_carry = carry; //initialize
ntrav++;
t1 = t1->sibling;
carry->sibling = NULL;
}
else
{
if(carry->degree == t1->degree)
{
if(carry->dist <= t1->dist) //make t1 a child of carry
{
siblingholder = t1->sibling;
link(t1, carry); //changes t1->sibling. hence need of a siblingholder
if(t1==head)
head = carry;
t1 = siblingholder;
}
else
{
//node pointing to carry should point to t1 now
prev_carry->sibling = t1;
link(carry, t1);
if(carry==head)
head = t1;
ntrav++;
carry = t1;
ntrav++;
t1 = t1->sibling;
carry->sibling = NULL;
}
}
else //carry->degree < t1->degree
{
prev_carry = carry;
carry->sibling = t1;
ntrav++;
carry = carry->sibling;
ntrav++;
t1 = t1->sibling;
carry->sibling = NULL;
}
}
}
else if(t2->degree < t1->degree)
{
if(carry==NULL)
{
carry = t2;
prev_carry = carry; //initialize
ntrav++;
t2 = t2->sibling;
carry->sibling = NULL;
}
else
{
if(carry->degree == t2->degree)
{
if(carry->dist <= t2->dist) //make t2 a child of carry
{
siblingholder = t2->sibling;
link(t2, carry); //changes t2->sibling
if(t2==head)
head = carry;
t2 = siblingholder;
}
else
{
//node pointing to carry should point to t2 now
prev_carry->sibling = t2;
link(carry, t2);
if(carry==head)
head = t2;
ntrav++;
carry = t2;
ntrav++;
t2 = t2->sibling;
carry->sibling = NULL;
}
}
else //carry->degree < t2->degree
{
prev_carry = carry;
carry->sibling = t2;
ntrav++;
carry = carry->sibling;
ntrav++;
t2 = t2->sibling;
carry->sibling = NULL;
}
}
}
else //t1->degree = t2->degree
{
//can optimise here by making checks on degree of tree. then make smaller tree part of the larger one
if(carry==NULL)
{
if(t1->dist <= t2->dist)
{
siblingholder = t2->sibling;
link(t2,t1);
if(t2==head)
head = t1;
ntrav++;
carry = t1;
prev_carry = carry; //initialize
ntrav++;
t1 = t1->sibling;
t2 = siblingholder;
carry->sibling = NULL;
}
else
{
siblingholder = t1->sibling;
link(t1,t2);
if(t1==head)
head = t2;
ntrav++;
carry = t2;
prev_carry = carry; //initialize
ntrav++;
t2 = t2->sibling;
t1 = siblingholder;
carry->sibling = NULL;
}
}
else //merge t1&t2. doesn't matter what carry->degree is
{
if(t1->dist <= t2->dist)
{
siblingholder = t2->sibling;
link(t2,t1);
if(t2==head)
head = t1;
carry->sibling = t1;
prev_carry = carry;
ntrav++;
carry = carry->sibling;
ntrav++;
t1 = t1->sibling;
t2 = siblingholder;
carry->sibling = NULL;
}
else
{
siblingholder = t1->sibling;
link(t1,t2);
if(t1==head)
head = t2;
carry->sibling = t2;
prev_carry = carry;
ntrav++;
carry = carry->sibling;
ntrav++;
t2 = t2->sibling;
t1 = siblingholder;
carry->sibling = NULL;
}
}
}
if(!flag)
{
head = carry;
if(carry!=NULL)
flag = true;
}
}
while(t2!=NULL) //merging carry-heap and t2-heap
{
if(carry->degree < t2->degree)
{
prev_carry = carry;
carry->sibling = t2;
ntrav++;
t2 = t2->sibling;
ntrav++;
carry = carry->sibling;
carry->sibling = NULL;
}
else //their degrees are equal
{
if(carry->dist <= t2->dist) //make t2 a child of carry
{
siblingholder = t2->sibling;
link(t2, carry); //changes t2->sibling. hence need of a siblingholder
if(t2==head)
head = carry;
ntrav++;
t2 = siblingholder;
}
else
{
//node pointing to carry should point to t2 now
prev_carry->sibling = t2;
link(carry, t2);
if(carry==head)
head = t2;
carry = t2;
ntrav++;
t2 = t2->sibling;
carry->sibling = NULL;
}
}
}
while(t1!=NULL) //merging carry-heap and t1-heap
{
if(carry->degree < t1->degree)
{
prev_carry = carry;
carry->sibling = t1;
ntrav++;
t1 = t1->sibling;
ntrav++;
carry = carry->sibling;
carry->sibling = NULL;
}
else //their degrees are equal
{
if(carry->dist <= t1->dist) //make t1 a child of carry
{
siblingholder = t1->sibling;
link(t1, carry); //changes t1->sibling. hence need of a siblingholder
if(t1==head)
head = carry;
t1 = siblingholder;
}
else
{
//node pointing to carry should point to t1 now
prev_carry->sibling = t1;
link(carry, t1);
if(carry==head)
head = t1;
carry = t1;
ntrav++;
t1 = t1->sibling;
carry->sibling = NULL;
}
}
}
}
//needed to reverse the kids into a heap in extract-min function
binomialheapnode* reverselist(binomialheapnode *x)
{
binomialheapnode *result = NULL;
binomialheapnode *prev, *current, *next;
prev = NULL;
current = x;
next = NULL;
while(current!=NULL)
{
//creating a new heap, so all these nodes have no parent now
current->parent = NULL;
ntrav++;
next = current->sibling;
current->sibling = prev;
ntrav++;
prev = current;
ntrav++;
current = next;
}
result = prev;
return result;
}
binomialheapnode* extractmin()
{
if(head==NULL)
return NULL;
binomialheapnode *travel = head;
//first scan the list to find minimum
int small = head->dist;
binomialheapnode *smallnode = head;
binomialheapnode *prev = NULL;
while(travel->sibling!=NULL)
{
if(travel->sibling->dist < small)
{
small = travel->sibling->dist;
smallnode = travel->sibling;
prev = travel;
}
ntrav++;
travel = travel->sibling;
}
if(prev==NULL && smallnode->sibling==NULL) //=> only one tree in heap
{
head = NULL; //the kids of smallnode will be the new heap
}
else
{
if(prev == NULL)
head = smallnode->sibling;
else
prev->sibling = smallnode->sibling;
}
if(smallnode->child!=NULL)
{
binomialheapnode *kidsheap = reverselist(smallnode->child);
//in this new heap, write code so that you assign all their parents to NULL
heapunion(kidsheap);
}
smallnode->sibling = NULL;
return smallnode;
}
binomialheapnode* heapsearch(int label) //vertex label
{
return heapsearch(head, label);
}
binomialheapnode* heapsearch(binomialheapnode *h, int label)
{
binomialheapnode *x, *p;
x = h;
p = NULL;
if(x->v == label)
{
p = x;
return p;
}
if (x->child != NULL && p == NULL)
{
ntrav++;
p = heapsearch(x->child, label);
}
if (x->sibling != NULL && p == NULL)
{
ntrav++;
p = heapsearch(x->sibling, label);
}
return p;
}
//decreasekey
void decreasekey(int label, int newval)
{
binomialheapnode *who = heapsearch(label);
int swapper;
//percolate-up
who->dist = newval;
while(who->parent!=NULL && who->dist < who->parent->dist)
{
nswap++;
swapper = who->v;
who->v = who->parent->v;
who->parent->v = swapper;
swapper = who->dist;
who->dist = who->parent->dist;
who->parent->dist = swapper;
ntrav++;
who = who->parent;
}
}
bool isEmpty()
{
if(head==NULL)
return 1;
return 0;
}
void printheap()
{
binomialheapnode *travel = head;
cout<<"\n\nthe heap:\n";
if(travel==NULL)
{
cout<<"heap empty";
return;
}
while(travel!=NULL)
{
cout<<travel->v<<"("<<travel->dist<<","<<travel->degree<<")"<<" ";
travel = travel->sibling;
}
}
};
class fibonacciheapnode{
public:
int v; //vertex-label
int dist;
int degree;
fibonacciheapnode *parent;
fibonacciheapnode *child;
fibonacciheapnode *leftsibling;
fibonacciheapnode *rightsibling;
bool mark; //whether it has lost a kid already or not
bool track; //when traversing a cirular linked list, we don't know when to stop cuz everything points to each other and nothing points to NULL. this 'track' will keep track of which nodes have been traversed so far
fibonacciheapnode(int label, int d)
{
v = label;
dist = d;
degree = 0;
parent = NULL;
child = NULL;
leftsibling = this; //if I have no siblings, then these pointers point to myself
rightsibling = this;
mark = false;
track = false;
}
}*ftemp; //to create nodes for insertion
class fibonacciheap{
public:
fibonacciheapnode *head; //points to node with minimum value in the heap
int num; //number of nodes in the heap
fibonacciheap()
{
head = NULL;
num = 0;
}
void insert(int label, int d) //wrapper function
{
ftemp = new fibonacciheapnode(label, d); //ftemp is a global pointer
num++;
insert(ftemp);
}
void insert(fibonacciheapnode *x)
{
//this function is also called from consolidate. thus, num++ is omitted here and put in wrapper function instead
if(head==NULL)
{
head = x;
}
else //just add the node to root list
{
x->rightsibling = head->rightsibling;
x->leftsibling = head;
head->rightsibling->leftsibling = x;
head->rightsibling = x;
if(x->dist < head->dist)
head = x;
}
}
void link(fibonacciheapnode *y, fibonacciheapnode *z) //remove whichever circular list node y is a part of, and make it a child of node z
{
//removing y from the circular list it is part of
y->leftsibling->rightsibling = y->rightsibling;
y->rightsibling->leftsibling = y->leftsibling;
//y->parent->mark = true
y->parent = z;
y->mark = false; //y is made a kid to someone else, so its mark is cleared
if(z->child == NULL)
{
z->child = y;
y->leftsibling = y;
y->rightsibling = y;
}
else
{
y->rightsibling = z->child->rightsibling;
y->leftsibling = z->child;
if(y->rightsibling!=NULL)
y->rightsibling->leftsibling = y;
z->child->rightsibling = y;
if(y->dist < z->child->dist)
z->child = y;
}
z->degree = z->degree + 1;
}
void heapunion(fibonacciheap *heap2) //merge heap2 into me
{
fibonacciheapnode *head2 = heap2->head;
num += heap2->num;
fibonacciheapnode *temp1;
fibonacciheapnode *temp2;
temp1 = head->rightsibling;
temp2 = head2->leftsibling;
head->rightsibling = temp2;
temp2->rightsibling = head;
head2->leftsibling = temp1;
temp1->leftsibling = head2;