Skip to content

Commit 12fc51e

Browse files
committed
Add py min pq
1 parent 01ec8d2 commit 12fc51e

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

python/data_structures/p165_min_pq.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
CLRS Exercise 6.5-3, p. 165
3+
'''
4+
5+
from .p156_min_heap import MinHeap
6+
import math
7+
8+
class MinPQ(MinHeap):
9+
def __init__(self, A, heap_size):
10+
super().__init__(A, heap_size)
11+
12+
def heap_minimum(self):
13+
return self.A[0]
14+
15+
def heap_extract_min(self):
16+
if self.heap_size < 1:
17+
raise Exception('heap underflow')
18+
min = self.heap_minimum()
19+
self.A[0] = self.A.pop()
20+
self.heap_size -= 1
21+
self.min_heapify(0)
22+
return min
23+
24+
def heap_decrease_key(self, i, key):
25+
if key > self.A[i]:
26+
raise Exception('new key is larger than current key')
27+
self.A[i] = key
28+
while i > 0 and self.A[self.parent(i)] > self.A[i]:
29+
self.A[i], self.A[self.parent(i)] = self.A[self.parent(i)], self.A[i]
30+
i = self.parent(i)
31+
32+
def min_heap_insert(self, key):
33+
self.heap_size += 1
34+
self.A.append(math.inf)
35+
self.heap_decrease_key(self.heap_size - 1, key)
36+
37+
# N.B. cf. Exercise 6.5-8, p. 166
38+
def heap_delete(self, i):
39+
pass # TO-DO

python/main.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,35 @@
137137
A = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] # cf. Figure 6.5, p. 164
138138
max_pq = MaxPQ(A, len(A))
139139
max_pq.build_max_heap()
140-
print('\nafter buildMaxHeap:', max_pq.A)
140+
print('\nafter build_max_heap:', max_pq.A)
141141
max_pq.heap_increase_key(8, 15)
142-
print('after heapIncreaseKey(4 -> 15):', max_pq.A)
142+
print('after heap_increase_key(4 -> 15):', max_pq.A)
143143

144144
A = [15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1] # cf. Exercise 6.5-2, p. 165
145145
max_pq = MaxPQ(A, len(A))
146146
max_pq.build_max_heap()
147-
print('\nafter buildMaxHeap:', max_pq.A)
147+
print('\nafter build_max_heap:', max_pq.A)
148148
max_pq.max_heap_insert(10)
149-
print('after maxHeapInsert(10):', max_pq.A)
149+
print('after max_heap_insert(10):', max_pq.A)
150150

151151
# TO-DO: fix heap_delete
152+
153+
# CLRS Section 6.5 Exercises - Min Priority Queue
154+
from data_structures.p165_min_pq import MinPQ
155+
A = []
156+
157+
print('\nExercise 6.5-3 - Min Priority Queue')
158+
159+
A = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] # cf. Figure 6.5, p. 164
160+
min_pq = MinPQ(A, len(A))
161+
min_pq.build_min_heap()
162+
print('\nafter build_min_heap:', min_pq.A)
163+
min_pq.heap_decrease_key(8, 15)
164+
print('after heap_decrease_key(4 -> 15):', min_pq.A)
165+
166+
A = [15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1] # cf. Exercise 6.5-2, p. 165
167+
min_pq = MinPQ(A, len(A))
168+
min_pq.build_min_heap()
169+
print('\nafter build_min_heap:', min_pq.A)
170+
min_pq.min_heap_insert(10)
171+
print('after min_heap_insert(10):', min_pq.A)

0 commit comments

Comments
 (0)