Priority Queue (ADT)

Context: FIT1008_MOC · a Queue (ADT) ordered by importance, not arrival · best implemented by a Heap · contrast linear impls

Quick Revision

  • 🎯 Objective: removal returns the highest-priority element ➔ FIFO Queue (ADT) is the special case where priority = waiting time.
  • 📦 Core Components: addget_maxdecrease_key (for graph algorithms).
  • ⚡ Key Constraint: every linear impl is stuck with one op; only a Heap / balanced tree makes both .

📝 Core

1. The Priority Queue (Priority-Ordered Removal)

  • Mechanism ➔ each element has a priority; removal returns the highest (or lowest — dual).
  • Core opsadd(element) and get_max() (remove-and-return the top).
  • No general search ➔ elements comparable by priority; only the extreme is cheap.

2. Why a Heap (and decrease_key)

  • Both fast ➔ linear structures trade add vs get_max; a Heap makes both via partial (heap) order.
  • decrease_key ➔ raise an element’s priority ➔ used by Dijkstra/Prim.
  • Heap choice ➔ binary heap: decrease-key → Dijkstra ; Fibonacci heap: amortised → (optimal); -ary heap tunes branching.

⚙️ Core Implementation

🔹 The interface

⚖️ Core Decision Matrix

Implementationget_max()adddecrease_key
Unsorted array / LinkList
Sorted array / list
Balanced BST (Binary Tree)
Binary Heap
Fibonacci heap amort. amort. amort.

(all × .)

When It Flips: linear structures trade add vs get_max; only non-linear structures (heap, balanced tree) make both fast. Applications: Dijkstra/Prim, event-driven simulation, Huffman coding, OS scheduling, A* search.

📊 Exam Execution Trace

Applied Exercise

Problem: Show why a sorted list can’t replace a heap. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: the heap balances both ops at ; the sorted list is stuck with one operation.

🧠 Active Recall