Top Banner
CS171 Introduction to Computer Science II Priority Queues and Binary Heap Priority Queues and Binary Heap
30

CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Aug 30, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

CS171 Introduction to Computer Science II

Priority Queues and Binary HeapPriority Queues and Binary Heap

Page 2: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Review

• Binary Search Trees (BST)

• Balanced search trees

• Hash tables

Page 3: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 4: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 5: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Priority Queues

• Need to process/search an item with largest (smallest) key, but not necessarily full sorted order

• Support two operations• Support two operations

– Remove maximum (or minimum)

– Insert

• Similar to

– Stacks (remove newest)

– Queues (remove oldest)

Page 6: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Example

Page 7: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Applications

• Job scheduling

– Keys corresponds to priorities of the tasks

• Sorting algorithm

– Heapsort– Heapsort

• Graph algorithms

– Shortest path

• Statistics

– Maintain largest M values in a sequence

Page 8: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 9: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 10: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Possible implementations

• Sorting N items

– Time: NlogN

– Space: N

• Elementary PQ - Compare each new key • Elementary PQ - Compare each new key

against M largest seen so far

– Time: NM

– Space: M

• Using an efficient MaxPQ Implementation

Page 11: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 12: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Implementations

• Elementary representations

– Unordered array (lazy approach)

– ordered array (eager approach)

• Efficient implementation• Efficient implementation

– Binary heap structure

• Can we implement priority queue using Binary

Search Trees?

Page 13: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 14: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Sequence-based Priority Queue

• Implementation with an unsorted list

• Performance:

• Implementation with a sorted list

• Performance:

4 5 2 3 1 1 2 3 4 5

14

• Performance:– insert takes O(1) time since

we can insert the item at the beginning or end of the sequence

– removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key

• Performance:

– insert takes O(n) time since we have to find the place where to insert the item

– removeMin and min take O(1) time, since the smallest key is at the beginning

Page 15: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 16: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 17: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Binary Heap Tree

• A heap is a binary tree storing keys at its nodes and satisfying two properties:

– Heap-Order: for every internal node v other than the root,key(v) ≥ key(parent(v))

2

65

17

key(v) ≥ key(parent(v))

– Complete Binary Tree: let h be the height of the heap

• for i = 0, … , h − 1, there are 2i nodes of depth i

• at depth h − 1, the internal nodes are to the left of the external nodes

• The last node of a heap is the rightmost node of depth h-1

79

last node

Page 18: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 19: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Height of a Heap

• Theorem: A heap storing n keys has height O(log n)

Proof: (we apply the complete binary tree property)

– Let h be the height of a heap storing n keys

– Since there are 2i keys at depth i = 0, … , h − 1 and at least one key at

depth h, we have n ≥ 1 + 2 + 4 +… + 2h−1 + 1

– Thus, n ≥ 2h , i.e., h ≤ log n

19

– Thus, n ≥ 2h , i.e., h ≤ log n

1

2

2h−1

1

keys

0

1

h−1

h

depth

Page 20: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 21: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 22: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 23: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Insert/Remove and Maintaining Heap

order

• When a node’s key is larger than its parent key

– Upheap (promote, swim)

• When a node’s key becomes smaller than its

children’s keyschildren’s keys

– Downheap (demote, sink)

Page 24: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 25: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 26: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 27: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 28: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues

Demo

Page 29: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues
Page 30: CS171 Introduction to Computer Science II Priority Queues and …cheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Priority Queues