Do a medical project in which the single source shortest path algorithm is used in case scoring. 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) must also be 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 the 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]}. Suppose g= , 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, 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 joined}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 is 0$b_mark[$start _node_id]=true;//initial point joins the collection $current_node_ Id= $start _node_id;//recently joined node Id$node_count=count ($node _info_arr); for ($i =0; $i < $node _count; $i + +) {$min = ' INF ';/ The nearest distance of the current node 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 Nearest node 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. '
';}}
Examples of these are graphs:
The result of the operation is:
0=>0:0
0=>4:10
0=>3:60
0=>2:30
0=>1:70
Go from: Kangrui's tribe» Single Source Shortest path PHP implementation