我在 php 中有 dijkstra 算法,为了添加边缘,我必须编写:
$g= new Graph();
$g->addedge("b", "c", 8);
$g->addedge("b", "e", 2);
我想从我的数据库中添加所有边。我写过:
$result= mysql_query("SELECT Start,End,Distance FROM test_stations");
while($row = mysql_fetch_array($result)){
$g->addedge($row['Start'],$row['End'],$row['Distance']);
$g->addedge($row['End'],$row['Start'],$row['Distance']);
}
但似乎它没有添加边缘。当我尝试运行它时,它会打印出: Undefined variable: nodes 。如果我通过手动添加边缘来运行程序,它会完美运行。有人可以帮助我吗?
我对节点和边缘有这个功能。
public $nodes = array();
public function addedge($start, $end, $weight = 0) {
if (!isset($this->nodes[$start])) {
$this->nodes[$start] = array();
}
array_push($this->nodes[$start], new Edge($start, $end, $weight));
}