Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),

Similar presentations


Presentation on theme: "Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),"— Presentation transcript:

1 Analysis of Algorithms Dynamic Programming

2 A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array), there by Avoiding the work of recomputing the answer every time the sub problem is encountered. Dynamic programming is typically applied to optimization problems. What is an optimization problem? There can be may possible solutions. Each solution has a value and We wish to find a solution with the optimal (minimum or maximum) value

3 Toward Dynamic Programming Problems with divide and conquer Recursive Fibonnacci numbers algorithm with exponential complexity. When sub problems are not independent Fib1(N) {if (N =< 1) return 1; else return Fib(N-1) + Fib(N-2) }

4 Dynamic Programming Bottom-up approach with Memoization  Instead of computing again and again, we can store the computed results in an array. The stored values can be used for any future use.  Assume, we want to compute Fib(n) with max. value of n is MAXVALUE. #define MAXVALUE 100; Fib2(n) { int F[MAXVALUE]; for(i = 1; i <= n; i++) F[i] = 1; for(i = 2; i <= n; i++) F[i] = F[i-1] + F[i-2]; return F[n]; }

5 Dynamic Programming Bottom-up Approach with Memoization Sometimes after developing an algorithm using array, we are able to revise the algorithm to save originally allocated space Fib2(n) {int Fn = 1, Fn1 = 1, Fn2 = 1; for(I = 2; I <= n; I++) {Fn = Fn1 + Fn2; Fn2 = Fn1; Fn1 = Fn; } return Fn; }

6 Dynamic Programming The steps in development of dynamic programming algorithm are Establish a recursive property that gives the solution to an instance of a problem Solve an instance of the problem in Bottom- up fashion by solving smaller instances first.

7 Example of Binomial Coefficient The binomial coefficient is given by For values of n and k that are not small, We can not compute the binomial coefficient directly from this definition because n! is very large

8 Example of Binomial Coefficient We can eliminate the need to compute n! or k! by using following recursive property. This suggests the following divide-and- conquer algorithm.

9 Example of Binomial Coefficient Using Divide-and- Conquer Int bin(int n, int k) { if (k = 0 or n = k ) return 1; else return(bin(n-1, k-1) + bin(n-1, k)) }

10 Example of Binomial Coefficient Using Divide-and- Conquer This algorithm is very similar to the divide-and-conquer algorithm of computing nth fabnacci term. Therefore this algorithm is very inefficient due to the same reasons.

11 Example of Binomial Coefficient Using Dynamic Programming We will use the recursive definition to construct our solution in an array B. Where B[i, j] will contain Solve an instance of the problem in a bottom-up fashion by computing the rows in B in sequence starting with the first row.

12 ` Int bin(int n, int k) { int i, j; int B[0..n, 0..k]; for i = 0 to n for j = 0 to minimum(i, k) if( j = 0 or j = i) B[i, j] = 1; else B[i, j] = B[i-1, j-1] + B[i-1, j]; return B[n, k] }

13 Complexity Analysis number of passes i 0123kk+1 n j 1234k+1 k+1 k+1 total number of passes 1+2+3++k+k+1+k+1…….+k+1 (k+1 is repeacted for n-k+1 times) k(k+1)/2+(k+1)(n-k+1) =((2n-k+2)(k+1))/2 є Ө(nk)

14 Dynamic Programming Dynamic programming is similar to divide and conquer in that we find a recursive property. But instead of blindly using recursion, we use the recursive property to iteratively solve the instances in sequence starting with the smallest instance And we solve each instance ONCE


Download ppt "Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),"

Similar presentations


Ads by Google