© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Priority Queues © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Assignment #8 R-8.20 Draw a binary tree T that simultaneously satisfies the following: Each internal node of T stores a single character. A preorder traversal of T yields EXAMFUN. An inorder traversal of T yields MAFXUEN.
C-8.50 Design algorithms for the following operations for a binary tree T: preorder_next(p): Return the position visited after p in a preorder traversal of T (or None if p is the last node visited). inorder_next(p): Return the position visited after p in an inorder traversal of T (or None if p is the last node visited). postorder_next(p): Return the position visited after p in a ostorder traversal of T (or None if p is the last node visited). What are the worst-case running times of your algorithms? The worst-case running times for these algorithms are all O(h) where h is the height of the tree T.
preorder_next(p) Algorithm preorder next(v): if v is internal then return v’s left child else p = parent of v if v is left child of p then return right child of p while v is not left child of p do v = p p = p.parent
inorder_next(p) Algorithm inorder next(v): if v is internal then p = right child of v while p has left child do p = left child of p return p else p = parent of v if v is left child of p then while v is not left child of p do v = p p = p.parent
postorder_next(p) Algorithm postorder next(v): if v is internal then p = parent of v if v is right child of p then return p else v = right child of p while v is not external do v = left child of v return v if v is left child of p then return right child of p
C-8. 58 Let T be a tree with n positions C-8.58 Let T be a tree with n positions. Define the lowest common ancestor (LCA) between two positions p and q as the lowest position in T that has both p and q as descendants (where we allow a position to be a descendant of itself ). Given two positions p and q, describe an efficient algorithm for finding the LCA of p and q. What is the running time of your algorithm?
lowest common ancestor (LCA) Algorithm LCA(v, w): int vdepth = v.depth int wdepth = w.depth while vdepth > wdepth do v = v.parent while wdepth > vdepth do w = w.parent while v != w do return v
This choice may be influenced by factors such as each plane’s an air-traffic control center that has to decide which flight to clear for landing This choice may be influenced by factors such as each plane’s distance from the runway, time spent waiting in a holding pattern, or amount of remaining fuel. It is unlikely that the landing decisions are based purely on a FIFO policy.
© 2013 Goodrich, Tamassia, Goldwasser Priority Queue ADT A priority queue stores a collection of items Each item is a pair (key, value) Main methods of the Priority Queue ADT add (k, x) inserts an item with key k and value x remove_min() removes and returns the item with smallest key Additional methods min() returns, but does not remove, an item with smallest key len(P), is_empty() Applications: Standby flyers Auctions Stock market © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Priority Queue Example © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser Total Order Relations Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct entries in a priority queue can have the same key Mathematical concept of total order relation Reflexive property: x x Antisymmetric property: x y y x x = y Transitive property: x y y z x z © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Composition Design Pattern An item in a priority queue is simply a key-value pair Priority queues store items to allow for efficient insertion and removal based on keys © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Sequence-based Priority Queue Implementation with an unsorted list Performance: add takes O(1) time since we can insert the item at the beginning or end of the sequence Remove_min and min take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance: add takes O(n) time since we have to find the place where to insert the item remove_min and min take O(1) time, since the smallest key is at the beginning 4 5 2 3 1 1 2 3 4 5 © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Unsorted List Implementation © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Sorted List Implementation © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Priority Queue Sorting We can use a priority queue to sort a set of comparable elements Insert the elements one by one with a series of add operations Remove the elements in sorted order with a series of remove_min operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P priority queue with comparator C while S.is_empty () e S.remove_first () P.add (e, ) while P.is_empty() e P.removeMin().key() S.add_last(e) © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to 1 + 2 + …+ n Selection-sort runs in O(n2) time the bottleneck computation is the repeated “selection” of the minimum element in Phase 2. For this reason, this algorithm is better known as selection-sort. (See Figure 9.7.) © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort: Inserting the elements into the priority queue with n insert operations takes time proportional to 1 + 2 + …+ n Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Insertion-sort runs in O(n2) time © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) () © 2013 Goodrich, Tamassia, Goldwasser Priority Queues
In-place Insertion-Sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertion-sort We keep sorted the initial portion of the sequence We can use swaps instead of modifying the sequence 5 4 2 3 1 5 4 2 3 1 4 5 2 3 1 2 4 5 3 1 2 3 4 5 1 1 2 3 4 5 1 2 3 4 5 © 2013 Goodrich, Tamassia, Goldwasser Priority Queues