priority queues, binary heaps - nd.edusemrich/ds17/22/notes.pdf · heap). brad sam michael erin...
TRANSCRIPT
CSE 20312 Priority Queues, Binary
Heaps
Motivation: Select 3 Longest Names
Find the 3 longest names. 1. Sort the names and select first/last 3
names.
2. Maintain a collection of 3 names, repeatedly insert a person and remove the shortest.
3. Use an ordered collection (binary heap).
BradSamMichaelErinAaronCameronRyanRoyceDanJorgeHappyShelbyBorah
Priority Queues
Priority Queue: Ordered Collections
Ordered collections support some sort of select operation: select(i): returnthe(i+1)thsmallestelement.
Priority Queue: Use Cases
Bandwidth Management
Scheduling
Search
Priority Queue: Implementations
Array If sorted: Insertion is O(n) Lookup/removal is O(1)
If unsorted: Insertion is O(1) Lookup/removal is O(n)
Linked List If sorted: Insertion is O(n) Lookup/removal is O(1)
If unsorted: Insertion is O(1) Lookup/removal is O(n)
No real difference in complexity for either array or linked list.
Priority Queue: STL
As with the stack and queue, in the C++ STL, a priority_queue is provided as a container adaptor:
priority_queue<int>pq;Note: Underlying container must have random access iterators
Sorting with Priority Queues
If we have a priority queue, we have a sorting algorithm:
Simply push all the elements into the queue and then pop them back out!
Sorting with Priority Queues
Different underlying data structure lead to different sorting algorithms:
Data Structure Resulting Algorithm
Unsorted Array Selection Sort
Sorted Array Insertion Sort
Binary Heap Heap Sort
Binary Heaps
Binary Heap: Rules
A heap is a binary tree where a less-than operator forms a strict weak ordering that can be used to compare the nodes’ entries. 1. Entry contained by a node is never less than the entries
of the node’s children
2. Tree is a complete binary tree
Because a heap is a complete tree, it is often implement with an array.
Binary Heap: Identification
Binary Heap: Push
1. Place new entry in the first location available (completeness).
2. While the new entry’s parent is less than the new entry, swap the new entry with its parent (ordering).
This is called reheapification upward.
Binary Heap: Push(45)
Binary Heap: Push(32)
Binary Heap: Pop
1. Save the top of the heap
2. Move the last entry to the root
3. While the out-of-place entry is less than one of its children, swap the out-of-place entry with its highest child
4. Return the saved top This is called reheapification downward.
Binary Heap: Pop
Binary Heap: Pop
Binary Heap: STL Implementation
We can use the make_heap STL function to heapify a sequence container: 1. Copy all the elements into the heap, in any order.
2. Then, working bottom-up, reheapify-down each node.
This is a O(n) process since different nodes only have to fall limited distances.
Heap Sort: Algorithm Convert container into a heap (heapify) While there are still items unsorted
Swap first element (max) with last unsorted Re-heapify-down the heap portion Decrement unsorted
Continually pop the heap to place items
towards the back
Heap Sort: Example Original: 54701
0thPass: 74501 //Heapifyoriginal1stPass: 54107 //Pop7&Re-heapifyDown2ndPass: 40157 //Pop5&Re-heapifyDown3rdPass: 10457 //Pop4&Re-heapifyDown4thPass: 01457 //Pop1&Re-heapifyDownObservations:Theheapisontheleft(sortedontheright)
Possiblylotsofswaps
Heap Sort: Implementation voidheap_sort(inta[],size_tn){
if(n<=1)return;make_heap(a,a+n);
for(size_tunsorted=n;unsorted>1;unsorted--){
pop_heap(a,a+unsorted);}
}
SourceCode
Heap Sort: Properties
Best case: O(nlogn)
Worst case:O(nlogn)
Not Stable
Animations