Recursion Return

Asked

Viewed 46 times

0

I’m trying to create an application where I put source, destination and cost data into an array and the user must enter the source and destination so that the system can then scan that array and return the path at the lowest cost. After several attempts, I decided to use recursiveness to bring this answer. I search through a foreach the paths and all accessible path that ends up giving in the destination I play in a variable to then store in another array so that finally I can make the comparisons. follows code:

    public function calc($origem, $destino, $autonomia, $litro_combustivel) {

    $rotas = array (
        '0' => array (
            'ponto_inicial' => 'A',
            'ponto_final' => 'B',
            'distancia' => 10
        ),
        '1' => array (
            'ponto_inicial' => 'B',
            'ponto_final' => 'D',
            'distancia' => 15
        ),
        '2' => array (
            'ponto_inicial' => 'A',
            'ponto_final' => 'C',
            'distancia' => 20
        ),
        '3' => array (
            'ponto_inicial' => 'C',
            'ponto_final' => 'D',
            'distancia' => 30
        ),
        '4' => array (
            'ponto_inicial' => 'B',
            'ponto_final' => 'E',
            'distancia' => 50
        ),
        '5' => array (
            'ponto_inicial' => 'D',
            'ponto_final' => 'E',
            'distancia' => 30
        )
    );

    $melhores_rotas = array();
    $i = 1;
    $caminho = $origem;
    $primeira_origem = $origem;
    $origem = null; 
    $status = 0;
    $anterior = null;

  /*A B 10
    B D 15
    A C 20
    C D 30
    B E 50
    D E 30*/

    //FUNÇÃO RECURSIVA-------------------------------------------------------------------------
    function calculaRota($rotas, $origem, $destino, $caminho, $melhores_rotas, $primeira_origem, $i, $status, $anterior) {

        if ($origem != $primeira_origem) {
            if ($status == 0) {
                $origem = $primeira_origem;
                $status = 1;
            }

            foreach ($rotas as $r) {

                if ($origem == $r['ponto_inicial']) {
                    $caminho = $caminho . $r['ponto_final'];
                    $anterior = $origem;
                    $origem = $r['ponto_final'];

                    if ($origem == $destino || $origem == "E") {

                        array_push($melhores_rotas, $caminho);
                        $caminho = substr($caminho, 0, -1);
                        $origem = $r['ponto_inicial'];

                    } else {
                        calculaRota($rotas, $origem, $destino, $caminho, $melhores_rotas, $primeira_origem, $i, $status, $anterior);

                        $origem = $r['ponto_inicial'];
                        $caminho = substr($caminho, 0, -1);
                    }

                }

            }

        } else {
            return $melhores_rotas;
        }


        if ($origem == $primeira_origem) {
            return $melhores_rotas;
        }


    }
    //------------------------------------------------------------------------------------------

    $dados = calculaRota($rotas, $origem, $destino, $caminho, $melhores_rotas, $primeira_origem, $i, $status, $anterior);

    dd($dados);


}

The problem is... I don’t get anything inside the $data variable and apparently from what I can tell it’s overwriting the content I’m adding inside the $bestes_routes array. Does anyone have any of how I can do these operations without replacing what’s in the array?

  • Luke the method array_push() that you are already using does not overwrite the contents of the source array, it adds the variables passed as parameters at the end of this array, and does not overwrite it.

  • If your idea is just to find the shortest route, in case the best route I suggest researching the Dijkstra algorithm, or Bellman Ford’s, or even Floyd-Warshall’s. These algorithms will seek the shortest path from point A to point B

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.