Professional Documents
Culture Documents
Simple recursion:
Solution:
Do not solve the same problem again,
just recall it rom memory.
Overlapping subproblems Two methods:
1. Memoization (Top-Down)
2. Tabulation (Bottom-Up)
Memoized solution
Algo fib(n, memo)
{
if memo[n] != null
return memo[n]
if n<=1
result = n
else
result = fib(n-1) + fib(n-2)
memo[n] = result
null null null null null null
return result
}
Tabulation
Algo fib(n)
{
if n<=1
result = n
int bottom_up[n+1];
bottom_up[0] = 0;
bottom_up[1] = 1;
for i from 2 upto n:
bottom_up[i] = bottom_up[i-1] + bottom_up[i-2] null null null null null null
return bottom_up[n];
}
Thank You