-
Notifications
You must be signed in to change notification settings - Fork 2
/
FibonacciHeap.py
192 lines (155 loc) · 5.46 KB
/
FibonacciHeap.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
class FibHeap:
#### Node Class ####
class Node:
def __init__(self, key, value):
# key value degree mark / prev next child parent
self.key = key
self.value = value
self.degree = 0
self.mark = False
self.parent = self.child = None
self.previous = self.next = self
def issingle(self):
return self == self.next
def insert(self, node):
if node == None:
return
self.next.previous = node.previous
node.previous.next = self.next
self.next = node
node.previous = self
def remove(self):
self.previous.next = self.next
self.next.previous = self.previous
self.next = self.previous = self
def addchild(self, node):
if self.child == None:
self.child = node
else:
self.child.insert(node)
node.parent = self
node.mark = False
self.degree += 1
def removechild(self, node):
if node.parent != self:
raise AssertionError("Cannot remove child from a node that is not its parent")
if node.issingle():
if self.child != node:
raise AssertionError("Cannot remove a node that is not a child")
self.child = None
else:
if self.child == node:
self.child = node.next
node.remove()
node.parent = None
node.mark = False
self.degree -= 1
#### End of Node Class ####
def __init__ (self):
self.minnode = None
self.count = 0
self.maxdegree = 0
def isempty(self):
return self.count == 0
def __len__(self):
return self.count
def insert(self, node):
self.count += 1
self._insertnode(node)
# return node
def _insertnode(self, node):
if self.minnode == None:
self.minnode = node
else:
self.minnode.insert(node)
if node.key < self.minnode.key:
self.minnode = node
# return node
def minimum(self):
if self.minnode == None:
raise AssertionError("Cannot return minimum of empty heap")
return self.minnode
def merge(self, heap):
self.minnode.insert(heap.minnode)
if self.minnode == None or (heap.minnode != None and heap.minnode.key < self.minnode.key):
self.minnode = heap.minnode
self.count += heap.count
def removeminimum(self):
if self.minnode == None:
raise AssertionError("Cannot remove from an empty heap")
removed_node = self.minnode
self.count -= 1
# 1: Assign all old root children as new roots
if self.minnode.child != None:
c = self.minnode.child
while True:
c.parent = None
c = c.next
if c == self.minnode.child:
break
self.minnode.child = None
self.minnode.insert(c)
# 2.1: If we have removed the last key
if self.minnode.next == self.minnode:
if self.count != 0:
raise AssertionError("Heap error: Expected 0 keys, count is " + str(self.count))
self.minnode = None
return removed_node
# 2.2: Merge any roots with the same degree
logsize = 100
degreeroots = [None] * logsize
self.maxdegree = 0
currentpointer = self.minnode.next
while True:
currentdegree = currentpointer.degree
current = currentpointer
currentpointer = currentpointer.next
while degreeroots[currentdegree] != None:
other = degreeroots[currentdegree]
# Swap if required
if current.key > other.key:
temp = other
other = current
current = temp
other.remove()
current.addchild(other)
degreeroots[currentdegree] = None
currentdegree += 1
degreeroots[currentdegree] = current
if currentpointer == self.minnode:
break
self.minnode = None
newmaxdegree = 0
for d in range (0,logsize):
if degreeroots[d] != None:
degreeroots[d].next = degreeroots[d].previous = degreeroots[d]
self._insertnode(degreeroots[d])
if (d > newmaxdegree):
newmaxdegree = d
maxdegree = newmaxdegree
return removed_node
def decreasekey(self, node, newkey):
if newkey > node.key:
raise AssertionError("Cannot decrease a key to a greater value")
elif newkey == node.key:
return
node.key = newkey
parent = node.parent
if parent == None:
if newkey < self.minnode.key:
self.minnode = node
return
elif parent.key <= newkey:
return
while True:
parent.removechild(node)
self._insertnode(node)
if parent.parent == None:
break
elif parent.mark == False:
parent.mark
break
else:
node = parent
parent = parent.parent
continue