Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods 1 2 Given : Stack A, Stack B 3 // based on requirement b will be reverse of a 4 add first(e) 5 { 6 A. push(e) ; 7 } 8 remove first(e) 9 { 10 A. pop() 11 } 12 add last(e) 13 { 14 B.push(e) ; 15 } 16 remove last () 17 { 18 B.pop() ; 19 } Running Time=constant Problems 1
Let T be a tree with n positions. Define the lowest common ancestor(lca) between two positions p and q as the lowest point in T that has both p and q as descendants. 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? Problems 2
Priority Queues and Readings - Chapter 9
Design ADT Question! Scheduler for simulated CPU Jobs " each job has a priority " from among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority " each job has a unique id and an attribute length that indicates the time required to process the job " once a job is scheduled, it runs for the required amount of time " when a job finishes we should remove it from our collection " jobs can be submitted to the scheduler at any point in time Prority Queues 4
Priority Queue ADT! A priority queue stores a collection of entries! Each entry is a pair (key, value)! Main methods of the Priority Queue ADT " insert(k, v) inserts an entry with key k and value v " removemin() removes and returns the entry with smallest key, or null if the the priority queue is empty! Additional methods " min() returns, but does not remove, an entry with smallest key, or null if the the priority queue is empty " size(), isempty() 2014 Goodrich, Tamassia, Goldwasser Prority Queues 5
Example! A sequence of priority queue methods: 2014 Goodrich, Tamassia, Goldwasser Prority Queues 6
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 " Comparability property: either x y or y x " Antisymmetric property: x y and y x x = y " Transitive property: x y and y z x z 2014 Goodrich, Tamassia, Goldwasser Prority Queues 7
Entry ADT! An entry in a priority queue is simply a keyvalue pair! Priority queues store entries to allow for efficient insertion and removal based on keys! Methods: " getkey: returns the key for this entry! As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { } K getkey(); V getvalue(); " getvalue: returns the value associated with this entry 2014 Goodrich, Tamassia, Goldwasser Prority Queues 8
Comparator ADT! A comparator encapsulates the action of comparing two objects according to a given total order relation! The comparator is external to the keys being compared! When the priority queue needs to compare two keys, it uses its comparator! Primary method of the Comparator ADT! compare(x, y): returns an integer i such that " i < 0 if a < b, " i = 0 if a = b " i > 0 if a > b " An error occurs if a and b cannot be compared. 2014 Goodrich, Tamassia, Goldwasser Prority Queues 9
Example Comparator! Lexicographic comparison of 2-D points: /** Comparator for 2D points under the standard lexicographic order. */ public class Lexicographic implements Comparator { int xa, ya, xb, yb; public int compare(object a, Object b) throws ClassCastException { xa = ((Point2D) a).getx(); ya = ((Point2D) a).gety(); xb = ((Point2D) b).getx(); yb = ((Point2D) b).gety(); if (xa!= xb) return (xb - xa); else return (yb - ya); } }! Point objects: /** Class representing a point in the plane with integer coordinates */ public class Point2D { protected int xc, yc; // coordinates public Point2D(int x, int y) { xc = x; yc = y; } public int getx() { return xc; } public int gety() { return yc; } } 2014 Goodrich, Tamassia, Goldwasser Prority Queues 10
Sequence-based Priority Queue! Implementation with an unsorted list! Implementation with a sorted list 4 5 2 3 1 1 2 3 4 5! 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 2014 Goodrich, Tamassia, Goldwasser Prority Queues 11
Better Implementation of Priority Queue?
2 5 6 9 7
! A heap is a binary tree storing keys at its nodes and satisfying the following properties:! Heap-Order: for every internal node v other than the root, key(v) key(parent(v))! Complete Binary Tree: let h be the height of the heap " for i = 0,, h 1, there are 2 i 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 maximum depth heap property 9 Structural property 5 7 2 6 last node 2014 Goodrich, Tamassia, Goldwasser 14
Example of non-! Heap property violation! Structural property violation 2 2 5 6 5 6 4 7 9 7 15
Finding the minimum element! The element with the smallest key value always sits at the root of the heap. " If it is elsewhere, it would have a parent with a larger key, thus violating the heap property. " hence finding the minimum can be done in O(1) time 16
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 2 i keys at depth i = 0,, h 1 and at least one key at depth h, we have n 1 + 2 + 4 + + 2 h 1 + 1 " Thus, n 2 h, i.e., h log n depth 0 1 h 1 h keys 1 2 2 h 1 1 2014 Goodrich, Tamassia, Goldwasser 17
Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n 9 2 5 6 7 2 5 6 9 7 0 1 2 3 4 18
and Priority Queues! We can use a heap to implement a priority queue! We store a (key, element) item at each internal node! We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 2014 Goodrich, Tamassia, Goldwasser 19
Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 20
Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 21
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 22
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 23
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 24
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 25
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 26
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 27
Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 28
Another View of Insertion! Enlarge heap! Consider path from root to inserted node! Find topmost element on this path with higher priority that of inserted element! Insert new element at this location by shifting down the other elements on the path (4,C) (2,T) (5,A) (6,Z) (5,A) (4,C) (15,K) (9,F) (7,Q) (20,B) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 29
Correctness of Upheap! The only nodes whose contents change are the ones on the path! Heap property may be violated only for children on these nodes! But new contents of these nodes only have lower priority! So heap property is not violated (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 30
Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (4, C) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 31
Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 32
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 33
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 34
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (13,W) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 35
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 36
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 37
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (11,S) 38
Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (13,W) (11,S) 39
Correctness of Downheap! Downheap traces a path down the tree! For a node on the path (say j) - both key(left(j)) and key(right(j)) > key(j)! All elements on path have lower key values than their siblings! All elements on this path are moved up.! Hence the heap property is not violated. (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 40
Run Time Analysis! heap of n nodes has height O(log n)! insertion - Upheap move the element all the way to the top " O(log n) steps in worst case! removal Downheap move the root element all the way to a leaf " O(log n) steps in worst case 41
Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n 9 2 5 6 7 2 5 6 9 7 0 1 2 3 4 42
Building a heap! Call Upheap procedure of the heap n times. " Upheap is O(log n) " inserting first element O(log 1) " inserting second element O(log 2) ".. " inserting the nth element O(log n) " so for the n insertions log 1+ log 2+ +log n = log n! = O(nlog n) 43
Merging Two! We are given two heaps and a key k! We create a new heap with the root node storing k and with the two heaps as subtrees 8 3 3 5 7 4 2 2 6! We perform Downheap to restore the heaporder property 8 5 2 4 6 3 4 8 5 7 6 2014 Goodrich, Tamassia, Goldwasser 44
Building a Heap Bottomup 16 15 4 12 6 7 23 20 45
Building a Heap Bottomup 25 9 11 17 16 15 4 12 6 7 23 20 46
Building a Heap Bottomup 15 4 6 17 16 25 9 12 11 7 23 20 47
Building a Heap Bottomup 5 8 15 4 6 17 16 25 9 12 11 7 23 20 48
Building a Heap Bottomup 4 6 15 5 7 17 16 25 9 12 11 8 23 20 49
Building a Heap Bottomup 14 4 6 15 5 7 17 16 25 9 12 11 8 23 20 50
Building a Heap Bottomup 4 5 6 15 9 7 17 16 25 14 12 11 8 23 20 51
Bottom-up Heap Construction! We can construct a heap storing n given keys in using a bottom-up construction with log n phases! In phase i, pairs of heaps with 2 i 1 keys are merged into heaps with 2 i+1 1 keys 2 i 1 2 i 1 2 i+1 1 2014 Goodrich, Tamassia, Goldwasser 52
Building a Heap Bottomup Analysis! Correctness: induction on i, all trees rooted at m > i are heaps! Running time: n calls to Downheap " Downheap O(log n) " so total running time O(nlog n)! We can provide a better bound O(n) " Idea for most of the time, Downheap works on smaller than n element heaps 53
Building a Heap Bottomup Analysis (2)! height of node: length of longest path from node to leaf! height of tree: height of root height h h-1 1 0 keys 1 2 2 h 1 2 h 54
Building a Heap Bottomup Analysis (3)! height of node: length of longest path from node to leaf! height of tree: height of root! time for Downheap(i) = O(height of subtree rooted at i)! Let us assume a complete binary tree " n = 2 h+1-1 55
Building a Heap Bottomup Analysis (4) 3 Total work for B 3*1 2 2 2*2 1 1 1 1 1*4 0 0 0 0 0 0 0 0 0*8 56
Building a Heap Bottomup Analysis (5)! 0 th level - 2 h nodes, no need to call Downheap! 1 st level - 2 h-1 nodes, Downheap - 1 swap! 2 nd level - 2 h-2 nodes, Downheap - 2 swaps! j th level - 2 h-j nodes, Downheap - j swaps! h th level 1 node, Downheap h swaps! So total number of swaps is h h j2 h j = j 2h j=0 j=0 " It can be shown that, this is bound by 2 (h+1) " 2 (h+1) = n-1! Therefore O(n) h = 2 h j 2 j 2 j j=0 57