-
Notifications
You must be signed in to change notification settings - Fork 409
/
priority_queue.py
130 lines (100 loc) · 3.11 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
from abc import ABCMeta, abstractmethod
import itertools
from FibonacciHeap import FibHeap
import heapq
import Queue
class PriorityQueue():
__metaclass__ = ABCMeta
@abstractmethod
def __len__(self): pass
@abstractmethod
def insert(self, node): pass
@abstractmethod
def minimum(self): pass
@abstractmethod
def removeminimum(self): pass
@abstractmethod
def decreasekey(self, node, new_priority): pass
class FibPQ(PriorityQueue):
def __init__(self):
self.heap = FibHeap()
def __len__(self):
return self.heap.count
def insert(self, node):
self.heap.insert(node)
def minimum(self):
return self.heap.minimum()
def removeminimum(self):
return self.heap.removeminimum()
def decreasekey(self, node, new_priority):
self.heap.decreasekey(node, new_priority)
class HeapPQ(PriorityQueue):
def __init__(self):
self.pq = []
self.removed = set()
self.count = 0
def __len__(self):
return self.count
def insert(self, node):
entry = node.key, node.value
if entry in self.removed:
self.removed.discard(entry)
heapq.heappush(self.pq, entry)
self.count += 1
def minimum(self):
priority, item = heapq.heappop(self.pq)
node = FibHeap.Node(priority, item)
self.insert(node)
return node
def removeminimum(self):
while True:
(priority, item) = heapq.heappop(self.pq)
if (priority, item) in self.removed:
self.removed.discard((priority, item))
else:
self.count -= 1
return FibHeap.Node(priority, item)
def remove(self, node):
entry = node.key, node.value
if entry not in self.removed:
self.removed.add(entry)
self.count -= 1
def decreasekey(self, node, new_priority):
self.remove(node)
node.key = new_priority
self.insert(node)
class QueuePQ(PriorityQueue):
def __init__(self):
self.pq = Queue.PriorityQueue()
self.removed = set()
self.count = 0
def __len__(self):
return self.count
def insert(self, node):
entry = node.key, node.value
if entry in self.removed:
self.removed.discard(entry)
self.pq.put(entry)
self.count += 1
def minimum(self):
(priority, item) = self.pq.get_nowait()
node = FibHeap.Node(priority, item)
self.insert(node)
return node
def removeminimum(self):
while True:
(priority, item) = self.pq.get_nowait()
if (priority, item) in self.removed:
self.removed.discard((priority, item))
else:
self.count -= 1
return FibHeap.Node(priority, item)
def remove(self, node):
entry = node.key, node.value
if entry not in self.removed:
self.removed.add(entry)
self.count -= 1
def decreasekey(self, node, new_priority):
self.remove(node)
node.key = new_priority
self.insert(node)