CSE 373: Floyd’s buildHeap algorithm; E 8 I 7 H 6 G 5 F 4 3 9 D 2 C 1 B 0 A And fjll an array in the level-order of the tree: divide-and-conquer J K The left child? 4 Observation: unfortunately, in practice, usually must percolate all ...which is: Runtime of removeMin: Re-analyzing removeMin 5 Observation: when percolating, we usually need to percolate up a ...which is: Runtime of insert: Re-analyzing insert invariant was useful after all) 10 ...assuming array has no ’gaps’. (Hey, it looks like the structure fjnd the last node now? If our tree is represented using an array, what’s the time needed to Finding the last node 3 14 13 12 11 L The right child? 6 1 a a 3 c 2 b Warmup a 0 In array form c c b a Warmup: c a a In tree form Insert the following letters into an empty binary min-heap. Draw 4 5 the heap’s internal state in both tree and array form: J Michael Lee Wednesday, Feb 7, 2018 1 How do we fjnd parent? G L F C K E 6 I H D B A Take a tree: The array-based representation of binary heaps 2 7 c, b, a, a, a, c � i − 1 � parent ( i ) = 2 leftChild ( i ) = 2 i + 1 Θ (1) : just use this.array[this.size - 1] . leftChild ( i ) = 2 i + 2 How does this change runtime of insert ? How does this change runtime of removeMin ? findLastNodeTime + addNodeToLastTime + numSwaps × swapTime fjndLastNodeTime + removeRootTime + numSwaps × swapTime 1 + 1 + numSwaps × 1 1 + 1 + numSwaps × 1 few times! So, numSwaps ≈ 1 in the average case, and the way down. So numSwaps ≈ height ≈ log ( n ) on average. numSwaps ≈ height = log ( n ) in the worst case!
Project 2 10 Fill out regrade request form on course website. 9 An interesting extension We discussed how to implement insert , where we insert one element into the heap. What if we want to insert n difgerent elements into the heap? An interesting extension Not sure yet. Can we do better? algorithm”. 11 Floyd’s buildHeap algorithm The basic idea: to the top Deadlines: Wait a day, then email me. 12 8 Make sure to... Feel free to schedule an appointment with me. Grades Some stats about the midterm: 7 Grades Common questions: ◮ Partner selection: Fri, Feb 9 ◮ Part 1: Fri, Feb 16 ◮ Parts 2 and 3: Fri, Feb 23 ◮ Mean and median ≈ 80 (out of 100) ◮ Standard deviation ≈ 13 ◮ Find a difgerent partner for project 3 ◮ ...or email me and petition to keep your current partner ◮ I want to know how to do better next time ◮ How will fjnal grades be curved? ◮ I want a midterm regrade. ◮ I want a regrade on a project or written homework Idea 1: just call insert n times – total runtime of Θ ( n log ( n )) ◮ Start with an array of all n elements ◮ Start traversing backwards – e.g. from the bottom of the tree Yes! Possible to do in Θ ( n ) time, using “Floyd’s buildHeap ◮ Call percolateDown(...) per each node
Floyd’s buildheap algorithm: example 15 7 9 10 A visualization: Floyd’s buildheap algorithm: example 13 14 2 4 13 1 12 2 11 3 6 10 4 2 8 1 9 0 10 1 1 0 5 3 2 8 0 3 6 3 8 4 1 0 2 3 5 0 0 1 6 2 3 7 9 10 9 9 0 1 8 2 7 3 6 5 1 5 4 6 3 7 2 8 7 6 A visualization: 2 5 2 4 0 3 7 8 6 1 9 0 10 4 1 0 3 5 5 15 14 4 13 1 12 11 7 3 10 6 9 1 8 2 0 3 4 8 11 3 10 6 9 1 2 12 7 3 6 0 5 2 5 1 2 7 8 6 1 0 2 3 9 13 10 A visualization: Floyd’s buildheap algorithm: example 13 15 14 4 10 Floyd’s buildheap algorithm: example A visualization: 14 9 10 A visualization: Floyd’s buildheap algorithm: example 13 15 0 3 13 1 12 2 11 3 7 2 0 0 2 8 1 9 0 10 1 6 4 2 3 5 8 0 1 10 9 3 0 1 4 2 3 5 8 1 10 6 2 3 7 9 10 0 0 1 5 8 2 7 3 6 4 5 9 4 6 3 7 2 8 1 7 6 13 2 5 5 4 6 3 7 8 6 1 9 0 10 0 1 4 3 2 2 15 14 0 13 1 12 11 7 3 10 0 9 1 8 2 4 3 4 8 11 3 10 0 9 1 2 12 7 3 6 4 5 5 2 1 5 7 8 0 1 6 2 3 9 13 10 A visualization: Floyd’s buildheap algorithm: example 13 15 14 0 13
Floyd’s buildheap algorithm: example 3 6 3 7 7 8 9 9 6 10 11 5 5 12 8 13 4 14 15 13 Floyd’s buildheap algorithm: example A visualization: 1 2 1 1 3 7 1 9 A visualization: 0 2 3 5 8 4 4 10 0 0 1 0 2 2 3 1 0 2 0 5 7 7 8 9 9 6 10 3 11 12 6 8 13 4 14 15 13 Floyd’s buildheap algorithm 14 Analyzing Floyd’s buildheap algorithm What’s the pattern? 3 1 3 4 7 6 9 10 0 2 3 5 1 8 10 5 0 0 1 0 2 2 3 1 4 2 2 6 10 9 2 5 0 6 3 7 7 8 1 6 0 10 3 11 5 12 A visualization: 13 4 14 15 4 3 Floyd’s buildheap algorithm: example 2 10 9 2 3 7 0 1 6 8 3 2 5 0 1 4 10 0 9 1 8 2 13 1 A visualization: 10 1 6 3 7 10 8 1 9 6 3 2 11 5 12 8 13 4 14 15 13 Floyd’s buildheap algorithm: example 5 7 4 5 9 2 3 7 0 1 6 0 2 3 0 1 0 3 2 8 2 1 0 4 9 10 15 Question: How much work is percolateDown actually doing? (1 node) × (4 work) Wait... isn’t this still n log ( n ) ? (2 nodes) × (3 work) We look at n nodes, and we run percolateDown(...) on each node, which takes log ( n ) time... right? (4 nodes) × (2 work) (8 nodes) × (1 work) Yes – algorithm is O ( n log ( n )) , but with a more careful analysis, we can show it’s O ( n ) ! work ( n ) ≈ n 2 · 1 + n 4 · 2 + n 8 · 3 + · · ·
Analyzing Floyd’s buildheap algorithm Lessons learned: Analyzing Floyd’s buildheap algorithm Strategy: prove the summation is upper-bounded by something even when the summation goes on for infjnity. If we can do this, then our original summation must defjnitely be upper-bounded by the same thing. i We had: i Using an identity (see page 4 of Weiss): i 17 Analyzing Floyd’s buildheap algorithm So design an algorithm that does less work closer to ‘bottom’ i doing asymptotic analysis!) 18 Analyzing Floyd’s buildheap algorithm What we’re skipping queues) 19 On to sorting And now on to sorting... 20 Why study sorting? “divide-and-conquer” (General purpose sorts work well most of the time, but you might need something more effjcient in niche cases) 16 21 Can we write this in summation form? Yes. i Let’s rewrite bottom as powers of two, and factor out the n : (Seems hard to analyze...) So let’s just make it infjnity! i work ( n ) ≈ n 2 · 1 + n 4 · 2 + n 8 · 3 + · · · � 1 2 1 + 2 2 2 + 3 � work ( n ) ≈ n 2 3 + · · · ? ∞ � � work ( n ) ≈ n 2 i ≤ n 2 i i =1 i =1 ? work ( n ) ≈ n � 2 i i =1 ∞ What is ? supposed to be? It’s the height of the tree: so log ( n ) . work ( n ) ≤ n � 2 i = n · 2 i =1 ? ∞ So buildHeap runs in O ( n ) time! � � work ( n ) ≈ n 2 i ≤ n 2 i i =1 i =1 ◮ Most of the nodes near leaves (almost 1 2 of nodes are leaves!) ◮ How do we merge two heaps together? ◮ More careful analysis can reveal tighter bounds ◮ Strategy: rather then trying to show a ≤ b directly, it can ◮ Other kinds of heaps (leftist heaps, skew heaps, binomial sometimes be simpler to show a ≤ t then t ≤ b . (Similar to what we did when fjnding c and n 0 questions when Why not just use Collections.sort(...) ? ◮ You should just use Collections.sort(...) ◮ A vehicle for talking about a technique called ◮ Difgerent sorts have difgerent purposes/tradeofgs. ◮ It’s a “thing everybody knows”.
Recommend
More recommend