10. Priority Queues
Description: A Priority Queue is a data structure where each element has an associated priority. Elements with higher priority are dequeued before elements with lower priority, regardless of their insertion order.
The main difference between a priority queue and a standard queue (which processes elements in First-In, First-Out / FIFO order) is that element retrieval in a priority queue is determined strictly by priority rather than insertion sequence.
Key Operations
- Enqueue: Adds an element to the priority queue with a specified priority level.
- Dequeue: Removes and returns the element with the highest priority.
- Peek: Views the element with the highest priority without removing it.
- Change Priority: Updates the priority level of an existing element in the queue.
Implementation Approaches
Priority queues can be implemented using various data structures:
- Array or List: Elements are stored in a linear array or list. If kept sorted on every insertion, the retrieval operation takes O(1) time, but insertion takes O(n) time.
- Heap: Typically implemented using a binary heap (Min-Heap or Max-Heap). Elements are structured such that extracting the highest-priority element takes O(log n) time, and inserting a new element also takes O(log n) time.
- Binary Search Tree (BST): Can be used to maintain elements ordered by priority, but heaps are generally preferred due to lower overhead and simpler structure.
Time Complexities (Heap-Based)
- Enqueue (Insertion): O(log n) — Inserting an element requires restoring the heap property (heapify-up).
- Dequeue (Extraction): O(log n) — Extracting the root (maximum or minimum value) requires swapping with the last node and restructuring the heap (heapify-down).
- Peek: O(1) — The element with the highest priority is always located at the root of the heap.
- Change Priority: O(log n) — Updating an element's priority may require locating, removing, or adjusting its position within the heap.
Types of Priority Queues
- Max-Priority Queue (Max-Heap): The element with the highest numerical priority value is kept at the root and retrieved first. Used when tasks with the largest values require immediate processing.
- Min-Priority Queue (Min-Heap): The element with the lowest numerical priority value resides at the root. Frequently used in shortest-path algorithms like Dijkstra's algorithm.
- Sorted Queue: Elements are maintained in a strictly ordered list, making retrieval O(1) at the expense of slower O(n) insertions.
Applications
- Dijkstra’s Algorithm: Priority queues track vertices ordered by their tentative distance from the source vertex to find the shortest path in non-negatively weighted graphs.
- Operating System Task Scheduling: Manages process execution queues where high-priority processes preempt lower-priority ones.
- Real-Time Data Processing: Used in event-driven systems where critical events or queries must be handled before routine background tasks.
Advantages
- Fast processing of highest-priority elements.
- Well-suited for dynamic environments where element priorities change during execution.
Disadvantages
- Insertion and deletion operations carry higher overhead compared to standard FIFO queues.
- Modifying element priorities often requires repositioning nodes, which can impact performance if executed frequently.
Priority queues are a powerful tool for optimizing problems where operation execution order depends on relative importance or urgency.
All rights remain with the original author (Archiveskk). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me