|
|
|
ใครมี มีตัวอย่าง code DepthFirstSearch ภาษาphp+++++++ขอดูเป็นตัวอย่างหน่อยคาบ |
|
|
|
|
|
|
|
สร้าง tree ก่อน
|
|
|
|
|
Date :
2010-10-05 22:46:01 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<?php
class state_space_graph{
var $connected;
var $start_state;
var $goal_state;
function connectedList($state,$connectedListArray){
$a = array();
for ($i=0;$i<count($connectedListArray);$i++){
$cla=$connectedListArray[$i];
if ($state==$cla{0} && $state!=$cla{1}){
if ( !in_array($cla{1},$a)){
$a[] = $cla{1};
}
} elseif ($state==$cla{1} && $sate!=$cla{0}){
if ( !in_array($cla{0},$a)){
$a[] = $cla{0};
}
}
}
return $a;
}
function state_space_graph($nodeList,$connectedList,$start,$goal){
$a = explode(',',$nodeList);
$b = explode(',',$connectedList);
$this->start_state = $start;
$this->goal_state = $goal;
for ($i=0;$i<count($a);$i++){
$this->connected[$a[$i]] = $this->connectedList($a[$i],$b);
}
}
function has_children($cs,&$sl,&$nsl,&$de,&$children){
$children = '';
for ($i=0;$i<count($this->connected[$cs]);$i++){
if ( false===strpos($sl,$this->connected[$cs][$i]) &&
false===strpos($nsl,$this->connected[$cs][$i]) &&
false===strpos($de,$this->connected[$cs][$i]))
{
$children = $children.$this->connected[$cs][$i];
}
}
return strlen($children);
}
function remove_front(&$list){
$list = substr($list,1);
}
function add_front(&$list,$cs){
$list = $cs.$list;
}
function define_empty(&$list){
$list = '';
}
function first_element(&$list){
return substr($list,0,1);
}
function path($list){
if (strlen($list)>0){
$s = $list{0};
}else{
$s = '';
}
for ($i=1;$i<strlen($list);$i++){
$s = $s.','.$list{$i};
}
return $s;
}
function pathrev($list){
if (strlen($list)>0){
$s = $list{0};
}else{
$s = '';
}
for ($i=1;$i<strlen($list);$i++){
$s = $list{$i}.','.$s;
}
return $s;
}
function backtrack(){
$cs = $this->start_state;
$goal = $this->goal_state;
$this->add_front($sl,$cs);
$this->add_front($nsl,$cs);
$this->define_empty($de);
echo '<table border=1 cellpadding=0 cellspacing=0 width=500>'
.'<tr bgcolor=silver><th>CS'.'</th><th>SL'.'</th><th>NSL'.'</th><th>DE'.'</th></tr>';
while (strlen($nsl) != 0) {
echo '<tr align=right bgcolor=gainsboro><td> '.$cs
.'</td><td> '.$this->path($sl)
.'</td><td> '.$this->path($nsl)
.'</td><td> '.$this->path($de).'</td></tr>';
if ($cs==$goal) break;
if ( !$this->has_children($cs,$sl,$nsl,$de,$children)){
//echo '<tr><td>no have</td></tr>';
while (strlen($sl) != 0 && $cs==$this->first_element($sl)){
$this->add_front($de,$cs);
$this->remove_front($sl);
$this->remove_front($nsl);
$cs = $this->first_element($nsl);
}
$this->add_front($sl,$cs);
} else{
//echo '<tr><td>have</td></tr>';
$this->add_front($nsl,$children);
$cs = $this->first_element($nsl);
$this->add_front($sl,$cs);
}
}
echo '</table>';
return ($cs==$goal?$sl:false);
}/* bracktrack function */
}
echo '<html><head><title>state space search</title></head><body>';
$g = new state_space_graph('A,B,C,D,E,F,G,H,I,J','AB,AC,AD,BE,BF,EH,EI,FJ,CF,CG','A','G');
$solution_path = $g->backtrack();
echo 'start = '.$g->start_state.'<br>';
echo 'goal = '.$g->goal_state.'<br>';
echo ($solution_path?'solution path is <b>'.$g->pathrev($solution_path).'.</b>':'don\'t found goal.');
echo '</body></html>';
?>
|
|
|
|
|
Date :
2010-10-05 23:12:25 |
By :
xyz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|