Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there ever a time you would not use recursion? [closed]

Tags:

java

recursion

I have a problem for a university lab;

Write a short program that outputs all possible strings formed by using characters ‘c’, ‘a’, ‘r’, ‘b’, ‘o’, and ‘n’ exactly once.

It seems to be a common interview question and well documented.

So I've coded it with Java using a recursive method which wasn't too hard, when or why would you choose not to use recursion and what would be the easiest way of doing it?

I started to code a counter that would count down on base 6, the output would then reference char's and print the string.

Thanks,

like image 675
LewisMc Avatar asked Sep 23 '10 06:09

LewisMc


2 Answers

Yes, there are plenty of times I would not use recursion. Recursion is not free, it has a cost in stack space and that can often be a much more limited resource than some others. There's also a time cost, however small, in setting up and tearing down stack frames.

By way of example, the much vaunted factorial function is one where I would probably opt for an iterative approach where the numbers were large. Calculating 10000! with the Python:

def factorial (n):
    if n = 1: return 1
    return n * factorial (n-1)

will attempt to use a whopping 10,000 stack frames (though Python will protect you against this). The equivalent iterative solution:

def factorial (n):
    r = 1
    while n > 1:
        r = r * n
        n = n - 1
    return r

will use just the one stack frame and precious little else.

It's true that recursive solutions are often more elegant code but you have to temper that with the limitations of your environment.

Your carbon example is one where I would actually use recursion since:

  • it uses at most six stack frames (one per character in the string); and
  • it's relatively elegant, at least much more so than six nested loops and huge equality checks.

For example the following Python code does the trick:

def recur (str, pref = ""):
    # Terminating condition.

    if str == "":
        print pref
        return

    # Rotate string so all letters get a chance to be first.

    for i in range (len (str)):
        recur (str[1:], pref + str[:1])
        str = str[1:] + str[:1]

recur ("abc")

producing:

abc
acb
bca
bac
cab
cba

Of course, if your string can be 10K long, I'd rethink it, since that would involve a lot more stack levels but, provided you keep in low enough, recursion is a viable solution.

like image 199
paxdiablo Avatar answered Nov 15 '22 20:11

paxdiablo


Use recursion when your data is inherently hierarchical/nested. Use iteration when your data is linear/flat.

In your case, there is a natural ordering you can impose on the combinations, so you can treat the data as linear, but if you view it as a tree you end up with the recursive approach.

If the structure of your algorithm reflects the structure of the underlying problem, you end up with simpler code that is easier to understand. Don't use recursion just because your CS201 professor thought it was So! Cool!

like image 25
Alex Feinman Avatar answered Nov 15 '22 18:11

Alex Feinman