Reputation: 383
I'm developing a graph data structure, but I'm with a problem:
<?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;
}
};
?>
Well when I create the Graph object, the size of the array is 0. And I can't add new Nodes.
Upvotes: 0
Views: 109
Reputation: 175
Try change your Graph class like this:
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;
}
}
Upvotes: 1