Floyd算法相比于迪杰特斯拉算法又是在于方便理解,逻辑清晰但不方便记录路径
public function run() { @set_time_limit(0); @ini_set('memory_limit', '2048M'); $obj_arr = array( array(0,7,9,999,999,14), array(7,0,10,15,999,999), array(9,10,0,11,999,2), array(999,15,11,0,6,999), array(999,999,999,6,0,9), array(14,999,2,999,9,0), ); $num = count($obj_arr); $result = $obj_arr; for($k=0;$k<$num;$k++){ for($i=0;$i<$num;$i++){ for($j=0;$j<$num;$j++){ $result[$i][$j] = min($result[$i][$k]+$result[$k][$j],$result[$i][$j]); } } } foreach($result as $k1 => $v1){ foreach($v1 as $k => $v){ echo $k1."=>".$k." ".$v."\n"; } } }
代码输出:
0=>0 0
0=>1 7
0=>2 9
0=>3 20
0=>4 20
0=>5 11
1=>0 7
1=>1 0
1=>2 10
1=>3 15
1=>4 21
1=>5 12
2=>0 9
2=>1 10
2=>2 0
2=>3 11
2=>4 11
2=>5 2
3=>0 20
3=>1 15
3=>2 11
3=>3 0
3=>4 6
3=>5 13
4=>0 20
4=>1 21
4=>2 11
4=>3 6
4=>4 0
4=>5 9
5=>0 11
5=>1 12
5=>2 2
5=>3 13
5=>4 9
5=>5 0