在浏览代码时,它为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;
}
....
?>
我得到的输出是,我需要在代码后面找到使用它的长度,请不要建议任何其他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