-
Notifications
You must be signed in to change notification settings - Fork 1
/
fibheap.py
375 lines (345 loc) · 12.2 KB
/
fibheap.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
class FhObject(object):
'''
This is a node object for the Fibonacci Heap class
Data entered here must be comparable with < or >
'''
def __init__(self):
self.parent = None
self.child = None
self.data = None
self.mark = 0
self.lsib = None
self.rsib = None
def add_child(self, new):
'''
Adds a child node to a node. Maintains min-heap property if child key is greater than parent key
Very useful for auto-updating the Fibonacci heap for min-heap during the consolidate operation
'''
if new.data < self.data:
self.__balance(self, new)
if self.child == None:
self.child = new
new.parent = self
else:
x = self.child
while x.rsib != None:
x = x.rsib
x.rsib = new
new.lsib = x
new.parent = self
if new.data < self.child.data:
self.child = new
def detach(self):
'''
Special delete method for Fibonacci heap implementation - detaches whole branch below the node
Does not actually delete the node or it's children - that is done by the Fibonacci Heap class
'''
if(self.parent != None):
#if this node was the min-child of parent, fix parent's min-child
if(self.parent.child == self):
minx = None
if(self.lsib != None):
x = self.lsib
if (minx == None):
minx = x
else:
if(x.data < minx.data):
minx = x
if(self.rsib != None):
x = self.rsib
if (minx == None):
minx = x
else:
if(x.data < minx.data):
minx = x
self.parent.child = minx
self.parent = None
#reset the siblings' connections before exiting the tree
if(self.lsib != None):
if(self.rsib != None):
self.lsib.rsib = self.rsib
self.rsib.lsib = self.lsib
self.rsib = None
else:
self.lsib.rsib = None
self.lsib = None
if(self.rsib != None):
self.rsib.lsib = None
self.rsib = None
def find_rank(self):
''' Finds the rank of a given node in the heap based on it's position '''
x = self
rank = 0
while x.child != None:
if x.child != None:
rank += 1
x = x.child
temp = x
if x.lsib != None:
while x.lsib != None:
x = x.lsib
if x.child != None:
break
if x.child != None:
continue
x = temp
if x.rsib != None:
while x.rsib != None:
x = x.rsib
if x.child != None:
break
if x.child != None:
continue
return rank
def __balance(self, parent, child):
''' Private method - responsible for maintaining balance of the heap '''
if child.data < parent.data:
temp = child.data
child.data = parent.data
parent.data = temp
if(parent.parent != None):
if parent.data < parent.parent.child.data:
parent.parent.child = parent
self.__balance(parent.parent, parent)
if(child.child != None):
if child.data > child.child.data:
self.__balance(child, child.child)
else:
x = child.child
while x.rsib != None:
x = x.rsib
if x.data < child.data:
child.child = x
self.__balance(child,x)
temp = x
while x.lsib != None:
x = x.lsib
if x.data < child.data:
child.child = x
self.__balance(child,x)
class FibonacciHeap(object):
'''
This is the class for a Fibonacci Heap
'''
def __init__(self):
self.roots = [] #this list will only hold roots
self.nodes = [] #this list will hold all nodes in the F-heap, useful for keeping find operation simple
self.minroot = None
self. icount = 0
def find_min(self):
'''
Finds the root with minimum value
Used in other functions to keep the pointer to the smallest value up to date
'''
index = 0
for i in range (0, len(self.roots)):
if self.minroot == None:
self.minroot = self.roots[i]
index = i
else:
if self.roots[i].data < self.minroot.data:
self.minroot = self.roots[i]
index = i
return index
def insert(self, item):
'''
Inserts new data into the sequence
Updates the minimum root automatically
Inserted data must be comparable with < or >
Tuples of the form (priority, key) accepted
'''
#algorithm is 'lazy' - we just insert the new item for now, we do not
#bother updating the heap for balance like we do in classic min-heap
new = FhObject()
new.data = item
self.roots.append(new)
self.icount += 1
self.nodes.append(new)
if self.minroot == None:
self.minroot = new
else:
if new.data < self.minroot.data:
self.minroot = new
def union(self, obj):
'''
Concatenates root lists of two Fibonacci Heaps
Updates minimum root automatically
'''
self.roots += obj.roots
self.nodes += obj.nodes
self.icount += 1
self.find_min()
def decrease_key(self, item, value):
'''
Decreases the value of a given node in the Fibonacci Heap
If multiple keys with the same value exists, it will perform this operation on the first node with the given value
Operation is return error message if key is not found
'''
#again, we take the 'lazy' approach - just re-attach the decreased nodes to root list
#we keep the order of the heap in check by marking nodes who've had more than 1 child
#detached due to this function, and we recursively detach marked parents
x = self.__find(item)
if x == None:
print "Error: Could not find a node with the given value"
return ''
x.data = value
if x not in self.roots:
if x.parent.mark == 0:
x.parent.mark = 1
x.detach()
self.roots.append(x)
else:
y = x.parent
x.detach()
self.roots.append(x)
while y.mark != 0:
z = y.parent
if z == None:
break
y.mark = 0
y.detach()
self.roots.append(y)
y = z
self.icount += 1
self.find_min()
def fh_pop(self):
'''
Pops the minimum element from the Fibonacci Heap
Functions similar to pop in a classic Priority Queue
'''
#first extarct the min-root and append all it's children to the root list
if self.is_empty():
print "Underflow",
return ''
else:
x = self.minroot
y = x.data
self.roots.remove(self.minroot)
self.nodes.remove(self.minroot)
self.icount += 1
if x.child:
x = x.child
self.roots.append(x)
temp = x
while x.lsib != None:
x = x.lsib
self.roots.append(x)
self.icount += 1
x = temp
while x.rsib != None:
x = x.rsib
self.roots.append(x)
self.icount += 1
for i in range(0,len(self.roots)):
if self.roots[i].parent != None:
self.roots[i].detach()
#consolidate - perform all the steps which the 'lazy' algorithm has been postponing so far
#cur - current node in the loop, checks nodes for same rank, if there is a match, attaches itself
#to it, and starts over, searching for root nodes matching it's new rank, once this is exhausted,
#it moves on to the next node
#prev - basically, to check for the algorithm's termination, we check if the root list has nodes with all
#unique ranks : this is the only condition which will not reset this variable and cause the loop to break
#We cannot directly loop this using the bound len(roots) because that bound changes during the loop
if self.is_empty():
self.minroot = None
return y
cur = self.roots[0]
i = self.roots.index(cur)
prev = 0
while True:
if prev == len(self.roots):
break
i = (i+1)%(len(self.roots))
if i == self.roots.index(cur):
cur = self.roots[(i+1)%(len(self.roots))]
i = self.roots.index(cur)
prev += 1
continue
if cur.find_rank() == self.roots[i].find_rank():
cur.add_child(self.roots[i])
self.icount += 1
self.roots.remove(self.roots[i])
i = self.roots.index(cur)
prev = 0
continue
self.minroot = None
self.find_min()
return y
def is_empty(self):
'''
Returns True if the Fibonacci Heap is empty
'''
if len(self.roots) == 0:
return True
else:
return False
def __find(self, item):
'''
Private method - find an item in Fibonacci heap which matches a given key
Used only by decrease key method
'''
for i in range (0, len(self.nodes)):
if self.nodes[i].data == item:
return self.nodes[i]
return None
import sys, unittest
class fhtest(unittest.TestCase):
'''
Performs a coprehensive check of the Fibonacci Heap Class
Checks if all functions are working correctly
'''
def test_fullcheck(self):
'''
Function for the coprehensive test
'''
#check initialization
fh = FibonacciHeap()
self.assertEqual(fh.is_empty(), True)
#check insertion and single deletion
x = len(fh.roots)
for i in range(1,101):
fh.insert(i)
self.assertEqual(len(fh.roots),x+100)
self.assertEqual(fh.minroot.data, 1)
temp = fh.fh_pop()
self.assertEqual(temp, 1)
self.assertEqual(fh.minroot.data, 2)
#check decrease key for change in min-root and invalid search
fh.decrease_key(67,-1)
fh.decrease_key(69,1)
self.assertEqual(fh.decrease_key(69,0),'')
self.assertEqual(fh.minroot.data, -1)
#check union
fh2 = FibonacciHeap()
fh2.insert(101)
fh.union(fh2)
#check pop
while not fh.is_empty():
temp = fh.fh_pop()
self.assertEqual(temp, 101)
self.assertEqual(fh.is_empty(), True)
suite = unittest.TestLoader().loadTestsFromTestCase(fhtest)
unittest.TextTestRunner(verbosity=1).run(suite)
# a demo test i made for class
from random import randint
a = FibonacciHeap()
#help(a)
a.insert(6)
a.insert(5)
a.insert(1)
a.insert(9)
a.insert(3)
print a.fh_pop()
print a.minroot.find_rank()
a.decrease_key(9,2)
a.decrease_key(5,4)
print a.fh_pop()
print a.fh_pop()
print a.fh_pop()
print ''
b = FibonacciHeap()
for i in range(1,101):
b.insert(randint(1,100))
while not b.is_empty():
print b.fh_pop(),
print ''