-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci-heap.html
More file actions
2144 lines (1718 loc) · 102 KB
/
fibonacci-heap.html
File metadata and controls
2144 lines (1718 loc) · 102 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fibonacci Heap</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="Python tutorial to learn Python.The Complete Python Programming Course,Learning Python for Data Analysis and Visualization,Python for Beginners." name="description"/>
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="learn about data types, variables, lists, tuples, dictionaries, decision-making statements, and loops,user-defined functions, object-oriented programming, threading, and Python scripting." property="og:description"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
<!--<![endif]-->
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--first part end-------------------------------------------->
<iframe width="560" height="315" src="https://www.youtube.com/embed/d3EhYirYYcc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="editor-contents">
<h1>Fibonacci Heap</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn what a Fibonacci Heap is. Also, you will find working examples of different operations on a fibonacci heap in C, C++, Java and Python.</p>
<div id="node-1665" class="node node-algorithm clearfix" about="/dsa/fibonacci-heap" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Fibonacci Heap" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="definition">A fibonacci heap is a data structure that consists of a collection of trees which follow min heap or max heap property. We have already discussed <strong>min heap</strong> and <strong>max heap property</strong> in the <a href="/dsa/heap-data-structure.html">Heap Data Structure</a> article. These two properties are the characteristics of the trees present on a fibonacci heap.</p>
<p>In a fibonacci heap, a node can have more than two children or no children at all. Also, it has more efficient heap operations than that supported by the binomial and binary heaps.</p>
<p>The fibonacci heap is called a <strong>fibonacci</strong> heap because the trees are constructed in a way such that a tree of order <code>n</code> has at least <code>F<sub>n+2</sub></code> nodes in it, where <code>F<sub>n+2</sub></code> is the <code>(n + 2)<sup>th</sup></code> Fibonacci number.</p>
<figure><img alt="Fibonacci Heap" src="https://cdn.programiz.com/sites/tutorial2program/files/fibonacci-heap.png" title="Fibonacci Heap" width="524" height="274"><figcaption>Fibonacci Heap</figcaption></figure><hr><h2 id="properties">Properties of a Fibonacci Heap</h2>
<p>Important properties of a Fibonacci heap are:</p>
<ol><li>It is a set of <strong>min heap-</strong><a href="https://cs.lmu.edu/~ray/notes/orderedtrees/" target="_blank"><strong>ordered</strong></a> trees. (i.e. The parent is always smaller than the children.)</li>
<li>A pointer is maintained at the minimum element node.</li>
<li>It consists of a set of marked nodes. (Decrease key operation)</li>
<li>The trees within a Fibonacci heap are unordered but <a href="https://mathworld.wolfram.com/RootedTree.html" target="_blank">rooted</a>.</li>
</ol><hr><h2 id="memory">Memory Representation of the Nodes in a Fibonacci Heap</h2>
<p>The roots of all the trees are linked together for faster access. The child nodes of a parent node are connected to each other through a circular doubly linked list as shown below.</p>
<p>There are two main advantages of using a circular doubly linked list.</p>
<ol><li>Deleting a node from the tree takes <code>O(1)</code> time.</li>
<li>The concatenation of two such lists takes <code>O(1)</code> time.</li>
</ol><figure><img alt="Fibonacci Heap Structure" src="https://cdn.programiz.com/sites/tutorial2program/files/fibonacci-heap-structure.png" title="Fibonacci Heap Structure" width="661" height="282"><figcaption>Fibonacci Heap Structure</figcaption></figure><hr><h2 id="operations">Operations on a Fibonacci Heap</h2>
<h3>Insertion</h3>
<p>Algorithm</p>
<pre>insert(H, x)
degree[x] = 0
p[x] = NIL
child[x] = NIL
left[x] = x
right[x] = x
mark[x] = FALSE
concatenate the root list containing x with root list H
if min[H] == NIL or key[x] < key[min[H]]
then min[H] = x
n[H] = n[H] + 1</pre>
<p>Inserting a node into an already existing heap follows the steps below.</p>
<ol><li>Create a new node for the element.</li>
<li>Check if the heap is empty.</li>
<li>If the heap is empty, set the new node as a root node and mark it <var>min</var>.</li>
<li>Else, insert the node into the root list and update <var>min</var>.</li>
</ol><figure><img alt="Insertion operation in fibonacci heap" src="https://cdn.programiz.com/sites/tutorial2program/files/insert-f-heap.png" title="Insertion operation in fibonacci heap" width="588" height="591"><figcaption>Insertion Example</figcaption></figure><h3>Find Min</h3>
<p></p>
<div class="clearfix"></div><p>The minimum element is always given by the <var>min</var> pointer.</p>
<h3>Union</h3>
<p>Union of two fibonacci heaps consists of following steps.</p>
<ol><li>Concatenate the roots of both the heaps.</li>
<li>Update <var>min</var> by selecting a minimum key from the new root lists.</li>
</ol><figure><img alt="Union of two heaps" src="https://cdn.programiz.com/sites/tutorial2program/files/union-f-heap.png" title="Union of two heaps" width="548" height="274"><figcaption>Union of two heaps</figcaption></figure><h3>Extract Min</h3>
<p>It is the most important operation on a fibonacci heap. In this operation, the node with minimum value is removed from the heap and the tree is re-adjusted.</p>
<p>The following steps are followed:</p>
<ol><li>Delete the min node.</li>
<li>Set the min-pointer to the next root in the root list.</li>
<li>Create an array of size equal to the maximum degree of the trees in the heap before deletion.</li>
<li>Do the following (steps 5-7) until there are no multiple roots with the same degree.</li>
<li>Map the degree of current root (min-pointer) to the degree in the array.</li>
<li>Map the degree of next root to the degree in array.</li>
<li>If there are more than two mappings for the same degree, then apply union operation to those roots such that the min-heap property is maintained (i.e. the minimum is at the root).</li>
</ol><p>An implementation of the above steps can be understood in the example below.</p>
<ol><li>We will perform an extract-min operation on the heap below.
<figure><img alt="Extract min" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-1.png" title="Extract min operation" width="588" height="274"><figcaption>Fibonacci Heap</figcaption></figure></li>
<li>Delete the min node, add all its child nodes to the root list and set the min-pointer to the next root in the root list.
<figure><img alt="Delete the min node" src="https://www.programiz.com/sites/tutorial2program/files/extract-min-2.png" title="Extract min operation" width="588" height="274"><figcaption>Delete the min node</figcaption></figure></li>
<li>The maximum degree in the tree is 3. Create an array of size 4 and map degree of the next roots with the array.
<figure><img alt="Create an array" src="https://www.programiz.com/sites/tutorial2program/files/extract-min-5.png" title="Extract min operation" width="588" height="308"><figcaption>Create an array</figcaption></figure></li>
<li>Here, 23 and 7 have the same degrees, so unite them.
<figure><img alt="Unite those having the same degrees" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-6.png" title="Extract min operation" width="524" height="308"><figcaption>Unite those having the same degrees</figcaption></figure></li>
<li>Again, 7 and 17 have the same degrees, so unite them as well.
<figure><img alt="Unite those having the same degrees" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-7.png" title="Extract min operation" width="524" height="308"><figcaption>Unite those having the same degrees</figcaption></figure></li>
<li>Again 7 and 24 have the same degree, so unite them.
<figure><img alt="Unite those having the same degrees" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-8.png" title="Extract min operation" width="528" height="372"><figcaption>Unite those having the same degrees</figcaption></figure></li>
<li>Map the next nodes.
<figure><img alt="Map the remaining nodes" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-10.png" title="Extract min operation" width="528" height="384"><figcaption>Map the remaining nodes</figcaption></figure></li>
<li>Again, 52 and 21 have the same degree, so unite them
<figure><img alt="Unite those having the same degrees" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-11.png" title="Extract min operation" width="472" height="384"><figcaption>Unite those having the same degrees</figcaption></figure></li>
<li>Similarly, unite 21 and 18.
<figure><img alt="Unite those having the same degrees" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-12.png" title="Extract min operation" width="472" height="384"><figcaption>Unite those having the same degrees</figcaption></figure></li>
<li>Map the remaining root.
<figure><img alt="Map the remaining nodes" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-13.png" title="Extract min operation" width="472" height="384"><figcaption>Map the remaining nodes</figcaption></figure></li>
<li>The final heap is.
<figure><img alt="Final heap" src="https://cdn.programiz.com/sites/tutorial2program/files/extract-min-13-1.png" title="Extract min operation" width="472" height="338"><figcaption>Final fibonacci heap</figcaption></figure></li>
</ol><h3>Decreasing a Key and Deleting a Node</h3>
<p>These are the most important operations which are discussed in <a href="/dsa/decrease-key-and-delete-node-from-a-fibonacci-heap.html">Decrease Key and Delete Node Operations</a>.</p>
<hr><h2 id="code">Python, Java and C/C++ Examples</h2>
<div class="tabbed-editor">
<div id="py1" onclick="changepy()" class="tabbed-editor__node tabbed-editor__node--active"><a href="#python-code">Python</a></div>
<div id="java1" onclick="changejava()" class="tabbed-editor__node"><a href="#java-code">Java</a></div>
<div id="c1" onclick="changec()" class="tabbed-editor__node"><a href="#c-code">C</a></div>
<div id="cpp1" onclick="changecpp()" class="tabbed-editor__node"><a href="#cpp-code">C++</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Fibonacci Heap in python</span>
<span class="hljs-keyword">import</span> math
<span class="hljs-comment"># Creating fibonacci tree</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FibonacciTree</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, value)</span>:</span>
self.value = value
self.child = []
self.order = <span class="hljs-number">0</span>
<span class="hljs-comment"># Adding tree at the end of the tree</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_at_end</span><span class="hljs-params">(self, t)</span>:</span>
self.child.append(t)
self.order = self.order + <span class="hljs-number">1</span>
<span class="hljs-comment"># Creating Fibonacci heap</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FibonacciHeap</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
self.trees = []
self.least = <span class="hljs-literal">None</span>
self.count = <span class="hljs-number">0</span>
<span class="hljs-comment"># Insert a node</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_node</span><span class="hljs-params">(self, value)</span>:</span>
new_tree = FibonacciTree(value)
self.trees.append(new_tree)
<span class="hljs-keyword">if</span> (self.least <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> value < self.least.value):
self.least = new_tree
self.count = self.count + <span class="hljs-number">1</span>
<span class="hljs-comment"># Get minimum value</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_min</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">if</span> self.least <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>
<span class="hljs-keyword">return</span> self.least.value
<span class="hljs-comment"># Extract the minimum value</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">extract_min</span><span class="hljs-params">(self)</span>:</span>
smallest = self.least
<span class="hljs-keyword">if</span> smallest <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">for</span> child <span class="hljs-keyword">in</span> smallest.child:
self.trees.append(child)
self.trees.remove(smallest)
<span class="hljs-keyword">if</span> self.trees == []:
self.least = <span class="hljs-literal">None</span>
<span class="hljs-keyword">else</span>:
self.least = self.trees[<span class="hljs-number">0</span>]
self.consolidate()
self.count = self.count - <span class="hljs-number">1</span>
<span class="hljs-keyword">return</span> smallest.value
<span class="hljs-comment"># Consolidate the tree</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">consolidate</span><span class="hljs-params">(self)</span>:</span>
aux = (floor_log(self.count) + <span class="hljs-number">1</span>) * [<span class="hljs-literal">None</span>]
<span class="hljs-keyword">while</span> self.trees != []:
x = self.trees[<span class="hljs-number">0</span>]
order = x.order
self.trees.remove(x)
<span class="hljs-keyword">while</span> aux[order] <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
y = aux[order]
<span class="hljs-keyword">if</span> x.value > y.value:
x, y = y, x
x.add_at_end(y)
aux[order] = <span class="hljs-literal">None</span>
order = order + <span class="hljs-number">1</span>
aux[order] = x
self.least = <span class="hljs-literal">None</span>
<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> aux:
<span class="hljs-keyword">if</span> k <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
self.trees.append(k)
<span class="hljs-keyword">if</span> (self.least <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>
<span class="hljs-keyword">or</span> k.value < self.least.value):
self.least = k
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">floor_log</span><span class="hljs-params">(x)</span>:</span>
<span class="hljs-keyword">return</span> math.frexp(x)[<span class="hljs-number">1</span>] - <span class="hljs-number">1</span>
fibonacci_heap = FibonacciHeap()
fibonacci_heap.insert_node(<span class="hljs-number">7</span>)
fibonacci_heap.insert_node(<span class="hljs-number">3</span>)
fibonacci_heap.insert_node(<span class="hljs-number">17</span>)
fibonacci_heap.insert_node(<span class="hljs-number">24</span>)
<span class="hljs-keyword">print</span>(<span class="hljs-string">'the minimum value of the fibonacci heap: {}'</span>.format(fibonacci_heap.get_min()))
<span class="hljs-keyword">print</span>(<span class="hljs-string">'the minimum value removed: {}'</span>.format(fibonacci_heap.extract_min()))</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Operations on Fibonacci Heap in Java</span>
<span class="hljs-comment">// Node creation</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">node</span> </span>{
node parent;
node left;
node right;
node child;
<span class="hljs-keyword">int</span> degree;
<span class="hljs-keyword">boolean</span> mark;
<span class="hljs-keyword">int</span> key;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">node</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">this</span>.degree = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>.mark = <span class="hljs-keyword">false</span>;
<span class="hljs-keyword">this</span>.parent = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">this</span>.left = <span class="hljs-keyword">this</span>;
<span class="hljs-keyword">this</span>.right = <span class="hljs-keyword">this</span>;
<span class="hljs-keyword">this</span>.child = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">this</span>.key = Integer.MAX_VALUE;
}
node(<span class="hljs-keyword">int</span> x) {
<span class="hljs-keyword">this</span>();
<span class="hljs-keyword">this</span>.key = x;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_parent</span><span class="hljs-params">(node x)</span> </span>{
<span class="hljs-keyword">this</span>.parent = x;
}
<span class="hljs-function">node <span class="hljs-title">get_parent</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.parent;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_left</span><span class="hljs-params">(node x)</span> </span>{
<span class="hljs-keyword">this</span>.left = x;
}
<span class="hljs-function">node <span class="hljs-title">get_left</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.left;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_right</span><span class="hljs-params">(node x)</span> </span>{
<span class="hljs-keyword">this</span>.right = x;
}
<span class="hljs-function">node <span class="hljs-title">get_right</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.right;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_child</span><span class="hljs-params">(node x)</span> </span>{
<span class="hljs-keyword">this</span>.child = x;
}
<span class="hljs-function">node <span class="hljs-title">get_child</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.child;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_degree</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span> </span>{
<span class="hljs-keyword">this</span>.degree = x;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">get_degree</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.degree;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_mark</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> m)</span> </span>{
<span class="hljs-keyword">this</span>.mark = m;
}
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">get_mark</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.mark;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">set_key</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span> </span>{
<span class="hljs-keyword">this</span>.key = x;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">get_key</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.key;
}
}
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">fibHeap</span> </span>{
node min;
<span class="hljs-keyword">int</span> n;
<span class="hljs-keyword">boolean</span> trace;
node found;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">get_trace</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> trace;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">set_trace</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> t)</span> </span>{
<span class="hljs-keyword">this</span>.trace = t;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> fibHeap <span class="hljs-title">create_heap</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> fibHeap();
}
fibHeap() {
min = <span class="hljs-keyword">null</span>;
n = <span class="hljs-number">0</span>;
trace = <span class="hljs-keyword">false</span>;
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insert</span><span class="hljs-params">(node x)</span> </span>{
<span class="hljs-keyword">if</span> (min == <span class="hljs-keyword">null</span>) {
min = x;
x.set_left(min);
x.set_right(min);
} <span class="hljs-keyword">else</span> {
x.set_right(min);
x.set_left(min.get_left());
min.get_left().set_right(x);
min.set_left(x);
<span class="hljs-keyword">if</span> (x.get_key() < min.get_key())
min = x;
}
n += <span class="hljs-number">1</span>;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insert</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
insert(<span class="hljs-keyword">new</span> node(key));
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span> </span>{
display(min);
System.out.println();
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">(node c)</span> </span>{
System.out.print(<span class="hljs-string">"("</span>);
<span class="hljs-keyword">if</span> (c == <span class="hljs-keyword">null</span>) {
System.out.print(<span class="hljs-string">")"</span>);
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
node temp = c;
<span class="hljs-keyword">do</span> {
System.out.print(temp.get_key());
node k = temp.get_child();
display(k);
System.out.print(<span class="hljs-string">"->"</span>);
temp = temp.get_right();
} <span class="hljs-keyword">while</span> (temp != c);
System.out.print(<span class="hljs-string">")"</span>);
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">merge_heap</span><span class="hljs-params">(fibHeap H1, fibHeap H2, fibHeap H3)</span> </span>{
H3.min = H1.min;
<span class="hljs-keyword">if</span> (H1.min != <span class="hljs-keyword">null</span> && H2.min != <span class="hljs-keyword">null</span>) {
node t1 = H1.min.get_left();
node t2 = H2.min.get_left();
H1.min.set_left(t2);
t1.set_right(H2.min);
H2.min.set_left(t1);
t2.set_right(H1.min);
}
<span class="hljs-keyword">if</span> (H1.min == <span class="hljs-keyword">null</span> || (H2.min != <span class="hljs-keyword">null</span> && H2.min.get_key() < H1.min.get_key()))
H3.min = H2.min;
H3.n = H1.n + H2.n;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">find_min</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.min.get_key();
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display_node</span><span class="hljs-params">(node z)</span> </span>{
System.out.println(<span class="hljs-string">"right: "</span> + ((z.get_right() == <span class="hljs-keyword">null</span>) ? <span class="hljs-string">"-1"</span> : z.get_right().get_key()));
System.out.println(<span class="hljs-string">"left: "</span> + ((z.get_left() == <span class="hljs-keyword">null</span>) ? <span class="hljs-string">"-1"</span> : z.get_left().get_key()));
System.out.println(<span class="hljs-string">"child: "</span> + ((z.get_child() == <span class="hljs-keyword">null</span>) ? <span class="hljs-string">"-1"</span> : z.get_child().get_key()));
System.out.println(<span class="hljs-string">"degree "</span> + z.get_degree());
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">extract_min</span><span class="hljs-params">()</span> </span>{
node z = <span class="hljs-keyword">this</span>.min;
<span class="hljs-keyword">if</span> (z != <span class="hljs-keyword">null</span>) {
node c = z.get_child();
node k = c, p;
<span class="hljs-keyword">if</span> (c != <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">do</span> {
p = c.get_right();
insert(c);
c.set_parent(<span class="hljs-keyword">null</span>);
c = p;
} <span class="hljs-keyword">while</span> (c != <span class="hljs-keyword">null</span> && c != k);
}
z.get_left().set_right(z.get_right());
z.get_right().set_left(z.get_left());
z.set_child(<span class="hljs-keyword">null</span>);
<span class="hljs-keyword">if</span> (z == z.get_right())
<span class="hljs-keyword">this</span>.min = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>.min = z.get_right();
<span class="hljs-keyword">this</span>.consolidate();
}
<span class="hljs-keyword">this</span>.n -= <span class="hljs-number">1</span>;
<span class="hljs-keyword">return</span> z.get_key();
}
<span class="hljs-keyword">return</span> Integer.MAX_VALUE;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">consolidate</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">double</span> phi = (<span class="hljs-number">1</span> + Math.sqrt(<span class="hljs-number">5</span>)) / <span class="hljs-number">2</span>;
<span class="hljs-keyword">int</span> Dofn = (<span class="hljs-keyword">int</span>) (Math.log(<span class="hljs-keyword">this</span>.n) / Math.log(phi));
node[] A = <span class="hljs-keyword">new</span> node[Dofn + <span class="hljs-number">1</span>];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <= Dofn; ++i)
A[i] = <span class="hljs-keyword">null</span>;
node w = min;
<span class="hljs-keyword">if</span> (w != <span class="hljs-keyword">null</span>) {
node check = min;
<span class="hljs-keyword">do</span> {
node x = w;
<span class="hljs-keyword">int</span> d = x.get_degree();
<span class="hljs-keyword">while</span> (A[d] != <span class="hljs-keyword">null</span>) {
node y = A[d];
<span class="hljs-keyword">if</span> (x.get_key() > y.get_key()) {
node temp = x;
x = y;
y = temp;
w = x;
}
fib_heap_link(y, x);
check = x;
A[d] = <span class="hljs-keyword">null</span>;
d += <span class="hljs-number">1</span>;
}
A[d] = x;
w = w.get_right();
} <span class="hljs-keyword">while</span> (w != <span class="hljs-keyword">null</span> && w != check);
<span class="hljs-keyword">this</span>.min = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <= Dofn; ++i) {
<span class="hljs-keyword">if</span> (A[i] != <span class="hljs-keyword">null</span>) {
insert(A[i]);
}
}
}
}
<span class="hljs-comment">// Linking operation</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fib_heap_link</span><span class="hljs-params">(node y, node x)</span> </span>{
y.get_left().set_right(y.get_right());
y.get_right().set_left(y.get_left());
node p = x.get_child();
<span class="hljs-keyword">if</span> (p == <span class="hljs-keyword">null</span>) {
y.set_right(y);
y.set_left(y);
} <span class="hljs-keyword">else</span> {
y.set_right(p);
y.set_left(p.get_left());
p.get_left().set_right(y);
p.set_left(y);
}
y.set_parent(x);
x.set_child(y);
x.set_degree(x.get_degree() + <span class="hljs-number">1</span>);
y.set_mark(<span class="hljs-keyword">false</span>);
}
<span class="hljs-comment">// Search operation</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">find</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key, node c)</span> </span>{
<span class="hljs-keyword">if</span> (found != <span class="hljs-keyword">null</span> || c == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-keyword">else</span> {
node temp = c;
<span class="hljs-keyword">do</span> {
<span class="hljs-keyword">if</span> (key == temp.get_key())
found = temp;
<span class="hljs-keyword">else</span> {
node k = temp.get_child();
find(key, k);
temp = temp.get_right();
}
} <span class="hljs-keyword">while</span> (temp != c && found == <span class="hljs-keyword">null</span>);
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> node <span class="hljs-title">find</span><span class="hljs-params">(<span class="hljs-keyword">int</span> k)</span> </span>{
found = <span class="hljs-keyword">null</span>;
find(k, <span class="hljs-keyword">this</span>.min);
<span class="hljs-keyword">return</span> found;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">decrease_key</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key, <span class="hljs-keyword">int</span> nval)</span> </span>{
node x = find(key);
decrease_key(x, nval);
}
<span class="hljs-comment">// Decrease key operation</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">decrease_key</span><span class="hljs-params">(node x, <span class="hljs-keyword">int</span> k)</span> </span>{
<span class="hljs-keyword">if</span> (k > x.get_key())
<span class="hljs-keyword">return</span>;
x.set_key(k);
node y = x.get_parent();
<span class="hljs-keyword">if</span> (y != <span class="hljs-keyword">null</span> && x.get_key() < y.get_key()) {
cut(x, y);
cascading_cut(y);
}
<span class="hljs-keyword">if</span> (x.get_key() < min.get_key())
min = x;
}
<span class="hljs-comment">// Cut operation</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">cut</span><span class="hljs-params">(node x, node y)</span> </span>{
x.get_right().set_left(x.get_left());
x.get_left().set_right(x.get_right());
y.set_degree(y.get_degree() - <span class="hljs-number">1</span>);
x.set_right(<span class="hljs-keyword">null</span>);
x.set_left(<span class="hljs-keyword">null</span>);
insert(x);
x.set_parent(<span class="hljs-keyword">null</span>);
x.set_mark(<span class="hljs-keyword">false</span>);
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">cascading_cut</span><span class="hljs-params">(node y)</span> </span>{
node z = y.get_parent();
<span class="hljs-keyword">if</span> (z != <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">if</span> (y.get_mark() == <span class="hljs-keyword">false</span>)
y.set_mark(<span class="hljs-keyword">true</span>);
<span class="hljs-keyword">else</span> {
cut(y, z);
cascading_cut(z);
}
}
}
<span class="hljs-comment">// Delete operations</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">delete</span><span class="hljs-params">(node x)</span> </span>{
decrease_key(x, Integer.MIN_VALUE);
<span class="hljs-keyword">int</span> p = extract_min();
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
fibHeap obj = create_heap();
obj.insert(<span class="hljs-number">7</span>);
obj.insert(<span class="hljs-number">26</span>);
obj.insert(<span class="hljs-number">30</span>);
obj.insert(<span class="hljs-number">39</span>);
obj.insert(<span class="hljs-number">10</span>);
obj.display();
System.out.println(obj.extract_min());
obj.display();
System.out.println(obj.extract_min());
obj.display();
System.out.println(obj.extract_min());
obj.display();
System.out.println(obj.extract_min());
obj.display();
System.out.println(obj.extract_min());
obj.display();
}
}</code></pre></div>
</div>
<div class="code-editor__area" id="c-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="c hljs cpp"><span class="hljs-comment">// Operations on a Fibonacci heap in C</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><math.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdbool.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
<span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> _<span class="hljs-title">NODE</span> {</span>
<span class="hljs-keyword">int</span> key;
<span class="hljs-keyword">int</span> degree;
<span class="hljs-class"><span class="hljs-keyword">struct</span> _<span class="hljs-title">NODE</span> *<span class="hljs-title">left_sibling</span>;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> _<span class="hljs-title">NODE</span> *<span class="hljs-title">right_sibling</span>;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> _<span class="hljs-title">NODE</span> *<span class="hljs-title">parent</span>;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> _<span class="hljs-title">NODE</span> *<span class="hljs-title">child</span>;</span>
<span class="hljs-keyword">bool</span> mark;
<span class="hljs-keyword">bool</span> visited;
} NODE;
<span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">fibanocci_heap</span> {</span>
<span class="hljs-keyword">int</span> n;
NODE *min;
<span class="hljs-keyword">int</span> phi;
<span class="hljs-keyword">int</span> degree;
} FIB_HEAP;
<span class="hljs-function">FIB_HEAP *<span class="hljs-title">make_fib_heap</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertion</span><span class="hljs-params">(FIB_HEAP *H, NODE *<span class="hljs-keyword">new</span>, <span class="hljs-keyword">int</span> val)</span></span>;
<span class="hljs-function">NODE *<span class="hljs-title">extract_min</span><span class="hljs-params">(FIB_HEAP *H)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">consolidate</span><span class="hljs-params">(FIB_HEAP *H)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fib_heap_link</span><span class="hljs-params">(FIB_HEAP *H, NODE *y, NODE *x)</span></span>;
<span class="hljs-function">NODE *<span class="hljs-title">find_min_node</span><span class="hljs-params">(FIB_HEAP *H)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">decrease_key</span><span class="hljs-params">(FIB_HEAP *H, NODE *node, <span class="hljs-keyword">int</span> key)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">cut</span><span class="hljs-params">(FIB_HEAP *H, NODE *node_to_be_decrease, NODE *parent_node)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">cascading_cut</span><span class="hljs-params">(FIB_HEAP *H, NODE *parent_node)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Delete_Node</span><span class="hljs-params">(FIB_HEAP *H, <span class="hljs-keyword">int</span> dec_key)</span></span>;
<span class="hljs-function">FIB_HEAP *<span class="hljs-title">make_fib_heap</span><span class="hljs-params">()</span> </span>{
FIB_HEAP *H;
H = (FIB_HEAP *)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(FIB_HEAP));
H->n = <span class="hljs-number">0</span>;
H->min = <span class="hljs-literal">NULL</span>;
H->phi = <span class="hljs-number">0</span>;
H->degree = <span class="hljs-number">0</span>;
<span class="hljs-keyword">return</span> H;
}
<span class="hljs-comment">// Printing the heap</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">print_heap</span><span class="hljs-params">(NODE *n)</span> </span>{
NODE *x;
<span class="hljs-keyword">for</span> (x = n;; x = x->right_sibling) {
<span class="hljs-keyword">if</span> (x->child == <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"node with no child (%d) \n"</span>, x->key);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"NODE(%d) with child (%d)\n"</span>, x->key, x->child->key);
print_heap(x->child);
}
<span class="hljs-keyword">if</span> (x->right_sibling == n) {
<span class="hljs-keyword">break</span>;
}
}
}
<span class="hljs-comment">// Inserting nodes</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertion</span><span class="hljs-params">(FIB_HEAP *H, NODE *<span class="hljs-keyword">new</span>, <span class="hljs-keyword">int</span> val)</span> </span>{
<span class="hljs-keyword">new</span> = (NODE *)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(NODE));
<span class="hljs-keyword">new</span>->key = val;
<span class="hljs-keyword">new</span>->degree = <span class="hljs-number">0</span>;
<span class="hljs-keyword">new</span>->mark = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">new</span>->parent = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">new</span>->child = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">new</span>->visited = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">new</span>->left_sibling = <span class="hljs-keyword">new</span>;
<span class="hljs-keyword">new</span>->right_sibling = <span class="hljs-keyword">new</span>;
<span class="hljs-keyword">if</span> (H->min == <span class="hljs-literal">NULL</span>) {
H->min = <span class="hljs-keyword">new</span>;
} <span class="hljs-keyword">else</span> {
H->min->left_sibling->right_sibling = <span class="hljs-keyword">new</span>;
<span class="hljs-keyword">new</span>->right_sibling = H->min;
<span class="hljs-keyword">new</span>->left_sibling = H->min->left_sibling;
H->min->left_sibling = <span class="hljs-keyword">new</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">new</span>->key < H->min->key) {
H->min = <span class="hljs-keyword">new</span>;
}
}
(H->n)++;
}
<span class="hljs-comment">// Find min node</span>
<span class="hljs-function">NODE *<span class="hljs-title">find_min_node</span><span class="hljs-params">(FIB_HEAP *H)</span> </span>{
<span class="hljs-keyword">if</span> (H == <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">" \n Fibonacci heap not yet created \n"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
} <span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> H->min;
}
<span class="hljs-comment">// Union operation</span>
<span class="hljs-function">FIB_HEAP *<span class="hljs-title">unionHeap</span><span class="hljs-params">(FIB_HEAP *H1, FIB_HEAP *H2)</span> </span>{
FIB_HEAP *Hnew;
Hnew = make_fib_heap();
Hnew->min = H1->min;
NODE *temp1, *temp2;
temp1 = Hnew->min->right_sibling;
temp2 = H2->min->left_sibling;
Hnew->min->right_sibling->left_sibling = H2->min->left_sibling;
Hnew->min->right_sibling = H2->min;
H2->min->left_sibling = Hnew->min;
temp2->right_sibling = temp1;
<span class="hljs-keyword">if</span> ((H1->min == <span class="hljs-literal">NULL</span>) || (H2->min != <span class="hljs-literal">NULL</span> && H2->min->key < H1->min->key))
Hnew->min = H2->min;
Hnew->n = H1->n + H2->n;
<span class="hljs-keyword">return</span> Hnew;
}
<span class="hljs-comment">// Calculate the degree</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">cal_degree</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
<span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (n > <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
count++;
}
<span class="hljs-keyword">return</span> count;
}
<span class="hljs-comment">// Consolidate function</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">consolidate</span><span class="hljs-params">(FIB_HEAP *H)</span> </span>{
<span class="hljs-keyword">int</span> degree, i, d;
degree = cal_degree(H->n);
NODE *A[degree], *x, *y, *z;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i <= degree; i++) {
A[i] = <span class="hljs-literal">NULL</span>;
}
x = H->min;
<span class="hljs-keyword">do</span> {
d = x->degree;
<span class="hljs-keyword">while</span> (A[d] != <span class="hljs-literal">NULL</span>) {
y = A[d];
<span class="hljs-keyword">if</span> (x->key > y->key) {
NODE *exchange_help;
exchange_help = x;
x = y;
y = exchange_help;
}
<span class="hljs-keyword">if</span> (y == H->min)
H->min = x;
fib_heap_link(H, y, x);
<span class="hljs-keyword">if</span> (y->right_sibling == x)
H->min = x;
A[d] = <span class="hljs-literal">NULL</span>;
d++;
}
A[d] = x;
x = x->right_sibling;
} <span class="hljs-keyword">while</span> (x != H->min);
H->min = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < degree; i++) {
<span class="hljs-keyword">if</span> (A[i] != <span class="hljs-literal">NULL</span>) {
A[i]->left_sibling = A[i];
A[i]->right_sibling = A[i];
<span class="hljs-keyword">if</span> (H->min == <span class="hljs-literal">NULL</span>) {
H->min = A[i];
} <span class="hljs-keyword">else</span> {
H->min->left_sibling->right_sibling = A[i];
A[i]->right_sibling = H->min;
A[i]->left_sibling = H->min->left_sibling;
H->min->left_sibling = A[i];
<span class="hljs-keyword">if</span> (A[i]->key < H->min->key) {
H->min = A[i];
}
}
<span class="hljs-keyword">if</span> (H->min == <span class="hljs-literal">NULL</span>) {
H->min = A[i];
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (A[i]->key < H->min->key) {
H->min = A[i];
}
}
}
}
<span class="hljs-comment">// Linking</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fib_heap_link</span><span class="hljs-params">(FIB_HEAP *H, NODE *y, NODE *x)</span> </span>{
y->right_sibling->left_sibling = y->left_sibling;
y->left_sibling->right_sibling = y->right_sibling;
<span class="hljs-keyword">if</span> (x->right_sibling == x)
H->min = x;
y->left_sibling = y;
y->right_sibling = y;
y->parent = x;
<span class="hljs-keyword">if</span> (x->child == <span class="hljs-literal">NULL</span>) {
x->child = y;
}
y->right_sibling = x->child;
y->left_sibling = x->child->left_sibling;
x->child->left_sibling->right_sibling = y;
x->child->left_sibling = y;
<span class="hljs-keyword">if</span> ((y->key) < (x->child->key))
x->child = y;
(x->degree)++;
}
<span class="hljs-comment">// Extract min</span>
<span class="hljs-function">NODE *<span class="hljs-title">extract_min</span><span class="hljs-params">(FIB_HEAP *H)</span> </span>{
<span class="hljs-keyword">if</span> (H->min == <span class="hljs-literal">NULL</span>)
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n The heap is empty"</span>);
<span class="hljs-keyword">else</span> {
NODE *temp = H->min;
NODE *pntr;
pntr = temp;
NODE *x = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">if</span> (temp->child != <span class="hljs-literal">NULL</span>) {
x = temp->child;
<span class="hljs-keyword">do</span> {
pntr = x->right_sibling;
(H->min->left_sibling)->right_sibling = x;
x->right_sibling = H->min;
x->left_sibling = H->min->left_sibling;
H->min->left_sibling = x;
<span class="hljs-keyword">if</span> (x->key < H->min->key)
H->min = x;
x->parent = <span class="hljs-literal">NULL</span>;
x = pntr;
} <span class="hljs-keyword">while</span> (pntr != temp->child);
}
(temp->left_sibling)->right_sibling = temp->right_sibling;
(temp->right_sibling)->left_sibling = temp->left_sibling;
H->min = temp->right_sibling;
<span class="hljs-keyword">if</span> (temp == temp->right_sibling && temp->child == <span class="hljs-literal">NULL</span>)
H->min = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">else</span> {
H->min = temp->right_sibling;
consolidate(H);
}
H->n = H->n - <span class="hljs-number">1</span>;
<span class="hljs-keyword">return</span> temp;
}
<span class="hljs-keyword">return</span> H->min;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">cut</span><span class="hljs-params">(FIB_HEAP *H, NODE *node_to_be_decrease, NODE *parent_node)</span> </span>{
NODE *temp_parent_check;
<span class="hljs-keyword">if</span> (node_to_be_decrease == node_to_be_decrease->right_sibling)
parent_node->child = <span class="hljs-literal">NULL</span>;
node_to_be_decrease->left_sibling->right_sibling = node_to_be_decrease->right_sibling;
node_to_be_decrease->right_sibling->left_sibling = node_to_be_decrease->left_sibling;
<span class="hljs-keyword">if</span> (node_to_be_decrease == parent_node->child)
parent_node->child = node_to_be_decrease->right_sibling;
(parent_node->degree)--;
node_to_be_decrease->left_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = node_to_be_decrease;
H->min->left_sibling->right_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = H->min;
node_to_be_decrease->left_sibling = H->min->left_sibling;
H->min->left_sibling = node_to_be_decrease;
node_to_be_decrease->parent = <span class="hljs-literal">NULL</span>;
node_to_be_decrease->mark = <span class="hljs-literal">false</span>;
}