seen or taken out of the queue first Operations: enqueue: add an element dequeue: get or remove the highest priority element"> seen or taken out of the queue first Operations: enqueue: add an element dequeue: get or remove the highest priority element">
Download presentation
Presentation is loading. Please wait.
1
Priority Queues & Heaps
2
Prioritization Problems
Print jobs: want printers to complete faculty print jobs before student jobs. May not be "first come, first served"; some have higher priority. ER scheduling: Patients are seen in the ER based on how urgent their case is. Highest priority -> seen or taken out of the queue first Operations: enqueue: add an element dequeue: get or remove the highest priority element
3
Priority Queue ADT priority queue: an ordered collection of elements that provides fast access to highest priority element enqueue: add element with specified priority peek: return highest-priority element dequeue: remove/return highest-priority element Specify priority/urgency/importance level when you add element Can choose whether larger or smaller numbers indicate higher priority Example: PriorityQueue<string> faculty; faculty.enqueue("Julien", 2); faculty.enqueue("Eberlein", 4); // low priority faculty.enqueue("Tewfik", 1); // high priority
4
PriorityQueue Implementation I: array or vector (non-optimal implementation)
Several ways to implement PQ: here we consider unsorted array enqueue by adding to end dequeue by removing highest-priority element With this implementation: what is big-O or enqueue? dequeue? peek? value t n a m 5 4 8 1 priority size = 4 capacity = 8 enqueue O(1) – adding to end dequeue O(N) – not sorted, pull element out after you find, then shift elements
5
PriorityQueue Implementation: sorted array
enqueue: add to proper place to preserve sorted order dequeue the first element Big-O of enqueue? dequeue? peek? Could also try a linked list implementation – enqueue at front Or a sorted linked list value t b m a r 2 4 5 7 priority
6
Heap Data Structure heap: tree-based data structure with every level full except the deepest, which has leaves filled left to right, and satisfies the heap property heap property: If P is a parent node of C, then the key (or value) of P is less than or equal to the key of C. (Min heap)
7
Heap Data Structure Node at top of heap is root
Provides efficient implementation of Priority Queue ADT Commonly a binary heap (in which the tree is a binary tree) is used In a heap, the highest priority element is always stored in the root Always remove the highest priority element from a priority queue Heap not sorted Common and space efficient implementation: array or vector array[1] contains the root, and next two elements contain its children, the next four elements contain the grandchildren of the root... Children of node at index n are at index 2n and 2n+1
8
Heaps: implementation
heap: stored in an array with root at 1 Index i has parent index of i/2 Index is has children at indexes 2i and 2i+1 Must reinstate the heap property after adding or deleting Parent has higher priority than children Here we associate lower key with higher priority (min heap) index t d m a r 2 5 4 7
9
add to Heap: enqueue pq.enqueue("k", 1);
index pq.enqueue("k", 1); Add to heap in the first available index – then heap property likely violated Swap new element with its parent until it's in order ("bubbling/sifting up") Big-O of this bubbling up process? value t d m a r k 2 5 4 7 1 priority size = 6 capacity = 7
10
add to Heap: enqueue Bubble up "k":1
index Bubble up "k":1 index 6 swaps up with parent index 3 ("m":4) index 3 ("k":1) swaps with index 1 ("t":2) Keep swapping with parent until heap property satisfied, i.e., until new entry's value is >= its parent's value value k d t a r m 1 5 2 7 4 priority size = 6 capacity = 7
11
remove from Heap: dequeue
index pq.dequeue() // remove "k", the highest priority item To remove the min-priority element from a heap: Move last element up to root (index 1) Repeatedly swap it downward with its most-urgent child until in order Bubbling/percolating down Big-O? value k d t a r m y 1 2 4 7 5 8 6 priority size = 7 capacity = 7
12
remove from Heap: dequeue
index swap index 1 with most urgent child at index 2 ("d":2) swap index 2 ("y":6) with most urgent child at index 5 ("r":5) value y d t a r m 6 2 4 7 5 8 priority size = 6 capacity = 7 index value d r t a y m 2 5 4 7 6 8 priority size = 6 capacity = 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.