CS 1501
www.cs.pitt.edu/~nlf4/cs1501/
CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned - - PowerPoint PPT Presentation
CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned priority queues in building Huffman tries Primary operations they needed: Insert Find item with highest priority E.g., findMin() or findMax() Remove
www.cs.pitt.edu/~nlf4/cs1501/
○ Insert ○ Find item with highest priority ■ E.g., findMin() or findMax() ○ Remove an item with highest priority ■ E.g., removeMin() or removeMax()
○ Simplest approach: arrays
We mentioned priority queues in building Huffman tries
2
○ Add new item to the end of the array ○ Θ(1)
○ Search for the highest priority item (e.g., min or max) ○ Θ(n)
○ Search for the highest priority item and delete ○ Θ(n)
3
○ Add new item in appropriate sorted order ○ Θ(n)
○ Return the item at the end of the array ○ Θ(1)
○ Return and delete the item at the end of the array ○ Θ(1)
4
○ Insert ■ Average case of Θ(lg n), but worst case of Θ(n) ○ Find ■ Average case of Θ(lg n), but worst case of Θ(n) ○ Remove ■ Average case of Θ(lg n), but worst case of Θ(n)
○ No constant time operations ○ Worst case is Θ(n) for all operations
5
priority item, not to find/remove any item
○ Can we take advantage of this to improve our runtime? ■ Yes!
6
the tree:
○ T.item is of a higher priority than T.right_child.item ○ T.item is of a higher priority than T.left_child.item
T.right_child.item
○ This is a relaxation of the approach needed by a BST
7
○ Simply the root of the tree ■ Θ(1)
○ The tree is modified and the heap property must be maintained
8
heap property
9
7 42 37 5 5 42 7 5 8 15 15 37 12 15 12 9 9 42 3 3 9 3 7 3 5 Insert: 7, 42, 37, 5, 8, 15, 12, 9, 3
10
○ So let's simply overwrite the root with the item from the last leaf and delete the last leaf ■ But then the root is violating the heap property…
heap property
11
8 37 15 12 42 9 7 3 5 9 9 5 9 8
7 9 42 7 42 8 42
12
○ Θ(1)
○ Height of a complete binary tree is lg n ○ At most, upheap and downheap operations traverse the height of the tree ○ Hence, insert and remove are Θ(lg n)
13
○ This requires overhead for dynamic node allocation ○ Also must follow chains of parent/child relations to traverse the tree
○ We can easily represent a complete binary tree using an array
14
○ parent(i) = ⌊(i - 1) / 2⌋ ○ left_child(i) = 2i + 1 ○ right_child(i) = 2i + 2
15
○ MAX heap to sort ascending ○ MIN heap to sort descending
○ Don’t actually delete the leaf node
3 5 12 7 8 37 15 42 9 3 9 9 5 9 7 5 42 7 42 8 42 7 15 15 8 9 15 37 8 37 9 37 15 42 9 12 42 37 12 37 15 42 15 37 42 42 37
16
○ Worst case: ■ n log n
○ Yes
○ No
17
build a heap to help me keep track of the lowest price available from different stores.
class CardPrice implements Comparable<CardPrice>{ public String store; public double price; public CardPrice(String s, double p) { … } public int compareTo(CardPrice o) { if (price < o.price) { return -1; } else if (price > o.price) { return 1; } else { return 0; } } }
18
○ What is the runtime to find an arbitrary item in a heap? ■ Θ(n) ■ Hence, updating an item in the heap is Θ(n) ○ Can we improve on this? ■ Back the PQ with something other than a heap? ■ Develop a clever workaround?
19
item’s current position in the heap
20
"NE":0
n a x b Indirection "AMZN":1 "NCIX":2 "BB":3
n x "NE":2 "NCIX":0
a x "AMZN":0 "NCIX":1
x b "BB":1 "NCIX":3 "AMZN":1 "BB":0 a b
21