|
 |
|
อยากทราบการทำงานของโค๊ด dijkstra อยากวอนให้ผู้ที่รู้ช่วยอธิบายโค๊ดในส่วนที่มีให้หน่อยค่ะ ขอบพระคุณล่วงหน้าค่ะ |
|
 |
|
|
 |
 |
|
อยากทราบการทำงานของโค๊ด dijkstra อยากวอนให้ผู้ที่รู้ช่วยอธิบายโค๊ดในส่วนที่มีให้หน่อยค่ะ ขอบพระคุณล่วงหน้าค่ะ
ส่วนของโค๊ด-------
Code (PHP)
<?PHP
class Dijkstra {
var $visited = array();
var $distance = array();
var $previousNode = array();
var $startnode =null;
var $map = array();
var $infiniteDistance = 0;
var $numberOfNodes = 0;
var $bestPath = 0;
var $matrixWidth = 0;
function Dijkstra(&$ourMap, $infiniteDistance)
{
$this -> infiniteDistance = $infiniteDistance;
$this -> map = &$ourMap;
$this -> numberOfNodes = count($ourMap);
$this -> bestPath = 0;
}
function findShortestPath($start,$to = null)
{
$this -> startnode = $start;
for ($i=0;$i<$this -> numberOfNodes;$i++)
{
if ($i == $this -> startnode)
{
$this -> visited[$i] = true;
$this -> distance[$i] = 0;
} else {
$this -> visited[$i] = false;
$this -> distance[$i] = isset($this -> map[$this -> startnode][$i])
? $this -> map[$this -> startnode][$i]
: $this -> infiniteDistance;
}
$this -> previousNode[$i] = $this -> startnode;
}
$maxTries = $this -> numberOfNodes;
$tries = 0;
while (in_array(false,$this -> visited,true) && $tries <= $maxTries)
{
$this -> bestPath = $this->findBestPath($this->distance,array_keys($this -> visited,false,true));
if($to !== null && $this -> bestPath === $to)
{
break;
}
$this -> updateDistanceAndPrevious($this -> bestPath);
$this -> visited[$this -> bestPath] = true;
$tries++;
}
}
function findBestPath($ourDistance, $ourNodesLeft)
{
$bestPath = $this -> infiniteDistance;
$bestNode = 0;
for ($i = 0,$m=count($ourNodesLeft); $i < $m; $i++)
{
if($ourDistance[$ourNodesLeft[$i]] < $bestPath)
{
$bestPath = $ourDistance[$ourNodesLeft[$i]];
$bestNode = $ourNodesLeft[$i];
}
}
return $bestNode;
}
function updateDistanceAndPrevious($obp)
{
for ($i=0;$i<$this -> numberOfNodes;$i++)
{
if( (isset($this->map[$obp][$i])) && (!($this->map[$obp][$i] == $this->infiniteDistance) || ($this->map[$obp][$i] == 0 )) && (($this->distance[$obp] + $this->map[$obp][$i]) < $this -> distance[$i]))
{
$this -> distance[$i] = $this -> distance[$obp] + $this -> map[$obp][$i];
$this -> previousNode[$i] = $obp;
}
}
}
function printMap(&$map)
{
$placeholder = ' %' . strlen($this -> infiniteDistance) .'d';
$foo = '';
for($i=0,$im=count($map);$i<$im;$i++)
{
for ($k=0,$m=$im;$k<$m;$k++)
{
$foo.= sprintf($placeholder, isset($map[$i][$k]) ? $map[$i][$k] : $this -> infiniteDistance);
}
$foo.= "\n";
}
return $foo;
}
function getResults($to = null)
{
$ourShortestPath = array();
$foo = '';
for ($i = 0; $i < $this -> numberOfNodes; $i++)
{
if($to !== null && $to !== $i)
{
continue;
}
$ourShortestPath[$i] = array();
$endNode = null;
$currNode = $i;
$ourShortestPath[$i][] = $i;
while ($endNode === null || $endNode != $this -> startnode)
{
$ourShortestPath[$i][] = $this -> previousNode[$currNode];
$endNode = $this -> previousNode[$currNode];
$currNode = $this -> previousNode[$currNode];
}
$ourShortestPath[$i] = array_reverse($ourShortestPath[$i]);
if ($to === null || $to === $i)
{
if($this -> distance[$i] >= $this -> infiniteDistance)
{
$foo .= sprintf("ไม่่มีเส้น path จากจากโหนด %d ในรอบ•ี่ %d. \n",$this -> startnode,$i);
} else {
$foo .= sprintf($this -> distance[$i]);
}
if ($to === $i)
{
break;
}
}
}
return $foo;
}
}
?>
@ช่วยอธิบายเป็นขั้นตอนการทำงานอย่างละเอียดค่ะ พอดีว่ากำลังศึกษาเพื่อนำไปทำโปเจ็ค แต่ว่ายังไม่เข้าใจค่ะ รบกวนหน่อยนะคะ ด่วนค่ะ
Tag : PHP, CakePHP

|
|
 |
 |
 |
 |
Date :
2010-09-24 17:53:12 |
By :
ผู้เริ่มต้น |
View :
1076 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองอ่านก่อนน่ะครับ ไม่เข้าใจตรงไหนก็ค่อยถามน่ะครับ ความหมายของ function ลองเดาจาก ความหมายของคำศัพท์ครับ
|
 |
 |
 |
 |
Date :
2010-09-24 18:32:16 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|