WHY DYNAMIC PROGRAMMING??
In Divide & Conqure ,
1) We divide the sub-problem into random instances.
2) Solve each instance randomly.
3) Combine all the solutions to get the final solution.
Now ,there may arise two issues
1) Sub-problems are not independent to solve them
separately. AND/OR
2) We may have to calculate some part of solution
again and again.
SO WHAT’S THERE IN DP?
1) Solution First issue (independence)
• DP allows us to have inter-dependant sub-problems.
• Rather its good to have inter-related sub-instances so
that we can reuse them.
2) Solution to second issue (re-computation)
• Previously computed sub-solutions are used to calculate
further sub-solutions.
• So re-computation is avoided and each time we
calculate next value, We have its required sub-values
calculated.
HOW ITS DONE ?
 Sequence of actions : : ->
1) Divide the input at each possible split point.
2) Compute the solution to smallest instance.
3) STORE THE RESULT. ********
4) Using previously stored result calculate the next
result. (AND STORE IT……!!!)
5) Keep doing this till you reach the final solution.
SO,
1) We never compute the already
computed solution.
2)We always have required result
with us.
EXAMPLES
1) Solving the fibonacci sequence for n=1,000,002.
• This will be a very long process, but what if I
give you the results for n=1,000,000 and
n=1,000,0001? Suddenly the problem just
becomes more manageable.
2)string problems like string edit problem.
You solve a subset(s) of the problem and then use that
information to solve the more difficult original
problem.
FIBONACCI WITHOUT DP
int fib(int n)
{
if (n <= 1)
return n;//Return 0 for n=0 and 1 for n=1
return fib(n-1) + fib(n-2);//Else return Fn-1 + Fn-2
}
• Time complexity : T(n-1) + T(n-2)
Which is exponential…..!
The worst possible Fibonacci solution:
FIBONACCI WITH DP
int fib(int n)
{
/* Declare an array to store fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
TIME AND SPACE WITH DP
• Time Complexity: O(n)
• Extra Space: O(n)
V I S H N U D A H A T O N D E
PRESENTED BY

Dynamic programming Basics

  • 1.
    WHY DYNAMIC PROGRAMMING?? InDivide & Conqure , 1) We divide the sub-problem into random instances. 2) Solve each instance randomly. 3) Combine all the solutions to get the final solution. Now ,there may arise two issues 1) Sub-problems are not independent to solve them separately. AND/OR 2) We may have to calculate some part of solution again and again.
  • 2.
    SO WHAT’S THEREIN DP? 1) Solution First issue (independence) • DP allows us to have inter-dependant sub-problems. • Rather its good to have inter-related sub-instances so that we can reuse them. 2) Solution to second issue (re-computation) • Previously computed sub-solutions are used to calculate further sub-solutions. • So re-computation is avoided and each time we calculate next value, We have its required sub-values calculated.
  • 3.
    HOW ITS DONE?  Sequence of actions : : -> 1) Divide the input at each possible split point. 2) Compute the solution to smallest instance. 3) STORE THE RESULT. ******** 4) Using previously stored result calculate the next result. (AND STORE IT……!!!) 5) Keep doing this till you reach the final solution.
  • 4.
    SO, 1) We nevercompute the already computed solution. 2)We always have required result with us.
  • 5.
    EXAMPLES 1) Solving thefibonacci sequence for n=1,000,002. • This will be a very long process, but what if I give you the results for n=1,000,000 and n=1,000,0001? Suddenly the problem just becomes more manageable. 2)string problems like string edit problem. You solve a subset(s) of the problem and then use that information to solve the more difficult original problem.
  • 6.
    FIBONACCI WITHOUT DP intfib(int n) { if (n <= 1) return n;//Return 0 for n=0 and 1 for n=1 return fib(n-1) + fib(n-2);//Else return Fn-1 + Fn-2 } • Time complexity : T(n-1) + T(n-2) Which is exponential…..! The worst possible Fibonacci solution:
  • 7.
    FIBONACCI WITH DP intfib(int n) { /* Declare an array to store fibonacci numbers. */ int f[n+1]; int i; /* 0th and 1st number of the series are 0 and 1*/ f[0] = 0; f[1] = 1; for (i = 2; i <= n; i++) { /* Add the previous 2 numbers in the series and store it */ f[i] = f[i-1] + f[i-2]; } return f[n]; }
  • 8.
    TIME AND SPACEWITH DP • Time Complexity: O(n) • Extra Space: O(n)
  • 9.
    V I SH N U D A H A T O N D E PRESENTED BY