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

  1. Write the function header so that you are sure what the function will do and how it will be called;
  2. Decompose the problem into subproblems;
  3. Write recursive calls to solve those subproblems whose form is similar to that of the original problem.
  4. 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
  5. 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

  1. 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).
  2. The test for the base case must execute before the recursive call.
  3. 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).
  4. The recursive call must not skip over the base case.
  5. The non-recursive portions of the subprogram must operate correctly.



No comments

Powered by Blogger.