Skip to content

Latest commit

 

History

History
65 lines (48 loc) · 1.96 KB

File metadata and controls

65 lines (48 loc) · 1.96 KB

Priority Queue

  • Max Priority Queue: the list will be arranged in descending order of their priority
  • Min Priority Queue: the list will be arranged in ascending order of their priority

Three ways to implement Priority queue: [ref]

  1. List
  2. queue.PriorityQueue
from queue import priorityQueue

pq = PriorityQueue()
pq.put((10,'Red balls'))
pq.put((8,'Pink balls'))
pq.put((5,'White balls'))
pq.put((4,'Green balls'))
while not pq.empty():
    item = pq.get() # POPS + return smallest priority item
    print(item)
    
# ” item “ then the output will appear as: 
# (4, ‘Green balls’) 
# (5, ‘White balls’) 
# (8, ‘Pink balls’) 
# (10, ‘Red balls’) ”

More priortyQueue functions:

  • qsize() – Returns the current queue size.
  • empty() – Returns True if the queue is empty, False otherwise. It is equivalent to qsize()==0.
  • full() – Returns True if the queue is full, False otherwise. It is equivalent to qsize()>=maxsize.

3. heapq

import heapq
pq = []
heapq.heappush(pq,(4, "Tom"))
heapq.heappush(pq,(1, "Aruhi"))
heapq.heappush(pq,(3, "Dyson"))
heapq.heappush(pq,(2, "Bob"))
while pq:
    deque_item = heapq.heappop(pq)
    print(deque_item)
    
#  (1, ‘Aruhi’) 
#  (2, ‘Bob’) 
#  (3, ‘Dyson’) 
#  (4, ‘Tom’)

Resources

Theory:

Implementation: