17 dynamic programming
play

17: Dynamic Programming CS1101S: Programming Methodology Martin - PowerPoint PPT Presentation

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 17: Dynamic Programming CS1101S: Programming Methodology Martin Henz October 19, 2012 CS1101S: Programming Methodology 17: Dynamic Programming Fibonacci Numbers Dropping


  1. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 17: Dynamic Programming CS1101S: Programming Methodology Martin Henz October 19, 2012 CS1101S: Programming Methodology 17: Dynamic Programming

  2. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 1 Fibonacci Numbers 2 Dropping Eggs Puzzle 3 Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  3. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 1 Fibonacci Numbers 2 Dropping Eggs Puzzle 3 Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  4. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Inefficient Algorithm function f i b ( n ) { i f ( n < = 1) { return 1; } else { return f i b ( n − 1) + f i b ( n − 2 ) ; } } CS1101S: Programming Methodology 17: Dynamic Programming

  5. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Trace of Recursion CS1101S: Programming Methodology 17: Dynamic Programming

  6. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Memoization var f i b s = [ ] ; function f i b ( n ) { i f ( f i b s [ n ]!== undefined ) { return f i b s [ n ] ; } else i f ( n < = 1) { return 1; } else { var new fib = f i b ( n − 1) + f i b ( n − 2 ) ; f i b s [ n ] = new fib ; return new fib ; } } CS1101S: Programming Methodology 17: Dynamic Programming

  7. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree A Simple Loop for Fibonacci Numbers function f i b ( n ) { i f ( n < = 1) { return 1; } else { var l a s t = 1 , nextToLast = 1; answer = 1; var i = 2; while ( i < = n ) { answer = l a s t + nextToLast ; nextToLast = l a s t ; l a s t = answer ; i = i + 1; } return answer ; } } CS1101S: Programming Methodology 17: Dynamic Programming

  8. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 1 Fibonacci Numbers 2 Dropping Eggs Puzzle 3 Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  9. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Egg Dropping Puzzle Given n eggs, building with k floors Wanted Smallest number of egg dropping experiments required to find out in all cases, which floors an egg can be safely dropped from CS1101S: Programming Methodology 17: Dynamic Programming

  10. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Assumptions An egg that survives a fall can be used again. A broken egg must be discarded. The effect of a fall is the same for all eggs. If an egg breaks when dropped, then it would break if dropped from a higher floor. If an egg survives a fall then it would survive a shorter fall. A first-floor drop may break eggs, and eggs may survive a drop from the highest floor. CS1101S: Programming Methodology 17: Dynamic Programming

  11. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Special Case: One Egg Number of eggs = 1, number of floors = 21 We need at most 21 experiments CS1101S: Programming Methodology 17: Dynamic Programming

  12. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Special Case: Two Eggs Animated scenario click here CS1101S: Programming Methodology 17: Dynamic Programming

  13. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Observations Sub-tasks At each point in time, we have a number of eggs n available and a number of floors k to check Contiguous floors to check The height of the floors does not matter. At each point in time we need to check a certain number of contiguous floors, say from 10 to 14. Height does not matter Checking 10 to 14 is the same as checking 20 to 24. CS1101S: Programming Methodology 17: Dynamic Programming

  14. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree A simple algorithm function eggDrop (n , k ) { i f ( k = < 1 | | n === 1) { return k ; } else { var min = large constant ; var x = 1; var res = undefined ; while ( x < = k ) { res = max( eggDrop (n − 1, x − 1) , eggDrop (n , k − x ) ) ; i f ( res < min ) min = res ; x = x + 1; } return min + 1; } } CS1101S: Programming Methodology 17: Dynamic Programming

  15. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Solution Idea Observation We compute eggDrop(i,j) over and over again. Remember results in a table Allocate a 2-D table eggFloor that remembers the results; after computing s = eggDrop(i,j), remember s in a table. eggDrop [ i ] [ j ] = s ; CS1101S: Programming Methodology 17: Dynamic Programming

  16. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree 1 Fibonacci Numbers 2 Dropping Eggs Puzzle 3 Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  17. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Optimal Binary Search Tree Given a set of words { w 1 , . . . , w n } probabilities of each word’s occurrence { p 1 , . . . , p n } Wanted Binary tree that includes all words and has the lowest expected cost: n expected cost = d i p i � i = 1 where d i is the depth of word i in the tree CS1101S: Programming Methodology 17: Dynamic Programming

  18. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Sample Input CS1101S: Programming Methodology 17: Dynamic Programming

  19. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Three Possible Binary Search Trees CS1101S: Programming Methodology 17: Dynamic Programming

  20. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Comparison of the Three Trees CS1101S: Programming Methodology 17: Dynamic Programming

  21. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Structure of Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  22. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Example CS1101S: Programming Methodology 17: Dynamic Programming

  23. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Idea Proceed in order of growing tree size For each range of words, compute optimal tree Memoization For each range, store optimal tree for later retrieval CS1101S: Programming Methodology 17: Dynamic Programming

  24. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Computation of Optimal Binary Search Tree CS1101S: Programming Methodology 17: Dynamic Programming

  25. Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree Run Time For each cell of table Consider all possible roots Overall runtime O ( N 3 ) CS1101S: Programming Methodology 17: Dynamic Programming

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend