0

グラフ データ構造を開発していますが、問題があります。

<?php  
class Graph
{
    var $graph_arr = array();

    function Graph()
    {
        $this->graph_arr = array();
        //initialization of nodes, mythical for now
        $n = new Node("A", array("B", "C"));
        $this->graph_arr[] = $n;
        $n = new Node("B", array("A", "D"));
        $this->graph_arr[] = $n;
        $n = new Node("C", array("A", "E", "F"));
        $this->graph_arr[] =$n; 
        $n = new Node("D", array("B"));
        $this->graph_arr[] = $n;
        $n = new Node("E", array("C"));
        $this->graph_arr[] = $n;    
        $n = new Node("F", array("C"));
        $this->graph_arr[] = $n;
    }
};

class Node
{
    var $node_name;
    var $adjacent_nodes;
    var $is_visited;

    function Node($node_name, $adjacent_nodes)
    {
        $this->node_name = $node_name;
        $this->adjacent_nodes = $adjacent_nodes;
    }
    /** returns array of adjacent nodes **/

    function getAdjacentNodes()
    {
        return $this->adjacent_nodes;
    }

    function getNodeName()
    {
        return $this->node_name;
    }

    function isVisited()
    {
        return $this->is_visited;
    }

    function setVisited()
    {
        $this->is_visited = true;
    }
};

?>

Graph オブジェクトを作成すると、配列のサイズは 0 になります。また、新しいノードを追加できません。

4

1 に答える 1

1

次のように Graph クラスを変更してみてください。

class Graph
{
  var $graph_arr = array();

  function __construct() {
     $this->graph_arr = array();

   //initialization of nodes, mythical for now
   $n = new Node("A", array("B", "C"));
   $this->graph_arr[] = $n;
   $n = new Node("B", array("A", "D"));
   $this->graph_arr[] = $n;
   $n = new Node("C", array("A", "E", "F"));
   $this->graph_arr[] =$n; 
   $n = new Node("D", array("B"));
   $this->graph_arr[] = $n;
   $n = new Node("E", array("C"));
   $this->graph_arr[] = $n;    
   $n = new Node("F", array("C"));
   $this->graph_arr[] = $n;   
 }
}  
于 2012-11-25T14:47:08.813 に答える