Start with The Bible : CLRS 📖
The DP chapter told me first go and read Divide and Conquer then after come to me and I will show you the direction to the dynamic programming. Now, the only option for me is to read a chapter of divide and conquer.
Divide and Conquer
Now they told me, go check recursion first the come at me.
Recursion
To successfully apply recursion to a problem, you must be able to break the problem down into subparts, at least one of which is similar in form to the original problem.
The general approach to writing a recursion function
- Write the function header so that you are sure what the function will do and how it will be called;
- Decompose the problem into subproblems;
- Write recursive calls to solve those subproblems whose form is similar to that of the original problem.
- Write code to combine, augment, or modify the results of the recursive call(s) if necessary to construct the desired return value or create the desired side--effects; and
- Write base case(s) to handle any situations that are not handled properly by the recursive portion of the program.
Ensuring that recursion will work
- A recursive subprogram must have at least one base case and one recursive case (it's OK to have more than one base case, and more than one recursive case).
- The test for the base case must execute before the recursive call.
- The problem must be broken down in such a way that the recursive call is closer to the base case than the top-level call. ( This condition is actually not quite strong enough. Moving toward the base case alone is not sufficient; it must also be true that the base case is reached in a finite number of recursive calls. In practice though, it is rare to encounter situations where there is always a movement toward the base case but the base case is not reached).
- The recursive call must not skip over the base case.
- The non-recursive portions of the subprogram must operate correctly.
Leave a Comment