поиск с ограниченной глубиной — Не дает мне правильный результат из-за глубины, я думаю?

/*for example using a graph of this nature
----1-----0
/\
---2----3----1
/\   /\
--4--5--6--7---2

at level 2 it will return 1,2,4,5,3 instead of 1,2,4,5,3,6,7

this is the main function to loop through the graph*/
public function set_data()
{
$this->data_type();
while(count($this->_open) > 0 && $this->_state !== 'success')
{
$depth   = $this->_depth;
$current = array_pop($this->_open);

if($current == $this->_goal){
$this->_state = 'success';
}
if ($depth < $this->_limit) {
$this->set_children($current, $depth);
}
else{
$this->_state = 'cutoff';
}
array_push($this->_close, $current);
}
}
// the function to generate the children
public function set_children($current, $depth)
{
if(isset($this->_graph["$current"])){
foreach ($this->_graph["$current"] as $value)
{
//implementing stack - LIFO
array_push($this->_open, array_pop($this->_graph["$current"]));
}
$depth = $depth+1;
$this->_depth = $depth;
}
}

-1

Решение

Через некоторое время я наконец-то нашел решение. Я сделал $ глубину массивом для хранения глубины соответствующего узла.

  • Первая инициализация

    $ this -> _ deep = [0];

  • Во-вторых, текущая глубина текущего узла

    $ глубина = массив_поп ($ это -> _ глубина);

  • В-третьих, создайте глубину для каждого дочернего узла

    array_push ($ this -> _ глубина, $ глубина ++);
    $ Depth—;

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector