-
Notifications
You must be signed in to change notification settings - Fork 2
/
priority_queue.py
169 lines (140 loc) · 4.02 KB
/
priority_queue.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
class PriorityQueue:
def __init__(self, less):
self.heap = Heap([], less, update_position)
self.get_key_and_pos = {}
def __repr__(self):
return repr(self.heap)
def push(self, key):
kp = KeyWithPosition(key)
self.get_key_and_pos[key] = kp
self.heap.insert(kp)
def pop(self):
return self.heap.extract_max().key
def increase_key(self, key):
obj = self.get_key_and_pos[key]
heap_increase_key(self.heap, obj.position)
def empty(self):
return self.heap.heap_size == 0
class KeyWithPosition:
def __init__(self, k):
self.key = k
self.position = -1
def __repr__(self):
return str(self.key) + '@' + repr(self.position)
def update_position(key_with_pos, pos):
key_with_pos.position = pos
class Heap:
def __init__(self, data, less, update):
self.data = data
self.less = less
self.update = update
i = 0
for obj in self.data:
self.update(obj, i)
i += 1
build_max_heap(self)
def __repr__(self):
return repr(self.data[:self.heap_size])
def maximum(self):
return self.data[0]
def insert(self, obj):
self.heap_size += 1
if len(self.data) < self.heap_size:
self.data.append(obj)
else:
self.data[self.heap_size - 1] = obj
self.update(obj, self.heap_size - 1)
heap_increase_key(self, self.heap_size - 1)
def extract_max(self):
assert self.heap_size != 0
max = self.data[0]
self.data[0] = self.data[self.heap_size-1]
self.update(self.data[0], 0)
self.heap_size -= 1
max_heapify(self, 0)
return max
def left(i):
return 2 * i + 1
def right(i):
return 2 * (i + 1)
def parent(i):
return (i-1) // 2
def swap(A, i, j):
tmp = A[i]
A[i] = A[j]
A[j] = tmp
def heap_increase_key(H, i):
while i > 0 and H.less(H.data[parent(i)], H.data[i]):
swap(H.data, i, parent(i))
H.update(H.data[i], i)
H.update(H.data[parent(i)], parent(i))
i = parent(i)
def max_heapify(H, i):
l = left(i)
r = right(i)
if l < H.heap_size and H.less(H.data[i], H.data[l]):
largest = l
else:
largest = i
if r < H.heap_size and H.less(H.data[largest], H.data[r]):
largest = r
if largest != i:
swap(H.data, i, largest)
H.update(H.data[i], i)
H.update(H.data[largest], largest)
max_heapify(H, largest)
def build_max_heap(H):
H.heap_size = len(H.data)
last_parent = len(H.data) // 2
for i in range(last_parent, -1, -1):
max_heapify(H, i)
def heap_sort(H):
build_max_heap(H)
for i in range(len(H.data)-1, 0, -1):
swap(H.data, 0, i)
H.update(H.data[0], 0); H.update(H.data[i], i)
H.heap_size -= 1
max_heapify(H, 0)
def less(x, y):
return x < y
def ignore_update(obj, pos):
pass
if __name__ == "__main__":
# test Heap build and extract_max
L = [4,3,5,1,2]
h = Heap(L, less, ignore_update)
for i in range(5, 0, -1):
assert h.extract_max() == i
# test Heap insert
L = [4,3,5,1,2]
for k in L:
h.insert(k)
for i in range(5, 0, -1):
assert h.extract_max() == i
# test heap_sort
L = [4,3,5,1,2]
h = Heap(L, less, ignore_update)
heap_sort(h)
for i in range(0, 5):
assert L[i] == i + 1
# test PriorityQeueue push and pop
L = {'a': 4, 'b': 3, 'c':5,'d':1,'e':2}
def less(x, y):
return L[x.key] < L[y.key]
Q = PriorityQueue(less)
for k, v in L.items():
Q.push(k)
P = ['c', 'a', 'b', 'e', 'd']
for i in range(0, len(P)):
assert Q.pop() == P[i]
# test increase_key
Q = PriorityQueue(less)
for k, v in L.items():
Q.push(k)
for k, v in L.items():
L[k] = v + 2
Q.increase_key(k)
for i in range(5, 0, -1):
k = Q.pop()
assert L[k] == i + 2
print('passed all tests')