Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue:...

Post on 14-Jul-2019

218 views 0 download

Transcript of Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue:...

Priority Queues (Heaps)

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 1 / 26

Priority Queue: Motivating Example

3 jobs have been submitted to a printer in the order A, B, C.I Job A -100 pagesI Job B - 10 pagesI Job C - 1 page

Average waiting time with FIFO service:(100+110+111) / 3 = 107 time unitsAverage waiting time for shortest-job-first service:(1+11+111) / 3 = 41 time unitsA queue be capable to insert and deletemin?

Priority Queue

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 2 / 26

Heaps

A heap is a binary tree T that stores a key-element pairs at itsinternal nodesIt satisfies two properties:

I MinHeap: key(parent) ≤ key(child)OR MaxHeap: key(parent) ≥ key(child)

I all levels are full, except the last one, which is left-filled, i.e., acomplete binary tree.

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 3 / 26

What are Heaps Useful for?

To implement priority queuesPriority queue = a queue where all elements have a ”priority”associated with themRemove in a priority queue removes the element with the smallestpriority

I insertI removeMin

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 4 / 26

Heap or Not a Heap?

A heap T storing n keys has height h = blog(n + 1)c, which isO(logn)

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 5 / 26

Heap Insertion

Insert 6 – Add key in next available position

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 6 / 26

Heap Insertion

Begin Unheap

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 7 / 26

Heap Insertion

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 8 / 26

Heap Insertion

Terminate unheap whenI reach rootI key child is greater than key parent

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 9 / 26

Heap Removal

Remove element from priority queues? removeMin( )

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 10 / 26

Heap Removal

Begin downheap

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 11 / 26

Heap Removal

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 12 / 26

Heap Removal

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 13 / 26

Heap Removal

Terminate downheap whenI reach leaf levelI key child is greater than key parent

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 14 / 26

Bottom-up Heap Construction

We can construct a heap storing n given keys using a bottom-upconstruction with logn phasesIn phase i, pairs of heaps with 2i − 1 keys are merged into heapswith 2i+1 − 1 keys

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 15 / 26

Merging Two Heaps

We are given two twoheaps and a key kWe create a new heapwith the root nodestoring k and with thetwo heaps as subtreesWe perform heapDownto restore the heap-orderproperty

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 16 / 26

Merging Example

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 17 / 26

Example (contd.)

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 18 / 26

Example (contd.)

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 19 / 26

Example (contd.)

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 20 / 26

Bottom up Heap Construction

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 21 / 26

Bottom up Heap Construction

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 22 / 26

Bottom up Heap Construction

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 23 / 26

Bottom up Heap Construction

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 24 / 26

Bottom up Heap Construction

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 25 / 26

Heap Sorting

Step 1: Build a heapStep 2: removeMin( )Running time?

(Priority Queues (Heaps)) Data Structures and Programming Spring 2018 26 / 26