-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create priority_queue_using_list.py #2428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f3ebf4c
3617e19
3529a68
3cdfb54
9c17c2c
738feef
735390b
670887e
a5a13d2
8726046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,273 @@ | ||||||||
""" | ||||||||
Pure Python implementation of Priority Queue using lists | ||||||||
""" | ||||||||
|
||||||||
|
||||||||
class Error(Exception): | ||||||||
"""Base class for exceptions in this module.""" | ||||||||
|
||||||||
pass | ||||||||
|
||||||||
|
||||||||
class OverFlow(Error): | ||||||||
def __init__(self, msg): | ||||||||
self.msg = msg | ||||||||
|
||||||||
|
||||||||
class UnderFlow(Error): | ||||||||
def __init__(self, msg): | ||||||||
self.msg = msg | ||||||||
|
||||||||
|
||||||||
class FixedPriorityQueue: | ||||||||
""" | ||||||||
In a Priority Queue the elements are entred as an when the come | ||||||||
But while removing or deleting an element the highest priority element is deleted | ||||||||
in FIFO fashion. | ||||||||
Here the lowest integer has the highest priority. | ||||||||
Example: | ||||||||
priority(0) > priority(5) | ||||||||
priority(16) > priority(32) | ||||||||
Here as an example I have taken only 3 priorities viz. 0, 1, 2 | ||||||||
0 Being the highest priority and 2 being the lowest | ||||||||
You can change the priorities as per your need | ||||||||
|
||||||||
Examples | ||||||||
>>> FPQ = FixedPriorityQueue() | ||||||||
>>> FPQ.enqueue(0, 10) | ||||||||
>>> FPQ.enqueue(1, 70) | ||||||||
>>> FPQ.enqueue(0, 100) | ||||||||
>>> FPQ.enqueue(2, 1) | ||||||||
>>> FPQ.enqueue(2, 5) | ||||||||
>>> FPQ.enqueue(1, 7) | ||||||||
>>> FPQ.enqueue(2, 4) | ||||||||
>>> FPQ.enqueue(1, 64) | ||||||||
>>> FPQ.enqueue(0, 128) | ||||||||
>>> FPQ.print_queue() | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Priority 0: [10, 100, 128] | ||||||||
Priority 1: [70, 7, 64] | ||||||||
Priority 2: [1, 5, 4] | ||||||||
>>> FPQ.dequeue() | ||||||||
10 | ||||||||
>>> FPQ.dequeue() | ||||||||
100 | ||||||||
>>> FPQ.dequeue() | ||||||||
128 | ||||||||
>>> FPQ.dequeue() | ||||||||
70 | ||||||||
>>> FPQ.dequeue() | ||||||||
7 | ||||||||
>>> FPQ.print_queue() | ||||||||
Priority 0: [] | ||||||||
Priority 1: [64] | ||||||||
Priority 2: [1, 5, 4] | ||||||||
>>> FPQ.dequeue() | ||||||||
64 | ||||||||
>>> FPQ.dequeue() | ||||||||
1 | ||||||||
>>> FPQ.dequeue() | ||||||||
5 | ||||||||
>>> FPQ.dequeue() | ||||||||
4 | ||||||||
>>> FPQ.dequeue() | ||||||||
Traceback (most recent call last): | ||||||||
... | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
priority_queue.UnderFlow: Under Flow! | ||||||||
""" | ||||||||
|
||||||||
def __init__(self): | ||||||||
self.queue = [ | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
[], | ||||||||
[], | ||||||||
[], | ||||||||
] | ||||||||
|
||||||||
def enqueue(self, priority, data): | ||||||||
""" | ||||||||
This function enters the element into the queue based on its priority | ||||||||
If the priority is invalid an Exception is raised saying Invalid Priority! | ||||||||
If the queue is full an Exception is raised saying Over Flow! | ||||||||
""" | ||||||||
if priority > 2: | ||||||||
raise Exception("Invalid Priority!") | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
elif len(self.queue[priority]) == 100: | ||||||||
raise OverFlow("Over Flow!") | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
else: | ||||||||
self.queue[priority].append(data) | ||||||||
|
||||||||
def dequeue(self): | ||||||||
""" | ||||||||
Return the highest priority element in FIFO order. | ||||||||
If the queue is empty then an under flow exception is raised. | ||||||||
""" | ||||||||
if not self.queue[0] and not self.queue[1] and not self.queue[2]: | ||||||||
raise UnderFlow("Under Flow!") | ||||||||
|
||||||||
if len(self.queue[0]) != 0: | ||||||||
return self.queue[0].pop(0) | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
elif len(self.queue[1]) != 0: | ||||||||
return self.queue[1].pop(0) | ||||||||
else: | ||||||||
return self.queue[2].pop(0) | ||||||||
|
||||||||
def print_queue(self): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Instead, please do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
||||||||
print("Priority 0:", self.queue[0]) | ||||||||
print("Priority 1:", self.queue[1]) | ||||||||
print("Priority 2:", self.queue[2]) | ||||||||
|
||||||||
def __str__(self): | ||||||||
""" | ||||||||
Prints each priority queue within the FixedPriorityQueue | ||||||||
""" | ||||||||
s = "" | ||||||||
for i in range(len(self.queue)): | ||||||||
for j in self.queue[i]: | ||||||||
s += "Priority " + str(i) + ": " | ||||||||
s += str(i) + " " | ||||||||
s += "\n" | ||||||||
print(s) | ||||||||
return s | ||||||||
|
||||||||
|
||||||||
class ElementPriorityQueue: | ||||||||
""" | ||||||||
Element Priority Queue is the same as Fixed Priority Queue | ||||||||
The only difference being the value of the element itself is the priority | ||||||||
The rule for priorities are the same | ||||||||
The lowest integer has the highest priority. | ||||||||
Example: | ||||||||
priority(0) > priority(5) | ||||||||
priority(16) > priority(32) | ||||||||
You can change the priorities as per your need | ||||||||
|
||||||||
>>> EPQ = ElementPriorityQueue() | ||||||||
>>> EPQ.enqueue(10) | ||||||||
>>> EPQ.enqueue(70) | ||||||||
>>> EPQ.enqueue(100) | ||||||||
>>> EPQ.enqueue(1) | ||||||||
>>> EPQ.enqueue(5) | ||||||||
>>> EPQ.enqueue(7) | ||||||||
>>> EPQ.enqueue(4) | ||||||||
>>> EPQ.enqueue(64) | ||||||||
>>> EPQ.enqueue(128) | ||||||||
>>> EPQ.print_queue() | ||||||||
[10, 70, 100, 1, 5, 7, 4, 64, 128] | ||||||||
>>> EPQ.dequeue() | ||||||||
1 | ||||||||
>>> EPQ.dequeue() | ||||||||
4 | ||||||||
>>> EPQ.dequeue() | ||||||||
5 | ||||||||
>>> EPQ.dequeue() | ||||||||
7 | ||||||||
>>> EPQ.dequeue() | ||||||||
10 | ||||||||
>>> EPQ.print_queue() | ||||||||
[70, 100, 64, 128] | ||||||||
>>> EPQ.dequeue() | ||||||||
64 | ||||||||
>>> EPQ.dequeue() | ||||||||
70 | ||||||||
>>> EPQ.dequeue() | ||||||||
100 | ||||||||
>>> EPQ.dequeue() | ||||||||
128 | ||||||||
>>> EPQ.dequeue() | ||||||||
Traceback (most recent call last): | ||||||||
... | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
priority_queue.UnderFlow: Under Flow! | ||||||||
""" | ||||||||
|
||||||||
def __init__(self): | ||||||||
self.queue = [] | ||||||||
|
||||||||
def enqueue(self, data): | ||||||||
""" | ||||||||
This function enters the element into the queue | ||||||||
If the queue is full an Exception is raised saying Over Flow! | ||||||||
""" | ||||||||
if len(self.queue) == 100: | ||||||||
raise OverFlow("Over Flow!") | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
else: | ||||||||
self.queue.append(data) | ||||||||
|
||||||||
def dequeue(self): | ||||||||
""" | ||||||||
Return the highest priority element in FIFO order. | ||||||||
If the queue is empty then an under flow exception is raised. | ||||||||
""" | ||||||||
if len(self.queue) == 0: | ||||||||
raise UnderFlow("Under Flow!") | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
else: | ||||||||
data = min(self.queue) | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
self.queue.remove(data) | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return data | ||||||||
|
||||||||
def print_queue(self): | ||||||||
print(self.queue) | ||||||||
|
||||||||
def __str__(self): | ||||||||
""" | ||||||||
Prints all the elements within the Element Priority Queue | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
""" | ||||||||
s = "" | ||||||||
for i in self.queue: | ||||||||
Ashley-J-George marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
s += str(i) + " " | ||||||||
return s | ||||||||
|
||||||||
|
||||||||
def fixed_priority_queue(): | ||||||||
FPQ = FixedPriorityQueue() | ||||||||
FPQ.enqueue(0, 10) | ||||||||
FPQ.enqueue(1, 70) | ||||||||
FPQ.enqueue(0, 100) | ||||||||
FPQ.enqueue(2, 1) | ||||||||
FPQ.enqueue(2, 5) | ||||||||
FPQ.enqueue(1, 7) | ||||||||
FPQ.enqueue(2, 4) | ||||||||
FPQ.enqueue(1, 64) | ||||||||
FPQ.enqueue(0, 128) | ||||||||
str(FPQ) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
str(FPQ) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
print(FPQ.dequeue()) | ||||||||
|
||||||||
|
||||||||
def element_priority_queue(): | ||||||||
EPQ = ElementPriorityQueue() | ||||||||
EPQ.enqueue(10) | ||||||||
EPQ.enqueue(70) | ||||||||
EPQ.enqueue(100) | ||||||||
EPQ.enqueue(1) | ||||||||
EPQ.enqueue(5) | ||||||||
EPQ.enqueue(7) | ||||||||
EPQ.enqueue(4) | ||||||||
EPQ.enqueue(64) | ||||||||
EPQ.enqueue(128) | ||||||||
str(EPQ) | ||||||||
print(EPQ) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
str(EPQ) | ||||||||
print(EPQ) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
print(EPQ.dequeue()) | ||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
fixed_priority_queue() | ||||||||
element_priority_queue() |
Uh oh!
There was an error while loading. Please reload this page.