可以回溯尾递归算法转换为迭代?

编程入门 行业动态 更新时间:2024-10-12 03:24:08
本文介绍了可以回溯尾递归算法转换为迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

让我们的骑士之旅的问题。这可以转换为迭代?什么是confunsing我是回溯的一部分。我如何走回头路在一个循环?我一定要一定要用一个堆栈数据结构实现回溯,当我去从递归迭代?

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?

我问这里有更好的方式了这个问题:Can通过codeA的实用性和迭代,而不是递归回溯的例子有人形容?

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

推荐答案

没有,那是不可能的。

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.

同时,回溯是递归的固有属性。如果你有回溯,你有递归。正如你可能知道,一类算法,使简单的真正转化为迭代尾递归算法。但回溯的presence立即意味着你的递归是不是尾递归。

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.

更多推荐

可以回溯尾递归算法转换为迭代?

本文发布于:2023-11-30 14:29:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   转换为   算法   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!