COMP 103 Priority Queues Partially Ordered Trees and




















- Slides: 20
COMP 103 Priority Queues, Partially Ordered Trees and Heaps
RECAP-TODAY 2 RECAP Clever stuff with linked structures linked list idea nice way to implement a Stack, or a Queue Then we saw trees, and then Binary Search Trees (BST) adding, and finding (significance of balance) removing is “interesting” BST idea nice way to implement a Set, Bag, or Map in-order traversals Tree. Sort TODAY: Priority queue (PQ) – variation on queue – “best” first partially ordered tree (POT) nice way to implement PQ oh and we’ll also get another fast sorting algorithm! Reading: Chapter 17 (section 17. 4 - 17. 5)
Queues and priority queues 3 Queues: First in, first out ⇒ Bob Jack Jim Oldest out first Ian Priority Queues: Highest priority (“best”) out first 3 4 1 2 Bob Jack Jim Ian Sometimes low number means high priority! Emergency room, 111 calls, job scheduling in factory, etc. . . Operating system process scheduling Graph/network algorithms: route planning, find shortest/cheapest path AI search algorithms
Implementing Priority Queues 4 Unsorted list (array or linked list): Fast to enqueue (“offer”): O(1) Slow to dequeue (“poll”): O(n) have to search for highest priority item. 1 2 3 4 5 6 Sorted list (array or linked list): Fast to dequeue (“poll”): O(1) Slow to enqueue (“offer”): O(n) 7 8 9 10 have to search for insertion point (and move items up). Array of “Buckets” of queues for each priority: Fast to enqueue and dequeue: O(1) Requires a small finite number of priorities. Best choice when applicable.
Implementing Priority Queues: preview. . . 5 So, what to do when we have unlimited priorities? Binary Search Tree: Keeps items in order, and allows easy insertion Fast to enqueue: O(log n) [if balanced] Fast to dequeue: O(log n) [if balanced] How? But, hard to keep balanced Partially Ordered Tree: Fast to enqueue (“offer”): O(log n) Fast to dequeue (“poll”): O(log n) Keeps itself balanced! Fast to construct from unordered list!
Partially Ordered Trees 6 Binary Search Tree: Binary tree All left subtree < parent, All right subtree ≥ parent. Partially Ordered Tree • Binary tree • Children ≤ parent, • Order of children not important Cat 19 Bee 35 Gnu 13 Fox 3 Jay 1 Eel 26 Dog 14 Ant 9 Hen 23 Cat 19 Bee 35 Dog 14 Jay 1 Eel 26 Fox 3 Gnu 13 Hen 23 Keep largest (or smallest) element at the root. Ant 9
Partially Ordered Tree: add 7 Easier to add and remove because the order is not complete. Add: Bee 35 insert at bottom rightmost “push up” to correct position. Cat 19 (swapping) Dog 14 Jay 1 Gnu 13 Eel 26 Fox 3 Kea 24 Hen 23 Ant 9
Partially Ordered Tree: remove 8 Easier to add and remove because the order is not complete. Add: insert at bottom rightmost “push up” to correct position. Bee 35 Kea 24 Remove: “pull up” largest child and recurse. But: makes tree unbalanced! Dog 14 Jay 1 Gnu 13 Eel 26 Cat 19 Fox 3 Hen 23 Ant 9
Partially Ordered Tree: remove I 9 Easier to add and remove because the order is not complete. Add: insert at bottom rightmost “push up” to correct position. Bee 35 Kea 24 Remove: “pull up” largest child of root and recurse on that subtree. But: makes tree unbalanced! Alternative: Dog 14 Jay 1 Gnu 13 replace root by bottom rightmost node “push down” to correct position (swapping) keeps tree balanced – and complete! Eel 26 Cat 19 Fox 3 Hen 25 Ant 9
Partially Ordered Tree: remove II 10 Easier to add and remove because the order is not complete. Add: insert at bottom right “push up” to correct position. Eel 26 Kea 24 Remove: “pull up” largest child and recurse. But: makes tree unbalanced! Alternative: Dog 14 Jay 1 replace root by bottom rightmost node “push down” to correct position keeps tree balanced – and complete! Gnu 13 Hen 23 Cat 19 Fox 3 Ant 9
Partially Ordered Tree 11 insert at bottom rightmost, swap with parent, … Bee 35 Remove: replace root with bottom rightmost, Cat swap with largest child, … 19 Add: But: How do you find the bottom right? Once you have found it, how do you find its parent to push it up? Dog 14 Jay 1 Gnu 13 Fox 3 Kea 24 We need a tree where you can quickly get to: the bottom right node, children from parent, parent from children. Eel 26 Hen 23 Ant 9
Heap: 12 A complete, partially ordered, binary tree complete = every level full, except bottom, where nodes are to the left Implemented in an array using breadth-first order Bee 35 Kea 19 Eel 26 Dog 14 Jay 1 Fox 7 Gnu 13 Hen 23 Ant 9 Cat 4 Bee 35 Kea 19 Eel 26 Dog 14 Fox 7 Hen 23 Ant 9 Jay 1 Gnu 13 Cat 4 0 1 2 3 4 5 6 7 8 9
Heap 13 We can compute the index of parent and children of a node: the children of node i are at (2 i+1) and (2 i+2) is at (i-1)/2 the parent of node i Bee 35 Kea 19 Eel 26 Dog 14 Fox 7 Hen 23 Ant 9 Jay 1 Gnu 13 Cat 4 0 1 2 3 4 5 6 7 8 9 Bee 35 Kea 19 Bottom right node is last element used. There are no gaps! Jay 1 Dog 14 Eel 26 Fox 7 Gnu 13 Cat 4 Hen 23 Ant 9
Heap: add 14 10 11 Bee 35 Kea 19 Eel 26 Dog 14 Fox 7 Hen 23 Ant 9 Jay 1 Gnu 13 Cat 4 Pig 21 0 1 2 3 4 5 6 7 8 9 10 Insert at bottom of tree and push up: Put new item at end: 10 Compare with parent: (10 -1)/2 = 4 ⇒ Fox/7 If larger than parent, swap Compare with parent: (4 -1)/2 = 1 ⇒ Kea/19 If larger than parent, swap Compare with parent: (1 -1)/2 = 0 ⇒ Bee/35 11 12
Heap: remove 15 10 11 Bee 35 Pig 21 Eel 26 Dog 14 Kea 19 Hen 23 Ant 9 Jay 1 Gnu 13 Cat 4 Fox 7 0 1 2 3 4 5 6 7 8 9 10 Remove item at 0: Move last item to 0 Find largest child 2 0+1 = 1, 2 0+2 = 2 If smaller than largest child, swap Find largest child 2 2+1 = 5, 2 2+2 = 6 If smaller than largest child, swap Find largest child 2 5+1 = 11 : No such child 11 12
Heap. Queue 16 public class Heap. Queue <E> extends Abstract. Queue <E> { private List<E> data = new Array. List<E>(); private Comparator<E> comp; public Heap. Queue (Comparator <E> c) { comp = c; } public boolean is. Empty() { return data. is. Empty(); } public int size () { return data. size(); } public E peek () { if (is. Empty()) return null; else return data. get(0); } Use Array. List, not array, so it handles resizing. Comparator must be designed so that it compares the priority values (not the items)
Heap. Queue: offer and poll 17 public boolean offer(E value) { if (value == null) return false; else { data. add(value); pushup(data. size()-1); return true; } } add at the end of the array public E poll() { if (is. Empty()) return null; if (data. size() == 1) return data. remove(0); else { E ans = data. get(0); data. set(0, data. remove(data. size()-1)); pushdown(0); return ans; } } move last element into root
Heap. Queue: pushup 18 private void pushup(int child) { if (child == 0) return; int parent = (child-1)/2; // compare with value at parent and swap if parent smaller if (compare(data. get(parent), data. get(child)) < 0) { swap(data, child, parent); recurse up the tree. . . pushup(parent); } Bee Kea Eel Dog Fox Hen Ant Jay Gnu Tui } 35 19 26 14 7 23 9 1 13 22 0 1 2 3 4 5 6 private void swap(List<E> data, int from, int to) data. set(child, data. set(parent, data. get(child))); 7 8 9
Heap. Queue: pushdown 19 private void pushdown(int parent) { int large. Ch = 2*parent+1; int other. Ch = large. Ch+1; // check if any children if (large. Ch >= data. size()) return; // find largest child if (other. Ch < data. size() && compare(data. get(large. Ch), data. get(other. Ch)) < 0 ) large. Ch = other. Ch; // compare with largest child, and swap if smaller if (compare(data. get(parent), data. get(large. Ch)) < 0) { swap(data, large. Ch, parent); pushdown(large. Ch); } Cat Kea Eel Dog Fox Hen Ant Jay Gnu } 4 19 26 14 7 23 9 1 13 recurse down the tree. . . 0 1 2 3 4 5 6 7 8 9
Heap. Queue: Analysis 20 Cost of offer: = cost of pushup = O(log(n)) log(n) comparisons, 2 log(n) assignments Cost of poll: = cost of pushdown = O(log(n)) 2 log(n) comparisons, 2 log(n) assignments Conclusion: Heap. Queue is always fast!!




















