-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queues.py
166 lines (126 loc) · 4.25 KB
/
priority_queues.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
import time
import random
###########################################
### Unordered Array Max Priority Queue. ###
###########################################
class UnorderedMaxPQ:
# NOTE: The append() function not used to practise resizing array
# implementation.
pq = [None] * 2 # (Resizing)Array of the priority queue.
N = 0 # Size.
# Return true if the priority queue is empty.
def isEmpty(self):
return self.N == 0
# Insert a key into the priority queue.
def insert(self, key):
if (key == None):
raise TypeError('Cannot insert item of type None.')
if self.N == len(self.pq):
self.__resize(2 * self.N)
self.pq[self.N] = key
self.N += 1
# Return and remove the largest key.
def delMax(self):
if self.isEmpty():
raise RuntimeError('Cannot delete from empty priority queue')
m = 0
for i in range(self.N):
if self.pq[i] > self.pq[m]:
m = i
self.__exchange(m, self.N - 1)
m = self.pq[self.N - 1]
self.pq[self.N - 1] = None
self.N -= 1
if (self.N > 0 and self.N == len(self.pq) // 4):
self.__resize(len(self.pq) // 2)
return m
###
### HELPER FUNCTIONS
###
def __resize(self, capacity):
temp = [None] * capacity
for i in range(self.N):
temp[i] = self.pq[i]
self.pq = temp
def __exchange(self, a, b):
temp = self.pq[a]
self.pq[a] = self.pq[b]
self.pq[b] = temp
# Experiments to check time complexity.
if __name__ == "__main__":
M = UnorderedMaxPQ()
for i in range(100000):
M.insert(random.randint(0, 1000000))
startTime = time.time()
M.delMax()
print('Time taken to delete max from 100000 elements: ' + str(time.time() -
startTime) + 'seconds')
for i in range(100001):
M.insert(random.randint(0, 1000000))
startTime = time.time()
M.delMax()
print('Time taken to delete max from 200000 elements: ' + str(time.time() -
startTime) + 'seconds')
#######################################
### Binary Heap Max Priority Queue. ###
#######################################
class MaxPQ(UnorderedMaxPQ):
pq = [None] * 3 # (Resizing)Array of the priority queue.
N = 0 # Size.
# Insert a key into the priority queue.
def insert(self, key):
if (key == None):
raise TypeError('Cannot insert item of type None.')
if self.N == len(self.pq) - 1:
self.__resize(2 * (self.N + 1))
self.N += 1
self.pq[self.N] = key
self.__swim(self.N)
# Return and remove the largest key.
def delMax(self):
if self.N == 0:
raise RuntimeError('Cannot delete from empty priority queue.')
self._UnorderedMaxPQ__exchange(1, self.N)
m = self.pq[self.N]
self.pq[self.N] = None
self.N -= 1
self.__sink(1)
if (self.N > 0 and self.N + 1 == len(self.pq) // 4):
self.__resize(len(self.pq) // 2)
return m
###
### HELPER FUNCTIONS
###
def __swim(self, k):
while (k > 1 and self.pq[k // 2] < self.pq[k]):
self._UnorderedMaxPQ__exchange(k, k // 2)
k //= 2
def __resize(self, capacity):
temp = [None] * capacity
for i in range(self.N + 1):
temp[i] = self.pq[i]
self.pq = temp
def __sink(self, k):
while (k <= self.N // 2):
temp = 2 * k
if temp < self.N and self.pq[temp + 1] > self.pq[temp]:
temp += 1
if self.pq[k] > self.pq[temp]:
break
self._UnorderedMaxPQ__exchange(temp, k)
k = temp
# Experiments to check time complexity.
if __name__ == "__main__":
M = MaxPQ()
for i in range(100000):
M.insert(random.randint(0, 1000000))
startTime = time.time()
M.delMax()
print('Time taken to delete max from 100000 elements: ' + str(time.time() -
startTime) + 'seconds')
for i in range(100001):
M.insert(random.randint(0, 1000000))
startTime = time.time()
M.delMax()
print('Time taken to delete max from 200000 elements: ' + str(time.time() -
startTime) + 'seconds')