Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem...
-
Upload
peregrine-cunningham -
Category
Documents
-
view
226 -
download
0
Transcript of Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem...
![Page 1: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/1.jpg)
Analysis of Algorithms
Dynamic Programming
![Page 2: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/2.jpg)
Dynamic Programming
A dynamic programming algorithm solves every sub problem just once and then
Saves its answer in a table (array), there byAvoiding 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 andWe wish to find a solution with the optimal (minimum or
maximum) value
![Page 3: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/3.jpg)
Toward Dynamic ProgrammingProblems with divide and conquerRecursive Fibonnacci numbers algorithm with
exponential complexity.When sub problems are not independentFib1(N){ if (N =< 1)
return 1;else
return Fib(N-1) + Fib(N-2)}
![Page 4: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/4.jpg)
Dynamic ProgrammingBottom-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];
}
![Page 5: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/5.jpg)
Dynamic ProgrammingBottom-up Approach with
MemoizationSometimes 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;
}
![Page 6: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/6.jpg)
Dynamic Programming
The steps in development of dynamic programming algorithm areEstablish a recursive property that gives the
solution to an instance of a problemSolve an instance of the problem in Bottom-
up fashion by solving smaller instances first.
![Page 7: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/7.jpg)
Example of Binomial Coefficient
The binomial coefficient is given by
n. k 0for )!(!
!
knk
n
k
n
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
![Page 8: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/8.jpg)
Example of Binomial Coefficient
We can eliminate the need to compute n! or k! by using following recursive property.
n k or 0 k 1
n k 0 1
1
1
k
n
k
n
k
n
This suggests the following divide-and-conquer algorithm.
![Page 9: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/9.jpg)
Example of Binomial Coefficient Using Divide-and-
ConquerInt bin(int n, int k)
{
if (k = 0 or n = k )
return 1;
else
return(bin(n-1, k-1) + bin(n-1, k))
}
![Page 10: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/10.jpg)
Example of Binomial Coefficient Using Divide-and-
ConquerThis 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.
![Page 11: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/11.jpg)
Example of Binomial Coefficient Using Dynamic
ProgrammingWe will use the recursive definition to
construct our solution in an array B.Where B[i, j] will contain
j
i
Solve an instance of the problem in a bottom-up fashion by computing the rows in B in sequence starting with the first row.
![Page 12: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/12.jpg)
`
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;elseB[i, j] = B[i-1, j-1] + B[i-1, j];
return B[n, k]}
![Page 13: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/13.jpg)
Complexity Analysis
number of passesi 0 1 2 3 k k+1 nj 1 2 3 4 k+1 k+1 k+1total number of passes1+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)
![Page 14: Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),](https://reader035.fdocuments.net/reader035/viewer/2022080905/56649cef5503460f949bdaea/html5/thumbnails/14.jpg)
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