dknite's blog

By dknite, history, 8 days ago, In English

I have been stuck with Recursion. I tried a lot of youtube videos to understand the concept but the more I try, the more I feel like I understood nothing. I am able to do easy problems based on Recursion but not more than that. I really need suggestions to overcome and make my recursion as strong as possible. I really don't want to move forward without having a good hold on recursion.

If anyone has gone through this phase, then please suggest me the same.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
8 days ago, # |
  Vote: I like it +3 Vote: I do not like it

In my opinion all you need to know about recursion is its structure. If you understand what is base case and how you proceed with calling recursion — then you know recursion.

You might not feel good about it because problems are too hard at this stage, not because you don't know recursion.

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Well, I will only say the steps or approach that made me comfortable even with medium problems in recursion,

** This will not necessarily make you an expert in recursion-based problems, only practice will make you better.Rather this approach might help you in visualizing how actually your recursion-based code is working. **

Approach: 1. Take a pen and paper and write down the base cases of the problem

  1. Draw the recursion tree,

  2. Write the recursion based code (pseudocode in paper, actual code in editor),

  3. (Most imp) after writing the code , DEBUG your code, use breakpoints in every line of your code, and watch how every line of your code is getting executed,

While Debugging, try comparing your recursion tree (on paper) and look how your code is actually working, Eventually you will notice that your code will first create the left branches and nodes and then the next branches and so on.

try our some medium-level problems first before moving on to tougher problem,

Hope this helps!

  • »
    »
    8 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I did the same. But maybe the thing is I am practicing some harder level problems directly. I think I should start with the medium one.

    • »
      »
      »
      8 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeah , try medium level problems first.

      Start with combinations, permutations, next permutations, etc.

      Eventually when you feel like you have a good depth in it, then move on to harder problems.

  • »
    »
    8 days ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Strongly disagree by this advice, recursion tree is what makes recursion confusion to many. Don't try to understand recursion by drawing recursion tree and "going deeper" into the recursion, you'll just confuse yourself more!

    Instead try to "break" problems into smaller problems, like in knapsack, if your weight is $$$W$$$ and you take the last item in your bag (say cost of last item is $$$c[n]$$$), then rest of the problem is exactly like knapsack problem, except the fact, that your set of available items is $$$[1,2...n-1]$$$ and weight of knapsack is $$$W-w[n]$$$. Clearly, rest of the problem is again the EXACT same problem, except with smaller set of "available items" and "smaller bag". It's so obvious and intuitive!

    • »
      »
      »
      8 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I think this will help. Thanks for the suggestions.

»
8 days ago, # |
  Vote: I like it +3 Vote: I do not like it

Utkarsh Gupta's explanation Try this video once. Also, try to code some things yourself. It will help you get that "aha" feel for recursion.

  • »
    »
    8 days ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Ok.. I'll do the same.. Thanks for the suggestions.

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I recommend playing around with some recursive functions — it will help you understand recursion in order to master it