Embed presentation
Download to read offline


![Dynamic Programming Example:
Longest Common Subsequence
• Longest common subsequence (LCS)
problem:
– Given two sequences x[1..m] and y[1..n], find
the longest subsequence which occurs in both
– Ex: x = {A B C B D A B }, y = {B D C A B A}
– {B C} and {A A} are both subsequences of both
– Brute-force algorithm: For every subsequence
of x, check if it’s a subsequence of y](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-3-2048.jpg)

![Fibonacci(Recursion)
int fib(int n)
{
if(memo[n] != null){
return memo[n]
}
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-5-2048.jpg)
![Fibonacci(Memoized)
int fib(int n)
{
if(memo[n] != null){
return memo[n]
}
if (n <= 1)
result = 1
else{
result = fib(n-1) + fib(n-2);
}
fibo[n] = result;
return result;
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-6-2048.jpg)
![Fibonacci(DP)
function fib(n) {
res[0] = 0;
res[1] = 1;
for(let i = 2; i <= n; i ++) {
res[i] = fibo[i - 1] + fibo[i - 2]
}
return return
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-7-2048.jpg)


Dynamic programming is a strategy for designing algorithms that breaks problems down into recurring subproblems. It is useful when a problem can be solved by combining the solutions of its subproblems. The longest common subsequence problem finds the longest shared subsequence between two sequences and can be solved using dynamic programming by building up the solution from overlapping subproblems. Computing the nth Fibonacci number can also be solved with dynamic programming by storing and reusing previously computed values rather than recomputing them.


![Dynamic Programming Example:
Longest Common Subsequence
• Longest common subsequence (LCS)
problem:
– Given two sequences x[1..m] and y[1..n], find
the longest subsequence which occurs in both
– Ex: x = {A B C B D A B }, y = {B D C A B A}
– {B C} and {A A} are both subsequences of both
– Brute-force algorithm: For every subsequence
of x, check if it’s a subsequence of y](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-3-2048.jpg)

![Fibonacci(Recursion)
int fib(int n)
{
if(memo[n] != null){
return memo[n]
}
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-5-2048.jpg)
![Fibonacci(Memoized)
int fib(int n)
{
if(memo[n] != null){
return memo[n]
}
if (n <= 1)
result = 1
else{
result = fib(n-1) + fib(n-2);
}
fibo[n] = result;
return result;
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-6-2048.jpg)
![Fibonacci(DP)
function fib(n) {
res[0] = 0;
res[1] = 1;
for(let i = 2; i <= n; i ++) {
res[i] = fibo[i - 1] + fibo[i - 2]
}
return return
}](https://image.slidesharecdn.com/algorithmlecture06dynamicprogramming-191215150514/75/Algorithm-lecture-Dynamic-programming-7-2048.jpg)
