DS-chapter10(Dynamic Programming)

DS-chapter10(Dynamic Programming) - 3 Dynamic

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

View Full Document Right Arrow Icon
§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 )
Background image of page 1

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

View Full DocumentRight Arrow Icon
§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 )
Background image of page 2
§3 Dynamic Programming 2. Ordering Matrix Multiplications 〖 Example 〖 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 ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
§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 .
Background image of page 4
§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 N, TwoDimArray M ) { int i, k, Left, Right; long ThisM; for ( Left = 1; Left <= N; Left++ )
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2011 for the course COMPUTER S 10586 taught by Professor Jilinwang during the Spring '09 term at Zhejiang University.

Page1 / 15

DS-chapter10(Dynamic Programming) - 3 Dynamic

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online