Dates • Midterm Friday! CSE 326 Data Structures • Project 2 due next Wednesday Midterm Review • Homework 4 – Hmmmm….. – We ought to talk about this…. Hal Perkins Spring 2007 Logistics Material Covered • Closed Notes • Everything we’ve talked/read in class up to AVL trees • Closed Book – And for AVL trees, up to inserting and • Open Mind rotations, but not implementations in Java • You may bring a calculator, though don’t even think about loading it with notes or programs. And you probably won’t find it of much use anyway. 1
Back to our two functions f and g from before Material Not Covered Order Notation: Definition O( f( n ) ) : a set or class of functions • We won’t make you write syntactically correct Java code (pseudocode okay) g( n ) ∈ O( f( n ) ) iff there exist consts c and n 0 • We won’t make you do a super hard proof such that: c f(n) • We won’t test you on the details of g( n ) ≤ c f( n ) for all n ≥ n 0 g(n) generics, interfaces, etc. in Java n – But you should know the basic ideas since we Example: g( n ) =1000 n vs. f( n ) = n 2 n 0 spent a lecture on them and had to deal with Is g( n ) ∈ O( f( n ) ) ? 1000 n ≤ 1 * n 2 them in project 2A Pick: n0 = 1000, c = 1 for all n ≥ 1000 So g( n ) ∈ O( f( n ) ) Definition of Order Notation Log? log k n ∈ O(log 2 n)? • Upper bound: T(n) = O(f(n)) Big-O log k n=log 2 n/log 2 k Exist constants c and n’ such that T(n) ≤ c f(n) for all n ≥ n’ Lower bound: T(n) = Ω (g(n)) • Omega Exist constants c and n’ such that log 2 n 2 ∈ O(log 2 n)? T(n) ≥ c g(n) for all n ≥ n’ log 2 n 2 =2log 2 n Tight bound: T(n) = θ (f(n)) • Theta When both hold: T(n) = O(f(n)) T(n) = Ω (f(n)) 2
Implementations of Priority Queue ADT Priority Queue ADT insert deleteMin • Checkout line at the supermarket ??? O(1)/ O(N)worst-array full, O(N) – to find value Unsorted list (Array) • Printer queues ??? should say WHY, might reject on full instead. • operations: insert, deleteMin Unsorted list (Linked-List) O(N) – to find value O(1) O(1) to find val, but O(log N) to find loc w. O(N) to move vals, (or Sorted list (Array) Bin search, but O(N) O(1) if in reverse order) to move vals O(N) to find loc, O(1) Sorted list (Linked-List) O(1) to do the insert 6 2 15 23 Binary Search Tree (BST) O(N) O(N) 12 18 insert deleteMin Plus – 45 3 7 good O(log N) O(log N) Binary Heap memory Binary Heap close to O(1) usage 1.67 levels on average Heap Structure Property Tree Review • A binary heap is a complete binary tree. Tree T Complete binary tree – binary tree that is A completely filled, with the possible exception A root ( T ) : of the bottom level, which is filled left to right. leaves ( T ) : DEFJ..NI B C Examples : Since they have children (B) : this regular D E F G parent (H) : structure property, siblings (E) : we can take H I advantage of that Its parent or parent’s ancestor ancestors (F) : to store them in a descendents (G) : Its child or child’s descendent compact manner. J K L M N subtree (C) : Itself plus all descendents 3
Representing Complete Heap Order Property Binary Trees in an Array Heap order property : For every non-root node X, the value in the parent of X is 1 A From node i : less than (or equal to) the value in X. 2 3 B C This is the order for a MIN heap – could do the 2 * i left child: 7 6 same for a max heap. 4 5 F G D E 10 right child: (2 * i)+1 10 8 9 10 11 12 H I J K L parent: 20 80 └ i / 2 ┘ 20 80 40 60 85 99 30 15 implicit (array) implementation: 50 700 A B C D E F G H I J K L not a heap This is a PARTIAL order (diff than BST) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 For each node, its value is less than all of its descendants (no distinction between left and right) Heap Operations Insert: percolate up How? 10 Now insert 90. • findMin: (no swaps, even 20 80 though 99 is • insert(val): percolate up. Is the tree unique? larger!) 40 60 85 99 Swap 85 and 99. • deleteMin: percolate down. Swap 700 and 85? Now insert 7. 50 700 65 15 10 Optimization, 10 bubble up an 20 80 empty space 15 80 40 60 85 99 to reduce # of swaps 40 20 85 99 50 700 65 60 50 700 65 4
DeleteMin: percolate down BuildHeap: Floyd’s Method Max # of 10 11 12 0 1 2 3 10 exchanges? = O(log N), 12 5 11 3 10 6 9 4 8 1 7 2 20 15 There is a good Add elements arbitrarily to form a complete tree. chance goes to 40 60 85 99 Pretend it’s a heap and fix the heap-order property! bottom (started at bottom) vs. insert 50 700 65 12 - Could also use Red nodes need to the percolate 15 percolate down empty bubble 5 11 down 20 65 40 60 85 99 3 10 6 9 50 700 4 8 1 7 2 A Solution: d -Heaps How does height Operations on d -Heap compare to bin heap? (less) • Each node has d children • Insert : runtime = depth of tree • Still representible by array 1 decreases, O(log d n) worst • Good choices for d : 3 7 2 – (choose a power of two percolateDown for efficiency) requires comparison • deleteMin: runtime = 4 8 5 12 11 10 6 9 to find min, – fit one set of children in O(d log d n), worst/ave a cache line 12 1 3 7 2 4 8 5 121110 6 9 – fit one set of children on a memory page/disk Does this help insert or deleteMin more? block 5
Definition: Null Path Length Leftist Heap Properties null path length (npl) of a node x = the number of nodes between x • Heap-order property and a null in its subtree – parent’s priority value is ≤ to childrens’ priority OR values npl(x) = min distance to a descendant with 0 or 1 children – result: minimum element is at the root 2 ? • npl (null) = -1 1 • npl (leaf) = 0 1 • Leftist property ? ? • npl (single-child node) = 0 – For every node x , npl (left( x )) ≥ npl (right( x )) 0 0 1 ? 0 Equivalent definitions: – result: tree is at least as “heavy” on the left as the right 1. npl(x) is the height of largest Are leftist trees… 0 0 0 complete subtree rooted at x complete? No, 2. npl(x) = 1 + min{ npl (left(x)), npl (right(x))} balanced? no Merging Two Leftist Heaps Leftist Merge Continued • merge(T 1 ,T 2 ) returns one leftist heap Swap L and R if needed containing all elements of the two a a (distinct) leftist heaps T 1 and T 2 If npl (R ’ ) > npl (L 1 ) done? Leftist property? merge npl (left( x )) ≥ npl (right( x )) T 1 a a L 1 R ’ R ’ L 1 merge R ’ = Merge(R 1 , T 2 ) L 1 R 1 L 1 a < b R 1 Work at each step = call to merge, swap (constant) T 2 b traverse the right path of both trees = length is at most log N b runtime: O(log n) L 2 R 2 L 2 R 2 6
Leftist Merge Example Sewing Up the Leftist Example merge ? 1 3 5 merge 0 ? ? 1 0 0 3 7 3 3 10 12 1 ? 5 5 0 0 0 0 1 14 7 ? merge 7 1 7 5 1 3 0 0 0 5 5 10 12 10 0 12 0 0 0 0 0 0 0 14 0 14 0 0 14 10 8 7 8 0 0 10 0 10 8 8 8 8 0 0 0 12 14 0 12 12 0 Done? 8 (special case) We forgot to swap L-R at places! 0 12 Skew Heaps Finally…(Leftist) Problems with leftist heaps - Simple to implement, - no npl stuff – extra storage for npl – extra complexity/logic to maintain and check npl 1 1 3 3 – right side is “often” heavy and requires a switch 0 0 Solution: skew heaps 7 7 5 1 5 1 – “blindly” adjusting version of leftist heaps 0 0 0 0 0 0 14 8 8 14 10 10 – merge always switches children when fixing right 0 0 path 12 12 – amortized time for: merge, insert, deleteMin = O(log n ) – however, worst case time for all three = O( n ) 7
Yet Another Data Structure: Merging Two Skew Heaps Binomial Queues merge T 1 • Structural property a a merge – Forest of binomial trees with at most one tree of any height L 1 R 1 L 1 R 1 a < b What’s a forest? T 2 What’s a binomial tree? b b • Order property L 2 R 2 L 2 R 2 – Each binomial tree has the heap-order property Only one step per iteration, with children always switched Binomial Queue with n The Binomial Tree, B h • B h has height h and exactly 2 h nodes elements • B h is formed by making B h -1 a child of another Binomial Q with n elements has a unique structural B h -1 representation in terms of binomial trees! • Root has exactly h children ⎛ ⎞ h ⎜ ⎟ • Number of nodes at depth d is binomial coeff. ⎜ ⎟ Write n in binary: n = 1101 (base 2) = 13 (base 10) ⎝ ⎠ d – Hence the name; we will not use this last property 1 B 3 1 B 2 No B 1 1 B 0 B 0 B 1 B 2 B 3 8
Merging Two Binomial Example: Binomial Queue Queues Merge Essentially like adding two binary numbers! H1: H2: 1. Combine the two forests 3 1 -1 5 2. For k from 1 to maxheight { m ← total number of B k ’s in the two BQs a. 7 2 3 9 6 1 21 # of 1’s b. if m=0: continue; 0+0 = 0 c. if m =1: continue; 11 5 8 7 1+0 = 1 d. if m =2: combine the two B k ’s to form a B k +1 1+1 = 0+c e. if m =3: retain one B k and combine the 6 1+1+c = 1+c other two to form a B k +1 } Claim: When this process ends, the forest has at most one tree of any height Example: Binomial Queue Example: Binomial Queue Merge Merge H1: H2: H1: H2: 1 -1 5 1 -1 3 3 7 2 1 9 6 7 5 2 1 3 3 11 5 11 5 7 9 6 8 8 21 21 6 7 6 9
Recommend
More recommend