** 플로이드 알고리즘 : 최단거리검색 **
<?php
$n=8;
$m=5000;
$a = array();
$a[] = array(0,2,$m,$m,$m,3,$m,$m);
$a[] = array(2,0,4,1,$m,$m,$m,$m);
$a[] = array($m,4,0,$m,$m,$m,3,$m);
$a[] = array($m,1,$m,0,3,$m,2,$m);
$a[] = array($m,$m,3,3,0,$m,$m,4);
$a[] = array(3,$m,$m,$m,$m,0,6,$m);
$a[] = array($m,$m,$m,2,$m,6,0,4);
$a[] = array($m,$m,$m,$m,4,$m,4,0);
for($k = 0; $k < $n; $k++){
for($i = 0; $i < $n; $i++){
for($j = 0; $j < $n; $j++){
if ($a[$i][$j] > $a[$i][$k] + $a[$k][$j]) { $a[$i][$j] = $a[$i][$k] + $a[$k][$j]; }
}
}
}
for($i = 0; $i < $n; $i++){
for($j = 0; $j < $n; $j++){
print $i . "=>". $j ." : " . $a[$i][$j];
print "<br />";
}
}
?>
출처 : http://rhio.tistory.com/56
<?php
$n=8;
$m=5000;
$a = array();
$a[] = array(0,2,$m,$m,$m,3,$m,$m);
$a[] = array(2,0,4,1,$m,$m,$m,$m);
$a[] = array($m,4,0,$m,$m,$m,3,$m);
$a[] = array($m,1,$m,0,3,$m,2,$m);
$a[] = array($m,$m,3,3,0,$m,$m,4);
$a[] = array(3,$m,$m,$m,$m,0,6,$m);
$a[] = array($m,$m,$m,2,$m,6,0,4);
$a[] = array($m,$m,$m,$m,4,$m,4,0);
for($k = 0; $k < $n; $k++){
for($i = 0; $i < $n; $i++){
for($j = 0; $j < $n; $j++){
if ($a[$i][$j] > $a[$i][$k] + $a[$k][$j]) { $a[$i][$j] = $a[$i][$k] + $a[$k][$j]; }
}
}
}
for($i = 0; $i < $n; $i++){
for($j = 0; $j < $n; $j++){
print $i . "=>". $j ." : " . $a[$i][$j];
print "<br />";
}
}
?>
출처 : http://rhio.tistory.com/56
'프로그래밍 > PHP' 카테고리의 다른 글
xml 파서 (0) | 2009.06.15 |
---|---|
if문의 새로운(?) 문법. (2) | 2009.06.03 |
다익스트라 알고리즘 : 최단거리 검색 (0) | 2009.05.26 |
PHP 소켓 POST로.. XML 값 (0) | 2009.04.14 |
[정보] HTTP/1.1 METHOD 의 종류와 아파치에서의 제어 (0) | 2008.10.16 |