将函数从递归转换为迭代
我写的这个函数非常慢,因为 php 不能很好地处理递归。我正在尝试将其转换为 while 循环,但我无法理解如何做到这一点。
谁能给我一些建议吗?
public function findRoute($curLoc, $distanceSoFar, $expectedValue) {
$this->locationsVisited[$curLoc] = true;
$expectedValue += $this->locationsArray[$curLoc]*$distanceSoFar;
$at_end = true;
for($i = 1; $i < $this->numLocations; $i++) {
if($this->locationsVisited[$i] == false) {
$at_end = false;
if($expectedValue < $this->bestEV)
$this->findRoute($i, $distanceSoFar + $this->distanceArray[$curLoc][$i], $expectedValue);
}
}
$this->locationsVisited[$curLoc] = false;
if($at_end) {
if($expectedValue < $this->bestEV) {
$this->bestEV = $expectedValue;
}
}
}
I have this function that I wrote that is abysmally slow since php does not handle recursion well. I am trying to convert it to a while loop, but am having trouble wrapping my head around how to do it.
Can anyone give me some tips?
public function findRoute($curLoc, $distanceSoFar, $expectedValue) {
$this->locationsVisited[$curLoc] = true;
$expectedValue += $this->locationsArray[$curLoc]*$distanceSoFar;
$at_end = true;
for($i = 1; $i < $this->numLocations; $i++) {
if($this->locationsVisited[$i] == false) {
$at_end = false;
if($expectedValue < $this->bestEV)
$this->findRoute($i, $distanceSoFar + $this->distanceArray[$curLoc][$i], $expectedValue);
}
}
$this->locationsVisited[$curLoc] = false;
if($at_end) {
if($expectedValue < $this->bestEV) {
$this->bestEV = $expectedValue;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不会转换您的代码,但您可以通过创建堆栈将递归函数转换为迭代函数:
并且不要调用
$this->findroute()
,而是将参数推送到这个堆栈:现在将函数中的所有内容基本上都包围到一个 while 循环中,在启动堆栈后耗尽堆栈:
I'm not going to convert your code, but you can convert a recusive function into an iterative one by creating a stack:
And instead of invoking
$this->findroute()
, push your parameters onto this stack:Now surround basically everything in your function into a while loop draining the stack after having primed it:
您可以通过使用堆栈来存储当前状态,将递归函数转换为迭代函数。查看 array_push() 和 array_pop()。
You can convert a recursive function into an iterative function by using a stack to store the current state. Look into
array_push()
andarray_pop()
.乍一看,我不认为递归是你的问题,是的,它在 PHP 中很慢,但是看起来你对值的处理超出了你的需要,将值放入一个堆栈或多个堆栈中并处理它们,可能会很好。
自定义排序函数总是帮助我解决此类问题。
这将优先考虑堆栈顶部所有未访问的位置。
At a glance I don't think recursion is your problem, yes it's slow in PHP, but It looks like your going over values more than you need to, putting the values in a stack, or several stacks and handling them, may be nice.
custom sort functions have always helped me with problems of this sort.
This will prioritize all not visited locations at the top of the stack.
这看起来就像您试图找到遍历图中一系列节点的最佳路线。
我猜你没有学过计算机科学,因为“旅行推销员”问题是人工智能的原型。当然,因此,它有自己的维基百科页面:
http://en.wikipedia.org/wiki /Travelling_salesman_problem
抱歉 - 但仅仅从递归函数切换到迭代函数并不会让它运行得更快(“php 不能很好地处理递归。” - 你能为这个断言提供参考吗)。如果您需要更快的解决方案,那么您需要查看非详尽/模糊方法。
C.
This looks like your trying to find the optimal route for traversal of a series of nodes in a graph.
I'm guessing that you've not studied Computer Science as the "Travelling Salesman" problem is an archetype of Artificial Intelligence. Of course, as such, it has its own Wikipedia page:
http://en.wikipedia.org/wiki/Travelling_salesman_problem
Sorry - but just swapping from a recursive to an iterative function isn't going to make it go any faster ("php does not handle recursion well." - can you provide reference for this assertion). If you need a faster solution then you'll need to look at non-exhaustive/fuzzy methods.
C.