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

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

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

Page 1: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Priority Queues (Heaps)

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

Page 2: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 3: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 4: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 5: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 6: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Insertion

Insert 6 – Add key in next available position

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

Page 7: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Insertion

Begin Unheap

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

Page 8: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Insertion

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

Page 9: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 10: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Removal

Remove element from priority queues? removeMin( )

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

Page 11: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Removal

Begin downheap

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

Page 12: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Removal

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

Page 13: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Removal

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

Page 14: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 15: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 16: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

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

Page 17: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Merging Example

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

Page 18: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Example (contd.)

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

Page 19: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Example (contd.)

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

Page 20: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Example (contd.)

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

Page 21: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Bottom up Heap Construction

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

Page 22: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Bottom up Heap Construction

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

Page 23: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Bottom up Heap Construction

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

Page 24: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Bottom up Heap Construction

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

Page 25: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Bottom up Heap Construction

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

Page 26: Priority Queues (Heaps)ccf.ee.ntu.edu.tw/~yen/courses/ds18/chapter-6a.pdf · Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. ... A

Heap Sorting

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

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