Can a backtracking tail recursive algorithm be converted to iteration?

六眼飞鱼酱① 提交于 2020-01-12 01:59:16

问题


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?


回答1:


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.




回答2:


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.




回答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).



来源:https://stackoverflow.com/questions/13782136/can-a-backtracking-tail-recursive-algorithm-be-converted-to-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!