如何在递归函数中查找返回类型的长度

时间:2014-05-13 23:51:08

标签: php algorithm depth-first-search

在浏览代码时,它为DFS编写了类似于我在下面显示的内容。 我想获得每次遍历的长度,如何使用静态变量来获取返回值,

<?php 

.....
.....

    while loop start


    {
    //
    // some code
    //

    $deletebranch = json_encode($todel);
    echo $deletebranch;

    foreach ($abc as $key => $xyz)
    {
    ///
    /// code for finding $letter, $array_node 
    ///
    $finaldepthsearchgraph[$letter]['vertex'] = $letter;
    $finaldepthsearchgraph[$letter]['visited'] = false;
    $finaldepthsearchgraph[$letter]['letter'] = $letter;
    $finaldepthsearchgraph[$letter]['neighbours'] = $array_node;
    ///
    ///
    }


    depthFirstSearch($finaldepthsearchgraph['A'], $finaldepthsearchgraph);

    } // end of while loop


        function depthFirstSearch($vertex, $list )
        {

            if (!$vertex['visited']) {
                echo $vertex['letter'];
                //var_dump($list);
                 // output on screen
                // mark vertex as visited
                $list[$vertex['vertex']]['visited'] = true;
                foreach ($vertex['neighbours'] as $neighbour) {
                    // Watch neighbours, which were not visited yet
                    if (!$list[$neighbour]['visited']) {
                        // going through neighbour-vertexes
                        $list = depthFirstSearch(
                            $list[$neighbour], 
                            $list
                        );
                    }

                }
            }


            return $list;

        }


....
?>

ANSWER

我得到的输出是,我需要在代码后面找到使用它的长度,请不要建议任何其他DFS方法,我只需要执行此操作。

["A","B"]

ACBFGHL  

["B","C"]

ABCFGHL

["A","C"]

ABCFGHL

["C","F"]

ABC

["F","G"]

ABCF

["G","H"]

ABCFGLH

["H","L"]

ABCFGHL

["G","L"]

ABCFGHL

0 个答案:

没有答案