views:

1095

answers:

8

Possible Duplicates:
Is there a problem that has only a recursive solution?
Can every recursion be converted into iteration?
“Necessary” Uses of Recursion in Imperative Languages

Is there a problem that has only a recursive solution, that is, a problem that has a recursive solution, but an iterative solution has yet to be found, or better yet, has proven to be non-existing (obviously, this is not a tail-recursion)?

+4  A: 

All non np-complete problems can be solved with just sequence, decision, and iteration. Recursion should not be required, though it usually greatly simplifies the problem.

Matthew Vines
So can the NP-complete ones. It just takes longer.
Ian
+16  A: 

replace function calls with pushing arguments onto a stack, and returns with popping off the stack, and you've eliminated recursion.

Edit: in response to "using a stack does not decrease space costs"

If a recursive algorithm can run in constant space, it can be written in a tail-recursive manner. if it is written in tail-recursive format, then any decent compiler can collapse the stack. However, this means the "convert function calls to explicit stack-pushes" method also takes constant space as well. As an example, lets take factorial.

factorial:

def fact_rec(n):
    ' Textbook Factorial function '
    if n < 2:  return 1
    else:      return n * f(n-1)


def f(n, product=1):
    ' Tail-recursive factorial function '
    if n < 2: return product
    else:     return f(n-1, product*n)

def f(n):
    ' explicit stack -- otherwise same as tail-recursive function '
    stack, product = [n], 1
    while len(stack):
        n = stack.pop()
        if n < 2: pass 
        else:
            stack.append(n-1)
            product *= n
    return product

because the stack.pop() follows the stack.append() in the loop, the stack never has more than one item in it, and so it fulfills the constant-space requirement. if you imagine using a temp variable instead of a 1-length stack, it becomes your standard iterative-factorial algorithm.

of course, there are recursive functions that can't be written in tail-recursive format. You can still convert to iterative format with some kind of stack, but I'd be surprised if there were any guarantees on space-complexity.

Jimmy
That is not recursion elimination. When one replace recursive function with non-recursive one it is supposed to use limited memory instead of theoretically unlimited stack.
Kirill V. Lyadvinsky
"Guaranteed to use a finite amount of memory" is not the definition of a nonrecursive function. It might be a desirable property, but it's not a definitional requirement. An iterative algorithm with a memory leak will use a potentially unlimited amount of memory, but nobody would argue that alone makes the algorithm recursive.
Chuck
@jia3ep: this is recursion elimination. in fact, it already has a tangible benefit in some situations by moving the load from the operating system's call stack (which in many architectures has a limited space) into the heap, and avoiding the overhead of a function call.
Jimmy
This doesn't change the idea of the recursion in my opinion, you can as well implant a virtual machine in some non-stack way, and run your recursion code on top of it, I was thinking more in the spirit of a different algorithm. nice idea nonetheless.
Liran Orevi
@Jimmy, Hardware stack is much faster then using heap, so stack emulation has no obvious advantage.
Kirill V. Lyadvinsky
+3  A: 

It comes down to how many lines of code is it going to take to solve the problem...

List all the files on your C:\ using recursion and then without. Sure you can do it both ways - but one way is going to be a lot simpler to understand and debug.

PSU_Kardi
Search in a tree can be Depth-first or Breadth-first. The former is naturally translated with recursion, but the later is more natural to implement with an iterative method.
David Rodríguez - dribeas
A: 

No. Recursion is nothing more than a stack and you can achieve the same results by implementing a stack explicitly.

That may not be an especially satisfying answer, but you'd have to ask a much more specific question to get a better answer. For example, theory dictates that in the levels of computing there is quite a difference in the range of problems that can be solved if you have a while loop vs. only having (traditional) for loops.

I put "traditional" in there because they really mean loops that iterate a certain number of times whereas C style for (...;...;...) loops are while loops in disguise.

George Phillips
+6  A: 

In programming, recursion is really a special case of iteration - one where you use the call stack as a special means of storing state. You can rewrite any recursive method to be an iterative one. It may be more complicated or less elegant, but it's equivalent.

In mathematics, there are certain problems that require recursive techniques to arrive at an answer - some examples are finding roots (Newton's Method), computing primes, graph optimization, etc. However, even here, there's just a question of how you differentiate between the terms "iteration" and "recursion".

EDIT: As others have pointed out, there exist many functions whose definition is recursive - ex. the Ackermann function. However, this does not mean that they cannot be computed using iterative constructs - so long as you have a turing complete operation set and unlimited memory.

LBushkin
+13  A: 

The Ackermann function cannot be expressed without recursion

LB
+1. Nice. I think this is about the only real answer on here (well, besides Marc's comment).
Erich Mirabal
Counter: "The Ackermann function can also be expressed nonrecursively using Conway chained arrow notation" (see wikipedia)
Marc Gravell
and "hyper operators" and "the indexed version of Knuth's up-arrow notation"
Marc Gravell
Marc stole my comment. Though thinking about it, is that really a counter? Or just a "shortcut" to recursion?
GMan
I thought it was just a shortcut to recursion but still recursion...
LB
Interesting. The Ackermann function is not a primitive recursive function: http://en.wikipedia.org/wiki/Primitive_recursive_function. According to this page, a language with basic arithemetic, comparison, and bounded for-loops can't express this type of function. But if you add in WHILE or GOTO you're Turing-complete again.
Erika
see my response at http://stackoverflow.com/questions/1094679/is-there-a-problem-that-has-only-a-recursive-solution/1095538#1095538
Jimmy
Weisstein, Eric W. "Primitive Recursive Function." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/PrimitiveRecursiveFunction.html
Liran Orevi
I think it's important to differentiate between problems whose "mathematical" definition involves a recursive form ... vs. whose computation can only be achieved recursively. You can compute the Ackermann function using non-recursive programming form - however you _will_ to store state for each iteration.
LBushkin
@LBushkin I fully agree
LB
+7  A: 

You can define a Turing Machine without recursion (right?) So recursion isn't required for a language to be Turing-complete.

Erika
in computational linguistics, the Turing Machine is above the Stack based automatas because it can emulate those (the strip can be used as a stack)
fortran
+3  A: 

In Response to the Ackermann function answer, this is a pretty straightforward convert-the-call-stack-into-a-real-stack problem. This also shows one benefit of the iterative version.

on my platform (Python 3.1rc2/Vista32) the iterative version calculates ack(3,7) = 1021 fine, while the recursive version stackoverflows. NB: it didn't stackoverflow on python 2.6.2/Vista64 on a different machine, so it seems to be rather platform-dependant,

(Community wiki because this is really a comment to another answer [if only comments supported code formatting .... ])

def ack(m,n):
  s = [m]
  while len(s):
     m = s.pop()
     if m == 0:
        n += 1 
     elif n == 0:
        s.append(m-1)
        n = 1
     else:
        s.append(m-1)
        s.append(m)
        n -= 1
  return n
Jimmy
I assume the 'append' is really a 'push'?
Erich Mirabal
well, this is a list in python heh.
Jimmy