小编ser*_*ten的帖子

在深度优先搜索中实现显式堆栈

我有一个特别大的图形,因为它使用了过多的内存,因此使用递归几乎不可能遍历.

下面是我的深度优先函数,使用递归:

public function find_all_paths($start, $path)
{
    $path[] = $start;
    if (count($path)==25) /* Only want a path of maximum 25 vertices*/ {
        $this->stacks[] = $path;
        return $path;

    }
    $paths = array();

    for($i = 0; $i < count($this->graph[$start])-1; $i++) {
        if (!in_array($this->graph[$start][$i], $path)) {
     $paths[] = $this->find_all_paths($this->graph[$start][$i], $path);

        }
    }


    return $paths;
}
Run Code Online (Sandbox Code Playgroud)

我想重写这个函数,所以它是非递归的.我假设我需要创建某种类型的队列,并使用array_shift()函数的哪个部分弹出值,以及如何确保排队的顶点被保留(以便最终路径启用$this->stacks)?

php algorithm recursion graph depth-first-search

7
推荐指数
1
解决办法
1576
查看次数

标签 统计

algorithm ×1

depth-first-search ×1

graph ×1

php ×1

recursion ×1