{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

DS-chapter10&iuml;&frac14;ˆDynamic Programming)

# DS-chapter10&iuml;&frac14;ˆDynamic Programming) -...

This preview shows pages 1–6. Sign up to view the full content.

§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F ( N ) = F ( N – 1) + F ( N – 2) int Fib( int N ) { if ( N <= 1 ) return 1; else return Fib( N - 1 ) + Fib( N - 2 ); } T ( N ) T ( N – 1) + T ( N – 2) T ( N ) F ( N )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
§3 Dynamic Programming F6 F2 F1 F0 F3 F1 F2 F1 F0 F2 F1 F0 F3 F1 F2 F1 F0 F3 F1 F2 F1 F0 F4 F4 F5 Trouble-maker : The growth of redundant calculations is explosive. Solution : Record the two most recently computed values to avoid recursive calls. int Fibonacci ( int N ) { int i, Last, NextToLast, Answer; if ( N <= 1 ) return 1; Last = NextToLast = 1; /* F(0) = F(1) = 1 */ for ( i = 2; i <= N; i++ ) { Answer = Last + NextToLast; /* F(i) = F(i-1) + F(i-2) */ NextToLast = Last; Last = Answer; /* update F(i-1) and F(i-2) */ } /* end-for */ return Answer; } T ( N ) = O( N )
§3 Dynamic Programming 2. Ordering Matrix Multiplications 3 Example 3 Suppose we are to multiply 4 matrices M 1 [ 10 × 20 ] M 2 [ 20 × 50 ] M 3 [ 50 × 1 ] M 4 [ 1 × 100 ] . If we multiply in the order M 1 [ 10 × 20 ] ( M 2 [ 20 × 50 ] ( M 3 [ 50 × 1 ] M 4 [ 1 × 100 ] ) ) Then the computing time is 50 × 1 × 100 + 20 × 50 × 100 + 10 × 20 × 100 = 125,000 If we multiply in the order ( M 1 [ 10 × 20 ] ( M 2 [ 20 × 50 ] M 3 [ 50 × 1 ] ) ) M 4 [ 1 × 100 ] Then the computing time is 20 × 50 × 1 + 10 × 20 × 1 + 10 × 1 × 100 = 2,200 Problem: In which order can we compute the product of n matrices with minimal computing time ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
§3 Dynamic Programming Let b n = number of different ways to compute M 1 M 2 M n . Then we have b 2 = 1, b 3 = 2, b 4 = 5, Let M ij = M i M j . Then M 1 n = M 1 M n = M 1 i M i+ 1 n b n b i b n - i . 1 and 1 where 1 1 1 = = - = - b n b b b n i i n i n ) ( 4 n n n n O b = /* Catalan number */ Suppose we are to multiply n matrices M 1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ M n where M i is an r i - 1 × r i matrix. Let m ij be the cost of the optimal way to compute M i ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ M j . Then we have the recurrence equations: + + = = - + < i j r r r m m j i m j l i j l il j l i ij if } { min if 0 1 1 There are only O( N 2 ) values of M ij . If j – i = k , then the only values M xy required to compute M ij satisfy y – x < k .
§3 Dynamic Programming /* r contains number of columns for each of the N matrices */ /* r[ 0 ] is the number of rows in matrix 1 */ /* Minimum number of multiplications is left in M[ 1 ][ N ] */ void OptMatrix( const long r[ ], int

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

DS-chapter10&iuml;&frac14;ˆDynamic Programming) -...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online