- It is a way of making an algorithm more efficient by storing some of the intermediate results.
- It works well when our algorithm has lots of repetitive computations.
- We can use recurrsion to solve the programming problems but iteration method is mostly used.
- It is based on prncipal of optimality.
- Example:- Fibonacci number series
- Principal of optimality states that a problem can be solved by taking sequence of decisions to get the optimal solution.
- means we can solve larger problems given the solution to it's smaller sub-problems
- Recursion
- Store (memoize)
- Bottom-up
Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem.
In our recursive solution if we notice that there are repetitive computations then store them.
"memoize != memorize"
Going bottom-up is a way to avoid recursion, saving the memory cost that recursion incurs when it builds up the call stack. Put simply, a bottom-up algorithm "starts from the beginning," while a recursive algorithm often "starts from the end and works backwards."
Note:
- In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
- Now what dynamic programming says that fib(3) and fib(2) can be stored.
-
In greedy method we have a predefined path to follow and we assume that the path or algorithm we are following is optimal but in case of dynamic programming every stage we take a decision.
-
Greedy algorithm never reconsiders its choices whereas Dynamic programming may consider the previous state.
-
Greedy algorithm is less efficient whereas Dynamic programming is more efficient.
-
Greedy algorithm have a local choice of the sub-problems whereas Dynamic programming would solve the all sub-problems and then select one that would lead to an optimal solution.
- Greedy algorithm work based on choice property whereas Dynamic programming work based on principle of optimality.
- Greedy algorithm follows the top-down strategy whereas Dynamic programming follows the bottom-up strategy.