Use PHP to complete shortest path lookups

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.