-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathfibonacci_heap.py
548 lines (435 loc) · 14.2 KB
/
fibonacci_heap.py
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
# derived from https://github.com/danielborowski/fibonacci-heap-python with permission
from collections import deque
import math
class Node():
def __init__(self, key):
self.key = key
self.degree = 0
self.mark = False
# pointers
self.parent = None
self.child = None
self.left = None
self.right = None
class FibonacciHeap():
# make-heap
def __init__(self):
self.n = 0 # total nodes in Fibonacci heap
self.min = None
self.root_list = None # circular, doubly linked list
def minimum(self):
return self.min
def insert(self, key):
node = Node(key)
node.left = node
node.right = node
self.merge_with_root_list(node)
if self.min is None or node.key < self.min.key:
self.min = node
self.n += 1
return node
# merge node with doubly linked root list
def merge_with_root_list(self, node):
if self.root_list is None:
self.root_list = node
else:
# insert at end of root list
node.right = self.root_list
node.left = self.root_list.left
self.root_list.left.right = node
self.root_list.left = node
# union of two Fibonacci heaps in O(1)
def union(self, FH2):
FH = FibonacciHeap()
FH.root_list = self.root_list
# set min to lesser of FH1.min and FH2.min
FH.min = self.min if self.min.key < FH2.min.key else FH2.min
# fix pointers to combine root lists
last = FH2.root_list.left
FH2.root_list.left = FH.root_list.left
FH.root_list.left.right = FH2.root_list
FH.root_list.left = last
FH.root_list.left.right = FH.root_list
# update total nodes
FH.n = self.n + FH2.n
return FH
def extract_min(self):
z = self.min
if z is not None:
if z.child is not None:
# attach child nodes to root list
children = [x for x in self.iterate(z.child)]
for i in range(0, len(children)):
self.merge_with_root_list(children[i])
children[i].parent = None
self.remove_from_root_list(z)
# set new min node in heap
if z == z.right:
self.min = None
self.root_list = None
else:
self.min = z.right
self.consolidate()
self.n -= 1
return z
# combine root nodes of equal degree to consolidate the heap
# by creating a list of unordered binomial trees
def consolidate(self):
A = [None] * int(math.log(self.n) * 2)
nodes = [w for w in self.iterate(self.root_list)]
for w in range(0, len(nodes)):
x = nodes[w]
d = x.degree
while A[d] != None:
y = A[d]
if x.key > y.key:
temp = x
x, y = y, temp
self.heap_link(y, x)
A[d] = None
d += 1
A[d] = x
# find new min node - no need to reconstruct new root list below
# because root list was iteratively changing as we were moving
# nodes around in the above loop
for i in range(0, len(A)):
if A[i] is not None:
if A[i].key < self.min.key:
self.min = A[i]
# actual linking of one node to another in the root list
# while also updating the child linked list
def heap_link(self, y, x):
self.remove_from_root_list(y)
y.left = y.right = y
self.merge_with_child_list(x, y)
x.degree += 1
y.parent = x
y.mark = False
# merge a node with the doubly linked child list of a root node
def merge_with_child_list(self, parent, node):
if parent.child is None:
parent.child = node
else:
node.right = parent.child.right
node.left = parent.child
parent.child.right.left = node
parent.child.right = node
# remove a node from the doubly linked root list
def remove_from_root_list(self, node):
if node == self.root_list:
self.root_list = node.right
node.left.right = node.right
node.right.left = node.left
# function to iterate through a doubly linked list
def iterate(self, head):
node = head
stop = head
flag = False
while True:
if node == stop and flag is True:
break
elif node == stop:
flag = True
yield node
node = node.right
# hacky way of printing the tree
def print_fibonacci_heap(self, print_marked=False):
unvisited = deque()
root_list = []
marked_nodes = []
if self.root_list:
for node in self.iterate(self.root_list):
root_list.append(node.key)
unvisited.append(node)
print('--------------------')
print('-- Fibonacci Heap --')
print('--------------------')
print(f'Total nodes: {self.n}')
print(f'Minimum: {self.min.key if self.min else None}')
print(f'Root list node: {self.root_list.key}')
print(f'Root list: {root_list}')
while unvisited:
node = unvisited.popleft()
if node.mark and (node.key not in marked_nodes):
marked_nodes.append(node.key)
if node.child:
children = []
for child in self.iterate(node.child):
children.append(child.key)
if child.child:
unvisited.append(child)
if child.mark and (child.key not in marked_nodes):
marked_nodes.append(child.key)
print(f'Children of {node.key}: {children}')
if print_marked:
print(f'Marked nodes: {marked_nodes}')
print('--------------------\n')
# modify the key of some node in the heap in O(1) time
def decrease_key(self, x, k):
if k > x.key:
return None
x.key = k
y = x.parent
if y is not None and x.key < y.key:
self.cut(x, y)
self.cascading_cut(y)
if x.key < self.min.key:
self.min = x
# if a child node becomes smaller than its parent node we
# cut this child node off and bring it up to the root list
def cut(self, x, y):
self.remove_from_child_list(y, x)
y.degree -= 1
self.merge_with_root_list(x)
x.parent = None
x.mark = False
# cascading cut of parent node to obtain good time bounds
def cascading_cut(self, y):
z = y.parent
if z is not None:
if y.mark is False:
y.mark = True
else:
self.cut(y, z)
self.cascading_cut(z)
# remove a node from the doubly linked child list
def remove_from_child_list(self, parent, node):
if parent.child == parent.child.right:
parent.child = None
elif parent.child == node:
parent.child = node.right
node.right.parent = parent
node.left.right = node.right
node.right.left = node.left
def delete(self, x):
self.decrease_key(x, float('-inf'))
return self.extract_min()
# because I'm constructing the tree from the video manually
def set_node_count(self, n):
self.n = n
# manually craft a larger Fibonacci heap
def make_large_fibonacci_heap():
FH = FibonacciHeap()
five = FH.insert(5)
two = FH.insert(2)
sixteen = FH.insert(16)
nine = FH.insert(9)
twelve = Node(12)
thirty_eight = Node(38)
nineteen = Node(19)
twelve.right = thirty_eight
twelve.left = nineteen
twelve.parent = two
two.child = twelve
two.degree = 3
thirty_eight.left = twelve
thirty_eight.right = nineteen
thirty_eight.parent = two
nineteen.left = thirty_eight
nineteen.right = twelve
nineteen.parent = two
thirty_one = Node(31)
thirty_one.left = thirty_one.right = thirty_one
thirty_one.parent = twelve
twelve.child = thirty_one
twelve.degree = 1
twenty_two = Node(22)
twenty_two.left = twenty_two.right = twenty_two
twenty_two.parent = nineteen
nineteen.child = twenty_two
nineteen.degree = 1
twenty_nine = Node(29)
twenty_nine.left = twenty_nine.right = twenty_nine
twenty_nine.parent = sixteen
sixteen.child = twenty_nine
sixteen.degree = 1
twenty_five = Node(25)
fifty_nine = Node(59)
twenty_five.right = fifty_nine
twenty_five.left = fifty_nine
fifty_nine.right = twenty_five
fifty_nine.left = twenty_five
twenty_five.parent = nine
nine.child = twenty_five
nine.degree = 2
thirty_two = Node(32)
thirty_two.left = thirty_two.right = thirty_two
thirty_two.parent = twenty_five
twenty_five.child = thirty_two
twenty_five.degree = 1
FH.set_node_count(13)
return FH
def make_union_heap_1():
FH = FibonacciHeap()
five = FH.insert(5)
two = FH.insert(2)
twelve = Node(12)
thirty_eight = Node(38)
nineteen = Node(19)
twelve.right = thirty_eight
twelve.left = nineteen
twelve.parent = two
two.child = twelve
two.degree = 3
thirty_eight.left = twelve
thirty_eight.right = nineteen
thirty_eight.parent = two
nineteen.left = thirty_eight
nineteen.right = twelve
nineteen.parent = two
thirty_one = Node(31)
thirty_one.left = thirty_one.right = thirty_one
thirty_one.parent = twelve
twelve.child = thirty_one
twelve.degree = 1
twenty_two = Node(22)
twenty_two.left = twenty_two.right = twenty_two
twenty_two.parent = nineteen
nineteen.child = twenty_two
nineteen.degree = 1
FH.set_node_count(7)
return FH
def make_union_heap_2():
FH = FibonacciHeap()
sixteen = FH.insert(16)
nine = FH.insert(9)
twenty_nine = Node(29)
twenty_nine.left = twenty_nine.right = twenty_nine
twenty_nine.parent = sixteen
sixteen.child = twenty_nine
sixteen.degree = 1
twenty_five = Node(25)
fifty_nine = Node(59)
twenty_five.right = fifty_nine
twenty_five.left = fifty_nine
fifty_nine.right = twenty_five
fifty_nine.left = twenty_five
twenty_five.parent = nine
nine.child = twenty_five
nine.degree = 2
thirty_two = Node(32)
thirty_two.left = thirty_two.right = thirty_two
thirty_two.parent = twenty_five
twenty_five.child = thirty_two
twenty_five.degree = 1
FH.set_node_count(6)
return FH
# make heap for decrease key example
def make_decrease_key_heap():
FH = FibonacciHeap()
seven = FH.insert(7)
eighteen = FH.insert(18)
eighteen.mark = True
thirty_eight = FH.insert(38)
twenty_four = Node(24)
seventeen = Node(17)
twenty_three = Node(23)
twenty_four.right = seventeen
twenty_four.left = twenty_three
twenty_four.parent = seven
seventeen.right = twenty_three
seventeen.left = twenty_four
seventeen.parent = seven
twenty_three.right = twenty_four
twenty_three.left = seventeen
twenty_three.parent = seven
seven.child = twenty_four
seven.degree = 3
twenty_one = Node(21)
thirty_nine = Node(39)
thirty_nine.mark = True
twenty_one.right = thirty_nine
twenty_one.left = thirty_nine
twenty_one.parent = eighteen
thirty_nine.right = twenty_one
thirty_nine.left = twenty_one
twenty_one.parent = eighteen
eighteen.child = twenty_one
seven.degree = 2
fourty_one = Node(41)
fourty_one.left = fourty_one.right = fourty_one
fourty_one.parent = thirty_eight
thirty_eight.child = fourty_one
thirty_eight.degree = 1
twenty_six = Node(26)
twenty_six.mark = True
fourty_six = Node(46)
twenty_six.left = twenty_six.right = fourty_six
twenty_six.parent = twenty_four
fourty_six.left = fourty_six.right = twenty_six
fourty_six.parent = twenty_four
twenty_four.child = twenty_six
twenty_four.degree = 2
thirty = Node(30)
thirty.left = thirty.right = thirty
thirty.parent = seventeen
seventeen.child = thirty
seventeen.degree = 1
fifty_two = Node(52)
fifty_two.left = fifty_two.right = fifty_two
fifty_two.parent = twenty_one
twenty_one.child = fifty_two
twenty_one.degree = 1
thirty_five = Node(35)
thirty_five.left = thirty_five.right = thirty_five
thirty_five.parent = twenty_six
twenty_six.child = thirty_five
twenty_six.degree = 1
FH.set_node_count(14)
return FH
def insert_example_1():
FH = FibonacciHeap()
FH.insert(5)
FH.insert(2)
FH.insert(16)
FH.insert(9)
FH.print_fibonacci_heap()
def insert_example_2():
FH = make_large_fibonacci_heap()
FH.insert(20)
FH.print_fibonacci_heap()
def union_example():
print('FH1 before union:')
FH1 = make_union_heap_1()
FH1.print_fibonacci_heap()
print('FH2 before union:')
FH2 = make_union_heap_2()
FH2.print_fibonacci_heap()
union = FH1.union(FH2)
print('Union of FH1 and FH2:')
union.print_fibonacci_heap()
def extract_min_example():
FH = make_large_fibonacci_heap()
print('Before extract min:')
FH.print_fibonacci_heap()
FH.extract_min()
print('After extract min:')
FH.print_fibonacci_heap()
def decrease_key_example():
FH = make_decrease_key_heap()
print('Before decrease key:')
FH.print_fibonacci_heap(True)
x = FH.root_list.child.child.right
FH.decrease_key(x, 15)
print('After decrease key (46 to 15):')
FH.print_fibonacci_heap(True)
x = FH.root_list.child.child.child.left
FH.decrease_key(x, 5)
print('After decrease key (35 to 5):')
FH.print_fibonacci_heap(True)
def delete_example():
FH = make_large_fibonacci_heap()
print('Before delete:')
FH.print_fibonacci_heap()
x = FH.min.child.right.right
print(f'After delete of {x.key}:')
FH.delete(x)
FH.print_fibonacci_heap()
def main():
insert_example_1()
insert_example_2()
union_example()
extract_min_example()
decrease_key_example()
delete_example()
main()