2017-04-10 5 views
-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; 
    } 
} 
+0

thanks @ miken32 – Tunde

答えて

0

私はついに解決策を得ました。対応するノードの深さを保持するために、$ depthを配列にしました。

  • まず初期

    $この - > _深さ= [0]。

  • 第二に、現在のノード

    $深さ= array_pop($この - > _深さ)の現在の深さ。

  • 第三に、それぞれの子ノード

    array_push($この - > _深さ、$深さ++)のための深さを作成します。 $ depth--;