-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriority_queue.py
More file actions
executable file
·74 lines (59 loc) · 2.12 KB
/
Priority_queue.py
File metadata and controls
executable file
·74 lines (59 loc) · 2.12 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
class PriorityQueue:
def __init__(self):
self.Queue = []
def Add(self, element: any, priority: int) -> None:
"""
I used an int type to classify priority why ?
it's much easier to implement and it's has infinity cases
since numbers are inexhaustible (infinity)
one could have a billion or million priorities
"""
import operator
self.Queue.append((element, priority))
self.Queue.sort(key=operator.itemgetter(1))
def Display(self) -> list:
return self.Queue
def Pop(self) -> any:
if not self.Queue:
raise IndexError
return self.Queue.pop(0)[0]
def ChangePriority(self, element: any, new_priority: int) -> None:
for I in self.Queue:
if I[0] == element:
self.Queue.remove(
I
) # using pop is better O(1) but I'm lazy remove uses O(n)
self.Add(element, new_priority)
break
else:
raise ValueError
"""
I didn't use any in-built method - heapq
my method above works for priority queue
but due to the sorting on every Add the
time complexity becomes O(n²Log(n),no way to improve unless
with the heapq module which sorts internally
Even if I tried sorting before pop function,the time
complexity still remains O(n²log(n))
"""
from heapq import heappop, heappush, heapify
class PQueue: # a more optimized version
def __init__(self):
self.queue = []
def Append(self, element: any, priority: int) -> None:
heappush(self.queue, (priority, element))
def Pop(self):
if not self.queue:
raise IndexError
return heappop(self.queue)[1]
def Display(self) -> list:
return self.queue.copy()
def ChangePriority(self, element: any, new_priority: int) -> None:
for I in self.queue:
if I[1] == element:
self.queue.remove(I)
heapify(self.queue)
heappush(self.queue, (new_priority, element))
break
else:
raise ValueError