{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 6 - Dynamic Programming

# Lecture 6 - Dynamic Programming - Dynamic Programming...

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

Dynamic Programming

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

View Full Document
Dynamic Programming Similar to divide-and-conquer in that the problem instance is divided into smaller instances. It is a bottom-up approach. In this approach the small instances are solved first, their result is saved and retrieved later when needed instead of being recomputed. Dynamic Programming includes two steps: – Establish a recursive property that gives the solution to an instance of the problem. – Solve an instance of the problem in a bottom-up fashion by solving smaller instances first. Trade space for time.
Solving Fibonacci Using Divide and Conquer fib(4) fib(2) fib(0) fib(1) fib(1) fib(2) fib(3) fib(0) fib(1) int fib ( int n) { if (n <= 1) return n; else return fib(n-1) + fib(n-2); } Fibonacci Sequence: f 0 = 0 f 1 = 1 f n = f n-1 + f n-2

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

View Full Document
Fibonacci Sequence -Dynamic Programming- Problem : Determine the n th term in the Fibonacci Sequence Inputs : a nonnegative integer n. Outputs: fib2, the n th term in the Fibonacci Sequence. int fib2 ( int n) { index i; int f [0. .n]; f [0] = 0; if (n > 0) { f [1] = 1; for (i = 2; i <= n; i++) f [i] = f [i – 1] + f [i –2]; } return f [n]; } Recursive property Solve smaller instances first Use an array to save computed results
The Binomial Coefficient The Binomial Coefficient is given by: Finding the Coeff. by directly applying the Above definition is not possible for large values of n and k. Recursive property: n! k! (n - k)! n k For 0 k n = n 0 ) ( = 1 n n ) ( = 1 n k ) ( = n- 1 k- 1 ) ( n- 1 k ) ( +

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

View Full Document
Finding the Recursive Property ( 1)! ( 1)!(( 1) ( 1))! n k n k ( !(( 1) )! n k n k  ( ( 1)!( n k n k ( !( n k n k ( kn k n k ( )( n k n k n k ( 1)! ( )( k n n k n k n k ( )( n k k n k n k   ( nn k n k ! n k n k n k = = = = = + = + = + = n - 1 k - 1 + n - 1 k
As for Fibonacci numbers, this is a very inefficient algorithm The reason is that same instances are solved in each recursive call. bin(n-1, k-1) bin(n-1, k) Binomial Coefficient Using Divide and Conquer int bin ( int n, int k) { if (k = = 0 || n = = k ) return 1; else return bin(n-1, k-1) + bin(n-1, k); } n 0 ) ( = 1 n n ) ( = 1 n k ) ( = n- 1 k- 1 ) ( n- 1 k ) ( + Both need the result of bin(n-2, k-1) Thus bin(n-2, k-1) is solved separately in each recursive call

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

View Full Document
Recursive Property: B[i-1][j-1] + B[i-1][j] 0 < j < i 1 j = 0 or j = i Solve an instance of the problem in a bottom-up fashion. Compute the rows in B in sequence starting from the first row. Binomial Coefficient Using Dynamic Programming i 0 ) ( = 1 i i ) ( = 1 i j ) ( = i - 1 j - 1 ) ( i - 1 j ) ( + B[i][j] = 0 1 2 3 4 j k 0 1 2 3 4 i n 1 1 1 1 1 1 2 3 4 1 3 6 1 4 1 B[i-1][j-1] B[i-1][j] B[i][j]
Binomial Coefficient Algorithm Using Dynamic Prog. Problem : Compute the Binomial Coefficient Inputs : nonnegative integers n and k, where k n. Outputs: bin2, the binomial coefficient . int bin2 ( int n, int k) { index i,j; int B[0. .n][0. .k]; for (i = 0; i <= n; i++) for (j = 0; j <= minimum(i,k); j++) if (j = = 0 || j = = i ) B[i][j] = 1; else B[i][j] = B[i-1][j-1] + B[i-1][j]; return B[n][k]; } n k

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

View Full Document
Time Complexity of Binomial Coefficient Algorithm int bin2 ( int n, int k) { index i,j; int B[0. .n][0. .k]; for (i = 0; i <= n; i++) for (j = 0; j <= minimum(i,k); j++) if (j = = 0 || j = = i ) B[i][j] = 1; else B[i][j] = B[i-1][j-1] + B[i-1][j]; return B[n][k]; } i Nb of Passes 0 1 2 k k+1 n 1 2 k+1 k+1 k+1 T(n) = 1 + 2 + … + k + (k+1) + (k+1) + … + (k+1) = + (n - k + 1) (k + 1) = k(k + 1) 2 (2n - k + 2)(k + 1) 2 (nk)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 35

Lecture 6 - Dynamic Programming - Dynamic Programming...

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

View Full Document
Ask a homework question - tutors are online