DS-chapter10(Dynamic Programming)

DS-chapter10(Dynamic Programming) -...

Info icon This 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 )
Image of page 1

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

View Full Document Right 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 )
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 ?
Image of page 3

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

View Full Document Right 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 .
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++ )
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern