priority queues, binary heaps - nd.edusemrich/ds17/22/notes.pdf · heap). brad sam michael erin...

23
CSE 20312 Priority Queues, Binary Heaps

Upload: dinhdan

Post on 14-Jul-2019

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

CSE 20312 Priority Queues, Binary

Heaps

Page 2: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 3: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Priority Queues

Page 4: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Priority Queue: Ordered Collections

Ordered collections support some sort of select operation: select(i): returnthe(i+1)thsmallestelement.

Page 5: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Priority Queue: Use Cases

Bandwidth Management

Scheduling

Search

Page 6: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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.

Page 7: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 8: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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!

Page 9: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 10: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heaps

Page 11: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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.

Page 12: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heap: Identification

Page 13: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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.

Page 14: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heap: Push(45)

Page 15: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heap: Push(32)

Page 16: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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.

Page 17: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heap: Pop

Page 18: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Binary Heap: Pop

Page 19: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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.

Page 20: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 21: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 22: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

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

Page 23: Priority Queues, Binary Heaps - nd.edusemrich/ds17/22/notes.pdf · heap). Brad Sam Michael Erin Aaron Cameron Ryan Royce Dan Jorge Happy Shelby Borah Priority Queues . Priority Queue:

Heap Sort: Properties

Best case: O(nlogn)

Worst case:O(nlogn)

Not Stable

Animations