Intro

Priority Queue is a Queue where:

  1. every item in the queue has a priority, and
  2. higher-priority items are dequeued before lower-priority items

Usage

Comparison

Advantages

Disadvantages

Problems

Corner Cases

Implementation

Priority queues can be implemented using

Binary Heaps

Sorted List

Sorted Linked List

Time Complexity
Operations Worst case
peek() O(1)
dequeue() O(log n)
enqueue() O(log n)
Space Complexity
Implementation Worst case
Priority Queue as a Binary Heap O(n)

n is the number of elements in the priority queue

Algorithms

Dijkstra's Shortest-Path

A* search (a graph traversal algorithm like BFS)

Huffman codes (an encoding for data compression)

Patterns

Code

class Node:
    def __init__(self, val, priority):
        self.value = val
        self.priority = priority
        self.next = None

class Pqueue:
    def __init__(self):
        self.Front = None
        self.Rear = None

    def enqueue(self, val, priority=None):
        if self.empty() is True:
            self.Front = Node(val, priority)
            self.Rear = self.Front
            return
        inst = Node(val, priority)
        if inst.priority < self.Front.priority:
            inst.next = self.Front
            self.Front = inst
        elif inst.priority >= self.Rear.priority:
            self.Rear.next = inst
            self.Rear = self.Rear.next
        elif inst.priority == self.Front.priority:
            n = self.Front.next
            f = self.Front
            while n.priority == inst.priority:
                n = n.next
                f = f.next
            f.next = inst
            f.next.next = n
        else:
            f = self.Front.next
            n = f.next
            while n.priority <= inst.priority:
                n = n.next
                f = f.next
            f.next = inst
            f.next.next = n

    def dequeue(self):
        if self.empty() is True:
            print("Queue is empty")
            return -1
        elif self.Front == self.Rear:
            temp = self.Front.value
            self.Front = None
            self.Rear = None
            return temp
        else:
            temp = self.Front.value
            self.Front = self.Front.next
            return temp

    def front(self):
        if self.empty() is True:
            print("Queue empty")
            return -1
        else:
            return self.Front.value

    def rear(self):
        if self.empty() is True:
            print("Queue empty")
            return -1
        else:
            return self.Rear.value

    def empty(self):
        if self.Rear is None:
            return True
        else:
            return False

    def display(self):
        print("The Queue")
        if self.empty() is True:
            print("Queue empty")
        elif self.Front == self.Rear:
            print("val priority")
            print(self.Front.value, "\\t", self.Front.priority,  "<== FRONT <== REAR")
        else:
            i = self.Front.next
            print("val priority")
            print(self.Front.value, "\\t", self.Front.priority, "<== FRONT")
            while i is not self.Rear:
                print(i.value, "\\t", i.priority)
                i = i.next
            print(self.Rear.value, "\\t", self.Rear.priority, "<== REAR")
        print("Queue Over")

Resources