A priority queue is a collection of prioritized elements that allows arbitrary element insertion, and allows the removal of the element that has the highest priority. When an element is added to a priority queue, the user designates its priority by providing an associated priority. The element with the minimum priority will be the next to be removed from the queue.
In other words, a piority queue is data structure where each node contains two values:
Note
The priority values are always hidden. If you want to see the priorty for each item in the PriorityQueue(), you can set the static variable SHOW_PRIORITY to True just like so:
>>> PriorityQueue.SHOW_PRIORITY = True
The following table sums up all the different public functionality in this class and also provides the worst-case time complexity along side with the optimal time complexity that I will try to reach in future releases Insha’Allah. Generally, we are going to use the following indicators in the table:
Method | Description | Worst-case | Optimal |
---|---|---|---|
__len__() | Returns the number of values in the queue. | O(1) | O(1) |
enqueue() | Adds new value to the top of the queue. | O(1) | O(1) |
dequeue() | Adds the value from the top of the queue. | O(n) | O(n) |
top() | Returns the value at the top of the queue. | O(n) | O(n) |
clear() | Clears the priority queue. | O(1) | O(1) |
is_empty() | Checks if the priority queue is empty. | O(1) | O(1) |
is_full() | Checks if the priority queue is full. | O(1) | O(1) |
Here are all of the public methods that can be used with PriorityQueue() objects:
extra.lists.priority_queue.
PriorityQueue
(max_capacity=inf, seed=None)¶A priority queue is a collection of prioritized elements that allows arbitrary element insertion, and allows the removal of the element that has the highest priority. When an element is added to a priority queue, the user designates its priority by providing an associated priority. The element with the minimum priority will be the next to be removed from the queue.
__init__
(max_capacity=inf, seed=None)¶Creates a PriorityQueue() object!!
Parameters: |
|
---|---|
Raises: |
|
Example
>>> pq = PriorityQueue()
>>> type(pq)
<class 'extra.lists.priority_queue.PriorityQueue'>
>>> pq._max_capacity
inf
You can define the maximum capacity for your own instance:
>>> pq = PriorityQueue(10)
>>> pq._max_capacity
10
Note
If you passed a float number as the maximum capacity, then the value that get assigned is the rounding of that number:
>>> pq = PriorityQueue(10.6)
>>> pq._max_capacity
11
__len__
()¶Gets the length of the PriorityQueue() instance in constant time.
Returns: | The length of the PriorityQueue() instance. By Length, I mean the number of elements of in the instance. |
---|---|
Return type: | int |
Examples
>>> pq = PriorityQueue()
>>> len(pq)
0
>>> pq.enqueue(1)
>>> pq.enqueue(2)
>>> pq.enqueue(3)
>>> len(pq)
3
__repr__
()¶Represents the PriorityQueue() instance as a string.
Returns: | The string-representation of the PriorityQueue() instance. |
---|---|
Return type: | str |
Example
>>> pq = PriorityQueue()
>>> pq.enqueue(10, priority=1)
>>> pq.enqueue(20, priority=5)
>>> pq
─┬────┬────┬─
⟶│ 20 │ 10 │⟶
─┴────┴────┴─
You can show the priority of these items by enabling SHOW_PRIOIRTY static variable:
>>> PriorityQueue.SHOW_PRIORITY = True
>>> pq
─┬────────┬────────┬─
⟶│ 20|P:5 │ 10|P:1 │⟶
─┴────────┴────────┴─
clear
()¶Removes all objects within the PriorityQueue() instance in constant time.
Example
>>> pq = PriorityQueue()
>>> pq.enqueue(1)
>>> pq.enqueue(2)
>>> pq.enqueue(3)
>>> pq
─┬───┬───┬───┬─
⟶│ 3 │ 2 │ 1 │⟶
─┴───┴───┴───┴─
>>> len(pq)
3
>>> pq.clear()
>>> pq
─┬
⟶│
─┴
>>> len(pq)
0
Note
When you clear the PriorityQueue() instance, the max_capacity of the cleared instance remains the same as the one before.
dequeue
(lowest_priority=False)¶Pops the item that has the highest priority from the PriorityQueue() instance in constant time.
Returns: | The item that has the highest priority in the PriorityQueue(). |
---|---|
Return type: | object |
Raises: | UserWarning: – If the PriorityQueue() instance is empty!! |
Example
>>> pq = PriorityQueue()
>>> pq.dequeue()
UserWarning: Dequeuing from empty `extra.PriorityQueue()`!!
>>> pq.enqueue(10, priority=0)
>>> pq.enqueue(20, priority=2)
>>> PriorityQueue.SHOW_PRIORITY = True
>>> pq
─┬────────┬────────┬─
⟶│ 20|P:2 │ 10|P:0 │⟶
─┴────────┴────────┴─
>>> pq.dequeue()
20
>>> pq
─┬────────┬─
⟶│ 10|P:0 │⟶
─┴────────┴─
enqueue
(item, priority=None)¶Inserts the given item to the end of the PriorityQueue(), it does that in constant time.
Parameters: |
|
---|---|
Raises: |
|
Example
>>> pq = PriorityQueue(max_capacity=2)
>>> pq
─┬
⟶│
─┴
>>> pq.enqueue(1)
>>> pq.enqueue(2)
>>> pq
─┬───┬───┬─
⟶│ 2 │ 1 │⟶
─┴───┴───┴─
>>> pq.enqueue(3)
UserWarning: Enqueuing to a full `extra.PriorityQueue()` could lead to missing values!!
>>> pq
─┬───┬───┬─
⟶│ 3 │ 2 │⟶
─┴───┴───┴─
is_empty
()¶Checks if PriorityQueue() instance is empty or not in constant time.
Returns: | A boolean flag showing if the PriorityQueue() instance is empty or not. True shows that this instance is empty and False shows it’s not empty. |
---|---|
Return type: | bool |
Example
>>> pq = PriorityQueue()
>>> pq.is_empty()
True
>>> pq.enqueue(5)
>>> pq.is_empty()
False
is_full
()¶Checks if PriorityQueue() instance is at full-capacity in constant time.
Returns: | A boolean flag showing if the PriorityQueue() instance is full or not. True shows that this instance is full and False shows it’s not full. |
---|---|
Return type: | bool |
Example
>>> pq = PriorityQueue(max_capacity=2)
>>> pq.is_full()
False
>>> pq.enqueue(5)
>>> pq.is_full()
False
>>> pq.enqueue(10)
>>> pq.is_full()
True
top
()¶Returns the first item inserted to the PriorityQueue() instance in constant time.
Returns: | The PriorityQueue() instance’s first inserted item. |
---|---|
Return type: | object |
Raises: | IndexError: – If the PriorityQueue() instance is empty!! |
Example
>>> pq = PriorityQueue()
>>> pq.top()
IndexError: Can't retrieve from an empty `extra.PriorityQueue()`!!
>>> pq.enqueue(10)
>>> pq.enqueue(20)
>>> pq
─┬────┬────┬─
⟶│ 20 │ 10 │⟶
─┴────┴────┴─
>>> pq.top()
10