将函数从递归转换为迭代

发布于 2024-08-16 09:19:37 字数 835 浏览 4 评论 0原文

我写的这个函数非常慢,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城泪 2024-08-23 09:19:37

我不会转换您的代码,但您可以通过创建堆栈将递归函数转换为迭代函数:

$stack= array();

并且不要调用 $this->findroute(),而是将参数推送到这个堆栈:

$stack[] = array($i, $distanceSoFar + $this->distanceArray[$curLoc][$i], $expectedValue);

现在将函数中的所有内容基本上都包围到一个 while 循环中,在启动堆栈后耗尽堆栈:

while ($stack) { 
    // Do stuff you already do in your function here

I'm not going to convert your code, but you can convert a recusive function into an iterative one by creating a stack:

$stack= array();

And instead of invoking $this->findroute(), push your parameters onto this stack:

$stack[] = array($i, $distanceSoFar + $this->distanceArray[$curLoc][$i], $expectedValue);

Now surround basically everything in your function into a while loop draining the stack after having primed it:

while ($stack) { 
    // Do stuff you already do in your function here
虐人心 2024-08-23 09:19:37

您可以通过使用堆栈来存储当前状态,将递归函数转换为迭代函数。查看 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() and array_pop().

失与倦" 2024-08-23 09:19:37

乍一看,我不认为递归是你的问题,是的,它在 PHP 中很慢,但是看起来你对值的处理超出了你的需要,将值放入一个堆栈或多个堆栈中并处理它们,可能会很好。

自定义排序函数总是帮助我解决此类问题。

function sort_by_visited($x,$y)
{
   return ($this->locationsVisited[$x] > $this->locationsVisited[$y]) ? -1 : 1;
}

uasort($locationsVisited,'sort_by_visited');

这将优先考虑堆栈顶部所有未访问的位置。

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.

function sort_by_visited($x,$y)
{
   return ($this->locationsVisited[$x] > $this->locationsVisited[$y]) ? -1 : 1;
}

uasort($locationsVisited,'sort_by_visited');

This will prioritize all not visited locations at the top of the stack.

挽心 2024-08-23 09:19:37

这看起来就像您试图找到遍历图中一系列节点的最佳路线。

我猜你没有学过计算机科学,因为“旅行推销员”问题是人工智能的原型。当然,因此,它有自己的维基百科页面:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文