-1
/*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;
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Tunde
  • 1
  • 3

1 Answers1

0

After a long while I finally got a solution. I made the $depth an array to hold the depth of corresponding node.

  • First initialization

    $this->_depth = [0];

  • Second, current depth of the current node

    $depth = array_pop($this->_depth);

  • Third, create the depth for each child node

    array_push($this->_depth, $depth++); $depth--;

Tunde
  • 1
  • 3