Priority Queues (Today: inary Min Heaps) hapter in Weiss SE Data Structures Ruth Anderson Winter 0 //0 Today s Outline Announcements Project #, due pm Wed Jan. Written Assignment # posted, due at the beginning of class Friday Jan. Today s Topics: Asymptotic Analysis Priority Queues inary Min Heap //0 The One Page heat Sheet alculating series: e.g. n n( n + ) i = i=. rute force (Section..). Induction (Section..). Memorize simple ones! Solving recurrences: e.g. T(n) = T(n/) +. Expansion (example in class). Induction (Section..). Telescoping (later ) eneral proofs (Section..) e.g. How many edges in a tree with n nodes?. ounterexample. Induction. ontradiction //0 Simplifying Recurrences iven a recursive equation for the running time, can sometimes simplify it for analysis. For an upper-bound analysis, can optionally simplify to something larger, e.g. T(n) = T(floor(n/)) + to T(n) T(n/) + For a lower-bound analysis, can optionally simplify to something smaller, e.g. T(n) = T(n/ + ) + to T(n) T(n/) + //0 O( f(n) ) is a set of functions n + Set Notation O( n ) 0n log n n - n.00 n + n. n - 0 O( n ) Set Notation n - n.00 n + n So we say both Set notation allows us to 0n log n = O( n ) and formalize our intuition //0 0n log n O( n ) //0 O( n ) O( n ) n + O( n ) 0n log n n + n 00 n log n
Processor Scheduling Priority Queue ADT heckout line at the supermarket??? Printer queues??? operations: insert, deletemin insert deletemin //0 //0 Priority Queue ADT. PQueue data : collection of data with priority. PQueue operations insert deletemin (also: create, destroy, is_empty). PQueue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y //0 Applications of the Priority Q Select print jobs in order of decreasing length Forward packets on network routers in order of urgency Select most frequent symbols for compression Sort numbers, picking minimum first Anything greedy //0 Implementations of Priority Queue ADT insert deletemin Unsorted list (Array) Unsorted list (Linked-List) Sorted list (Array) Sorted list (Linked-List) inary Search Tree (ST) root(t): leaves(t): children(): parent(h): siblings(e): ancestors(f): descendents(): subtree(): Tree Review A D E F H J K L Tree T I M N //0 //0
depth(t): More Tree Terminology A Tree T Some More Tree Terminology T is binary if A Tree T height(): degree(): branching factor(t): D E F H J K L M N I T is n-ary if T is complete if D H I E J F //0 How deep is a complete tree with n nodes? //0 rief interlude: Some Definitions: A Perfect binary tree A binary tree with all leaf nodes at the same depth. All internal nodes have children. height h h+ nodes h non-leaves h leaves 0 Full inary Tree A binary tree in which each node has exactly zero or two children. (also known as a proper binary tree) (we will use this later for Huffman trees) //0 //0 inary Heap Properties. Structure Property. Ordering Property Heap Structure Property A binary heap is a complete binary tree. omplete binary tree binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: //0 //0
Representing omplete inary Trees in an Array Why better than tree with pointers? A D E F H I J K L From node i: left child: right child: parent: implicit (array) implementation: 0 A D E F H I J K L //0 //0 0 Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. Heap Operations findmin: insert(val): percolate up. deletemin: percolate down. 0 0 not a heap 0 0 0 0 0 00 0 0 0 00 0 0 0 //0 //0 Heap Insert(val) asic Idea:. Put val at next leaf position. Repeatedly exchange node with its parent if needed Insert pseudo ode (optimized) void insert(object o) { assert(!isfull()); size++; newpos = percolateup(size,o); Heap[newPos] = o; runtime: int percolateup(int hole, Object val) { while (hole > && val < Heap[hole/]) Heap[hole] = Heap[hole/]; hole /= ; return hole; (Java code in book) //0 //0
Insert: percolate up 0 0 Heap Deletemin 0 0 00 0 0 00 0 0 0 0 asic Idea:. Remove root (that is always the min!). Put last leaf node at root. Find smallest child of node. Swap node with its smallest child if needed.. Repeat steps & until no swaps needed. //0 //0 DeleteMin pseudo ode (Optimized) Object deletemin() { assert(!isempty()); returnval = Heap[]; size--; newpos = percolatedown(, Heap[size+]); Heap[newPos] = Heap[size + ]; return returnval; runtime: (Java code in book) int percolatedown(int hole, Object val) { while (*hole <= size) { left = *hole; right = left + ; if (right size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; else break; return hole; //0 DeleteMin: percolate down 0 0 0 0 00 0 0 0 0 00 //0 Insert:,,,,,, 0 Other Priority Queue Operations decreasekey given a pointer to an object in the queue, reduce its priority value Solution: change priority and increasekey given a pointer to an object in the queue, increase its priority value Solution: change priority and Why do we need a pointer? Why not simply data value? //0 //0 0
Other Heap Operations decreasekey(objptr, amount): raise the priority of a object, percolate up increasekey(objptr, amount): lower the priority of a object, percolate down remove(objptr): remove a object, move to top, them delete. ) decreasekey(objptr, ) ) deletemin() Worst case Running time for all of these: FindMax? ExpandHeap when heap fills, copy into new space. inary Min Heaps (summary) insert: percolate up. Θ(log N) time. deletemin: percolate down. Θ(log N) time. uild Heap? //0 //0 uildheap: Floyd s Method Add elements arbitrarily to form a complete tree. Pretend it s a heap and fix the heap-order property! uildheap pseudocode private void buildheap() { for ( int i = currentsize/; i > 0; i-- ) percolatedown( i ); runtime: //0 //0 uildheap: Floyd s Method Finally runtime: //0 //0