cs171 introduction to computer science ii priority queues and...
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/3.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/4.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/8.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/9.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/11.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/13.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/15.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/16.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/18.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/20.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/21.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/22.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/24.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/25.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/26.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/27.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/29.jpg)
![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](https://reader036.vdocuments.us/reader036/viewer/2022071609/614804afa830d0442101cfa6/html5/thumbnails/30.jpg)