Convert function from recursion to iteration

后端 未结 4 723
耶瑟儿~
耶瑟儿~ 2021-01-01 06:19

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

相关标签:
4条回答
  • 2021-01-01 06:56

    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
    
    0 讨论(0)
  • 2021-01-01 06:56

    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.

    0 讨论(0)
  • 2021-01-01 06:58

    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().

    0 讨论(0)
  • 2021-01-01 07:02

    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.

    0 讨论(0)
提交回复
热议问题