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

17 dynamic programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 5

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Trace of Recursion

CS1101S: Programming Methodology 17: Dynamic Programming

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

  • ut in all cases, which floors an egg can be safely dropped from

CS1101S: Programming Methodology 17: Dynamic Programming

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Special Case: Two Eggs

Animated scenario click here

CS1101S: Programming Methodology 17: Dynamic Programming

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Optimal Binary Search Tree

Given a set of words {w1, . . . , wn} probabilities of each word’s occurrence {p1, . . . , pn} Wanted Binary tree that includes all words and has the lowest expected cost: expected cost =

n

  • i=1

dipi where di is the depth of word i in the tree

CS1101S: Programming Methodology 17: Dynamic Programming

slide-18
SLIDE 18

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Sample Input

CS1101S: Programming Methodology 17: Dynamic Programming

slide-19
SLIDE 19

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Three Possible Binary Search Trees

CS1101S: Programming Methodology 17: Dynamic Programming

slide-20
SLIDE 20

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Comparison of the Three Trees

CS1101S: Programming Methodology 17: Dynamic Programming

slide-21
SLIDE 21

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Structure of Optimal Binary Search Tree

CS1101S: Programming Methodology 17: Dynamic Programming

slide-22
SLIDE 22

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Example

CS1101S: Programming Methodology 17: Dynamic Programming

slide-23
SLIDE 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

slide-24
SLIDE 24

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Computation of Optimal Binary Search Tree

CS1101S: Programming Methodology 17: Dynamic Programming

slide-25
SLIDE 25

Fibonacci Numbers Dropping Eggs Puzzle Optimal Binary Search Tree

Run Time

For each cell of table Consider all possible roots Overall runtime O(N3)

CS1101S: Programming Methodology 17: Dynamic Programming