19

Let's take the Knight Tour problem. Can that be converted to iteration? What is confunsing me is the backtracking part. How do I backtrack in a loop? Do I have to necessarily use a stack data-structure to implement backtracking when I go from recursion to iteration?


I asked this question in a better way here: Can someone describe through code a practical example of backtracking with iteration instead of recursion?

Community
  • 1
  • 1
chrisapotek
  • 6,007
  • 14
  • 51
  • 85

3 Answers3

31

No, it can't be.

All recursive algorithms can be implemented iteratively, by "emulating" recursion with an explicit LIFO data structure. But that does not change the algorithm itself, i.e. the algorithm remains recursive, not iterative.

Meanwhile, backtracking is an inherent property of recursion. If you have backtracking, you have recursion. As you probably know, a class of algorithms that allows straightforward genuine conversion to iteration is tail-recursive algorithms. But the presence of backtracking immediately means that your recursion is not tail-recursion.

What you can do is to attempt to invent an algorithm that does not require backtracking. That, of course, will be a completely different algorithm, not a conversion of the original recursive algorithm to iterative form.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Few people are smart enough to know that stuff. From that small group, few people would know how to put it in words in a simple way. You are one of them. THANKS! Can I say that backtracking using iteration + stack data-structure is also not possible, in other words BACKTRACKING => RECURSION. – chrisapotek Dec 08 '12 at 23:26
  • 1
    Doesn't the following answer show a tail-recursive and an iterative version of a backtracking algorithm? https://stackoverflow.com/questions/20514267/n-queen-backtracking-in-python-how-to-return-solutions-instead-of-printing-them/20531704#20531704 – user1767774 Sep 24 '16 at 20:11
11

All recursive algorithms can be converted to iterative algorithms, and vice-versa. This is a direct result of the Church-Turing thesis.

It might not always be obvious (or trivial), but any algorithm can be expressed as a recursive or as an iterative process; for the general case this question has been answered before.

As for the how, there are several techniques that can be applied for going from one style to the other, for instance take a look at this answer, or for a more detailed discussion read this article which explains how to use stacks to eliminate recursion.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 10
    The "direct result of Church-Turing thesis" is that any recursive *algorithm* allows iterative *implementation*. It does not in any way claim that any recursive *algorithm* can be converted to iterative *algorithm*. Moreover, the correct answer is no, it is generally not possible. Understanding the difference between an *algorithm* and its *implementation* is the key moment here. Without it it simply turns into a useless scholastic statement. – AnT stands with Russia Dec 08 '12 at 22:18
3

Basically, every recursion can be implemented as a loop + stack, because that what it basically basically implemented on the machine (hardware) level - just a bunch of branches and a stack for storing return addresses and arguments.

Have a loop that repeat while the condition is not met, and instead of recursive invokation - just push the parameters for the next iterations (and possibly the last state) to the stack, and go back to the start point of the loop.


EDIT: (since it is clear you are talking about tail-recursive backtracking, and not a simple recursion):

From wikipedia: In computer science, a tail call is a subroutine call that happens inside another procedure as its final action. As far as I know, a function with multiple recursive calls - is by definition not tail recursion, and since backtracking algorithms do have more then one call, they are not "tail recursive".

Also note - that a program that have only loops and a constant space can be translated to a second program P' that runs in polynomial time (Since there are at most 2^CONST states, which is basically CONST', and verifying each of these is done in polynomial time - so total of CONST'*p(n) time,. which is still polynomial), so unless P=NP, it is impossible since it will allow us to solve SAT in polynomial time by translating the backtracking solution to a loop based polynomial one. (And I believe a further reduction from HP is feasible to show it is impossible anyway).

amit
  • 175,853
  • 27
  • 231
  • 333
  • I am more interested in the HOW. – chrisapotek Dec 08 '12 at 21:36
  • @chrisapotek: The second part of the loop answers how it is done - the exact details are implementation dependent - but this is the general guide lines - just push to a stack the next states to be searched, (In knight tour problem, from each step insert all possible new steps to search, and return to the start point of the loop) – amit Dec 08 '12 at 21:44
  • @chrisapotek: I think I first misunderstood your true intent. Is the edit answering your question better? – amit Dec 08 '12 at 22:27