1

Sometimes when you're coding you're on a problem which can be solved with some recursive method. What is for you the best way to detect when the recursion is a good way to solve a problem and how to implement it efficiently?

I mean, how do you visualize it? How do you convince yourself that it works?

gnat
20.5k29 gold badges117 silver badges308 bronze badges
asked Aug 16, 2016 at 8:18
4
  • 11
    obligatory reference to How to decide how to build a recursive function Commented Aug 16, 2016 at 8:25
  • 2
    Possible duplicate of Recursion or while loops Commented Aug 16, 2016 at 8:38
  • 1
    @gnat: O_o no break condition ........ Commented Aug 16, 2016 at 8:38
  • 6
    Is Programmers.StackExchange tail recursive? Or did gnat cause a StackOverflow? Commented Aug 16, 2016 at 9:12

2 Answers 2

4

There is no one answer to wether you are better of with a recursion, loop, multiple function calls, ...

But there are some hints:

  • recursion depth can be limited by the size of the call stack (depends on the language). A value that pops into mind is a max of 1024.

  • if the data structure is recursive, for instance a binary tree, recursion is your friend.

  • put your problem into words: "and then the same for the next item" => iteration; "and then the same for the rest" => recursion.

  • if you need to store intermediate values, it is easier done with iteration.

  • if your problem is inherently self-similar, go with recursion.

Final remark: All problems can be solved by both iteration and recursion. It is a good exercise to implement both. Observe, which took you longer, which runs faster, which uses less memory, which is parallelisable? And most importantly: Which was more fun to implement?

answered Aug 16, 2016 at 8:49
3
  • 3
    Point 1 is (mostly) invalid for tail recursive languages. Commented Aug 16, 2016 at 9:15
  • 3
    "Most problems can be solved by both iteration and recursion." – All of them, actually. Recursion and Iteration are provably equivalent. Iteration is trivially equivalent to tail-recursion. The mapping from general recursion to iteration is not 1:1 like the mapping from iteration to tail-recursion, but it's doable: worst-case, you have to carry around a stack to emulate the recursive call stack. Commented Aug 16, 2016 at 10:09
  • @JörgWMittag; Thank you for pointing that out. I will edit the answer. Commented Aug 16, 2016 at 10:30
-1

Depends on the problem. ! The factorial example is pretty artificial; it’s so simple that it really doesn’t matter which version you choose.  Many ADTs (e.g., trees) are simpler & more natural if methods are implemented recursively.  Recursive isn’t always better. ! Solve a large problem by breaking it up into smaller and smaller pieces until you can solve it; combine the results. Recursion is much better than iteration for problems that can be broken down into multiple, smaller pieces. Precisely, in divide and conquer method using recursion can reduce your problem size at every step and would take less time than a naive iterative approach. Recursion is simpler to implement, and it is usually more 'elegant' than iterative solutions.

answered Aug 18, 2016 at 8:50
1
  • this post is rather hard to read (wall of text). Would you mind editing it into a better shape? Commented Aug 18, 2016 at 9:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.