Data:
<?PHP$list=[ ' A ' =>[' B ' =>1, ' C ' =>2, ' d ' =>3,], ' B ' =>[' A ' =>1, ' C ' =>4, ' e ' =>9 ', ' C ' =>[' a ' =>2, ' B ' =>4, ' d ' =>5, ' e ' =>6], ' d ' =>[' a ' =>3, ' C ' =>5, ' e ' =>8], ' e ' =>[' B ' =>7, ' C ' =>6, ' d ' =>8],];/*three main points: first, initialize two arrays, one to hold the nearest node that has been found, in order to prevent duplication of the search (an array of a and B, the next from B does not have to repeat from B to a), and the other to hold all the nodes outside the nearest node with a connection distance (storage C and D). Second, from a, find and a nearest node B. Thirdly, starting with this nearest node B, find the node C closest to this node, and compare the distance from the node C to a, found by B, to the distance from all other nodes to a. Finally, you can complete the search by repeating three steps. Final array: Array ([0] = = Array ([b] = 0 [ba] = 1 [BAC] = 3 [Bad] = 4 [be] + 9 [bace] = 9 [bade] =&G T ))*/functionFind$node,$list,$next,$a=[]){ if(Count($next)==Count($list)){ return $_get=[Array_merge($next,$a)]; } foreach($list[$node[strlen($node)-1]] as $k=$v){ foreach($next as $n=$m){ if($k==$n[strlen($n)-1]){ unset($list[$node][$k]); } } } foreach($list[$node[strlen($node)-1]] as $k=$v){ $a[$node.$k]=$next[$node]+$v; } foreach($a as $k=$v){ foreach($next as $n=$m){ if($k[strlen($k) -1]==$n[strlen($n)-1]){ unset($a[$k]); } } } foreach($a as $k=$v){ if(!isset($smallest) ||$smallest>$v ){ $smallest=$v ; $node=$k ; } } //$node = $node [Strlen ($node)-1]; $next[$node]=$smallest; unset($smallest); Find ($node,$list,$next,$a);} Find (' B ',$list, [' B ' =>0]); Print_r($_get);
Reference connection:
Https://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html
Http://www.vithen.com/inf/1451557147.html
Use PHP to complete shortest path lookups