Single Source Shortest path (Dijkstra algorithm) PHP implementation

Source: Internet
Author: User

To do a medical project in which the single source shortest path algorithm is used in case scoring. The idea of the Dijkstra algorithm for single source shortest path is as follows:
If there is a shortest path from I to J (Vi ...). VK,VJ), Vk is a vertex in front of Vj. So (Vi ... VK) is also necessarily the shortest path from I to K. Dijkstra is an algorithm that increments the shortest path length and generates the shortest path successively. For example: For source vertex V0, first select the shortest vertex vi in its directly adjacent vertex, then the shortest distance from V0 to the VJ Vertex is currently known dist[j]=min{dist[j],dist[i]+cost[i][j]}. If G=<v, E>, the source point is V0,u={v0} represents the set of vertices that have been marked, dist[i] records the shortest distance V0 to I, and cost[i][j] represents the cost of the edge I to J.
1. Select the vertex i with the lowest value of dist[i] from v-u and add I to u;

2. Update the dist value with the I directly adjacent vertex. (Dist[j]=min{dist[j],dist[i]+cost[i][j]})

3. Know U=v, stop.

Using PHP's unique nature, its code is as follows:

function Dijkstra () {$node _info_arr=array (//node adjacency table structure Array (' node_id ' =>0,//a node's id ' Next_node ' =>array (4,2,1), ' Node_type ' =>0, ' cost ' =>array (10,30,100)), Array (' node_id ' =>4,//a node's id ' Next_node ' =>array (3), ' Node_ Type ' =>1, ' Cost ' =>array (()), Array (' node_id ' =>3,//a Node ID ' next_node ' =>array (1), ' Node_type ' =>1, ' Cost ' =>array ', Array (' node_id ' =>2,//a Node ID ' next_node ' =>array (3,1), ' Node_type ' =>1, ' cost ' = Array (60,60)), Array (' node_id ' =>1,//a Node ID ' next_node ' =>array (), ' Node_type ' =>2, ' cost ' =>array ()); $ start_node_id=false;//start Node Id$i_cost=array (array ());//Two nodes overhead $i_dist=array ();//The shortest distance from the starting point to each point $b_mark=array ();// Whether to add foreach ($node _info_arr as & $node _info) {if ($node _info[' Node_type ']==0) {$start _node_id= $node _info[' Node_ Id '];//found the initial node}foreach ($node _info[' Next_node '] as $key = $next _node) {$i _cost[$node _info[' node_id ']][$next _node] = $node _info[' cost ' [$key];} $i _dist[$node _info[' node_id ']]= ' INF ';//initialized to Infinity $b_mark[$node _info[' node_id ']]=false;//initialization not added}if ($start _node_id===false) {return ' 302 ';} Calculate the initial node to the shortest path of the nodes $i_dist[$start _node_id]=0;//The initial point to its own distance of 0$b_mark[$start _node_id]=true;//initial point to increase the collection $current_node_ Id= $start _node_id;//recently added node Id$node_count=count ($node _info_arr); for ($i =0; $i < $node _count; $i + +) {$min = ' INF ';// The current node's recent distance if (Is_array ($i _cost[$current _node_id]) {foreach ($i _cost[$current _node_id] as $key + = $val) {if ($i _dist[ $key]== ' INF ' | | $i _dist[$key]> $i _dist[$current _node_id]+ $val) {$i _dist[$key]= $i _dist[$current _node_id]+ $val;}} foreach ($i _dist as $key = + $val) {if (! $b _mark[$key]) {if ($val! = ' inf ' && ($min = = ' inf ') | | $min > $val) {$min = $val; $candidate _node_id= $key;//Candidate recent id}}}if ($min = = ' INF ') {break;} $current _node_id= $candidate _node_id $b _mark[$current _node_id]=true;} foreach ($i _dist as $key = + $val) {echo $start _node_id. ' = '. $key. ': ' $val. ' <br/> ';}}

Examples of this are shown in figure:

The result of the execution is:
0=>0:0
0=>4:10
0=>3:60
0=>2:30
0=>1:70

Transferred from: Kangrui Tribe ? Single Source Shortest path PHP implementation

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.