Single-source shortest path (dijkstra algorithm) php implementation

Source: Internet
Author: User
The single-source shortest path (dijkstra algorithm) php implements a medical project, in which the single-source shortest path algorithm is used in case scoring. The single-source shortest path dijkstra algorithm has the following ideas:
If there is a shortest path (Vi... Vk, Vj) from I to j, Vk is a vertex before Vj. Then (Vi... Vk) must be the shortest path from I to k. Dijkstra is an algorithm that increases the length of the shortest path and generates the shortest path one by one. For example, for the source vertex V0, first select its directly adjacent vertex Vi with the shortest length, the shortest distance from V0 to Vj vertex is known as dist [j] = min {dist [j], dist [I] + cost [I] [j]}. Suppose G = The source vertex is V0, U = {V0} indicates the vertex set that has been marked. dist [I] records the shortest distance from V0 to I, cost [I] [j] indicates the overhead from edge I to j.
1. select vertex I that minimizes dist [I] value from the V-U and add I to U;

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

3. know U = V, stop.

The code of php is as follows:

Function dijkstra () {$ node_info_arr = array (// array of the adjacent table structure of the node ('node _ id' => 0, // id of a node 'next _ node' => array (, 1), 'node _ type' => 0, 'cost' => array, 100), array ('node _ id' => 4, // The id of a node 'next _ node' => array (3 ), 'node _ type' => 1, 'cost' => array (50), array ('node _ id' => 3, // id of a node 'next _ node' => array (1), 'node _ type' => 1, 'cost' => array (10 )), array ('node _ id' => 2, // id of a node 'next _ node' => array (3, 1), 'node _ type' => 1, 'cost' => array (60, 60) ), Array ('node _ id' => 1, // The id of a node 'next _ node' => array (), 'node _ type' => 2, 'cost' => array (); $ start_node_id = false; // start node id $ I _cost = array ()); // overhead between two nodes $ I _dist = array (); // The shortest distance from the start point to each point $ B _mark = array (); // whether foreach ($ node_info_arr as & $ node_info) {if ($ node_info ['node _ type'] = 0) is added) {$ start_node_id = $ node_info ['node _ id']; // locate 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 '; // The initialization is infinite $ B _mark [$ node_info ['node _ id'] = false; // initialization is not added} if ($ start_node_id = false) {return '000000';} // calculate the shortest path from the initial node to each node $ I _dist [$ start_node_id] = 0; // The distance between the initial vertex and its own is 0 $ B _mark [$ start_node_id] = true; // add the set $ current_node_id = $ start_node_id to the initial vertex; // recently added node id $ node_count = count ($ node_info_arr); for ($ I = 0; $ I <$ node_count; $ I ++) {$ min = 'inf '; // the closest distance to 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 latest 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.'
';}}

The example is an image:

The running result is:
0 => 0: 0
0 => 4: 10
0 => 3: 60
0 => 2: 30
0 => 1: 70


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.