-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.py
More file actions
33 lines (27 loc) · 865 Bytes
/
PriorityQueue.py
File metadata and controls
33 lines (27 loc) · 865 Bytes
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
import heapq
class PriorityQueue:
def __init__(self):
self.heap = []
self.counter = 0
def insert(self, key, value):
entry = [key, self.counter, value]
self.counter += 1
heapq.heappush(self.heap, entry)
def extract_min(self):
while self.heap:
priority, _, value = heapq.heappop(self.heap)
if value is not None:
return priority, value
raise KeyError('Pop from an empty priority queue')
def decrease_key(self, key, value):
index = -1
for i, entry in enumerate(self.heap):
if entry[2] == value:
index = i
break
if index != -1:
self.heap.pop(index)
# self.counter -= 1
self.insert(key, value)
def is_empty(self):
return len(self.heap) == 0