-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.py
More file actions
278 lines (227 loc) · 7.71 KB
/
priority_queue.py
File metadata and controls
278 lines (227 loc) · 7.71 KB
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
from empty import Empty
from positional_list import PositionalList
class PriorityQueue:
"""
Abstract base class for priority queue implementations
"""
#-------------------------------------------------------------------
class _Item:
"""
Composite design pattern - lightweight class stores access count
_key: the access count
_value: the value stored by the item
"""
__slots__ = '_key', '_value'
def __init__(self, k, v):
self._key = k
self._value = v
def __lt__(self, other):
return self._key < other._key
#-------------------------------------------------------------------
def is_empty(self):
"""
return: True if the Priority Queue is empty, false otherwise
"""
return len(self) == 0
#------------------Concrete Implementations------------------------------
class UnsortedPriorityQueue(PriorityQueue):
"""
An implementstion of a PriorityQueue
Store items in an unsorted list
Requires searching the entire list when accessing items
"""
def __init__(self):
"""
Creates an empty PriorityQueue
"""
self._data = PositionalList()
def __len__(self):
"""
returns: the number of items in the PriorityQueue
"""
return len(self._data)
def add(self, key, value):
"""
Adds a key-value pair to the PriorityQueue
"""
self._data.add_last(self._Item(key, value))
def _find_min(self):
"""
Non-public utility method
returns: the item with the minimum key
"""
if self.is_empty():
raise Empty('Priority Queue is empty')
minimum = self._data.first()
walk = self._data.after(minimum)
while walk is not None:
if walk.element() < minimum.element(): # __lt__ is implemented by the PriorityQueue class
minimum = walk
walk = self._data.after(walk)
return minimum
def min(self):
"""
returns: the (k,v) tuple with the minimum key, without removing it
"""
p = self._find_min()
item = p.element()
return (item._key, item._value)
def remove_min(self):
"""
removes the minimum (k,v) tuple from the priority queue
returns: the (k,v) tuple
"""
p = self._find_min()
item = self._data.delete(p) # PositionalList removes and returns item
return (item._key, item._value)
#--------------------------------------------------------------------------------
class SortedPriorityQueue(PriorityQueue):
"""
Implementation of a PriorityQueue using a sorted PositionalList
Adds items to the list in order
"""
def __init__(self):
"""
Creates an empty PriorityQueue
"""
self._data = PositionalList()
def __len__(self):
"""
returns: the number of items in the PriorityQueue
"""
return len(self._data)
def add(self, key, value):
"""
Adds a key-value pair to the PriorityQueue
"""
new_item = self._Item(key, value)
walk = self._data.last()
while walk is not None and new_item < walk.element():
walk = self._data.before(walk)
if walk is not None:
self._data.add_after(walk, new_item)
else:
self._data.add_first(new_item)
def min(self):
"""
returns: the (k,v) tuple with the minimum key, without removing it
"""
if self.is_empty():
raise Empty('Priority Queue is empty')
p = self._data.first()
item = p.element()
return (item._key, item._value)
def remove_min(self):
"""
removes the minimum (k,v) tuple from the priority queue
returns: the (k,v) tuple
"""
if self.is_empty():
raise Empty('Priority Queue is empty')
p = self._data.first()
item = self._data.delete(p)
return (item._key, item._value)
#----------------------------------------------------------------------------------
class HeapPriorityQueue(PriorityQueue):
"""
An implementation of a Priorrity Queue using an array based heap data structure
-------------------------------------------------------------------------------
uses the _Item class from PriorityQueue
"""
#--------Non-Public methods-------
# 0
# / \
# 1 2
# / \ / \
# 3 4 5 6
def _parent(self, i):
return (i - 1) // 2
def _left(self, i):
return i * 2 + 1
def _right(self, i):
return i * 2 + 2
def _has_left(self, i):
return self._left(i) < len(self._data)
def _has_right(self, i):
return self._right(i) < len(self._data)
def _swap(self, i, j):
"""
Swaps the the elements stored at indexex i and j
"""
self._data[i], self._data[j] = self._data[j], self._data[i]
def _upheap(self, i):
"""
Performed after inserting a new element into the heap
Restores heap-order property
"""
parent = self._parent(i)
if i > 0 and self._data[i] < self._data[parent]:
self._swap(i, parent)
self._upheap(parent)
def _downheap(self, i):
"""
Performed after removing the minimum element from the heap
Restores heap-order property
"""
if self._has_left(i):
left = self._left(i)
smallest = left
if self._has_right(i):
right = self._right(i)
if self._data[right] < self._data[left]:
smallest = right
if self._data[smallest] < self._data[i]:
self._swap(i, smallest)
self._downheap(smallest)
def _heapify(self):
"""
Performs bottom-up construction for a new heap
"""
# 0 0 X
# / \ / \ / \
# 0 0 X X X X
# / \ / \ / \ / \ / \ / \
# X X X X X X X X X X X X
start = self._parent(len(self) - 1) # starts at the parent of the last leaf
for i in range(start, -1, -1):
self._downheap(i)
#--------Public methods-----------
def __init__(self, contents=()):
"""
Create an new Priority Queue
If contents is given creates a heap using bottom up construction,
otherwise creates an empty heap
contents: an iterable sequence of (k,v) tuples
"""
self._data = [self._Item(k, v) for k, v in contents]
if len(self._data) > 1:
self._heapify()
def __len__(self):
"""
returns: the number of elements in the priority queue
"""
return len(self._data)
def add(self, key, value):
"""
Adds a key-value pair to te priority queue
"""
self._data.append(self._Item(key, value))
self._upheap(len(self._data) - 1)
def min(self):
"""
returns: the key-value pair with the minimum key without removal
"""
if self.is_empty():
raise Empty('Priority Queue is empty')
item = self._data[0]
return (item._key, item._value)
def remove_min(self):
"""
returns: the key-value pair with the minimal key removed from the priority queue
"""
if self.is_empty():
raise Empty('Priority Queue is empty')
self._swap(0, len(self._data) - 1)
item = self._data.pop()
self._downheap(0)
return (item._key, item._value)