The VIP Line
Think of an Emergency Room. A patient with a minor cut might have arrived 2 hours ago (first in line), but a patient who just arrived with a severe injury gets treated immediately. The severe injury has higher priority.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Unlike a normal queue where FIFO rules absolute, a Priority Queue serves elements based on their assigned Priority. The element with the highest priority is served first, regardless of when it arrived.
Think of an Emergency Room. A patient with a minor cut might have arrived 2 hours ago (first in line), but a patient who just arrived with a severe injury gets treated immediately. The severe injury has higher priority.
While you could use an Array or Linked List and sort it, the mathematically optimal way to implement a Priority Queue is using a Heap data structure, which allows O(log N) insertions and O(log N) removals.
Priority Queues are the backbone of algorithms like Dijkstra's Shortest Path, Prim's Minimum Spanning Tree, CPU Task Scheduling, and Huffman Coding for data compression.