Use php to implement flody algorithm output

Source: Internet
Author: User
Using php to implement the flody algorithm output the following is my implemented flody algorithm, but I cannot output the result when writing the output () function. please help me to write it out. thank you!
/**
* PHP implements graph joining matrix
*
* @ Author zhaojiangwei
* @ Since 2011/10/31 17:23
*/

Class MGraph {
Private $ vexs; // vertex array
Private $ arc; // edge adjacent matrix, that is, a two-dimensional array
Private $ arcData; // edge Array Information
Private $ direct; // graph type (undirected or undirected)
Private $ infinity = 65535; // represents infinity, that is, two points are not connected. this parameter is used when creating an image with a weight value. In this example, the weight value is not included.


Public function MGraph ($ vexs, $ arc, $ direct = 0 ){
$ This-> vexs = $ vexs;
$ This-> arcData = $ arc;
$ This-> direct = $ direct;
$ This-> initalizeArc ();
$ This-> createArc ();
}

Private function initalizeArc (){
Foreach ($ this-> vexs as $ value ){
Foreach ($ this-> vexs as $ cValue ){
$ This-> arc [$ value] [$ cValue] = ($ value = $ cValue? 0: $ this-> infinity );
}
}
}

// Create a graph $ direct: 0 indicates an undirected graph, and 1 indicates a directed graph.
Private function createArc (){
Foreach ($ this-> arcData as $ key => $ value ){
$ StrArr = str_split ($ key );
$ First = $ strArr [0];
$ Last = $ strArr [1];
$ This-> arc [$ first] [$ last] = $ value;
If (! $ This-> direct ){
$ This-> arc [$ last] [$ first] = $ value;
}
}
}

// Floyd algorithm
Public function floyd (){
$ Path = array (); // path array
$ Distance = array (); // distance array

Foreach ($ this-> arc as $ key => $ value ){
Foreach ($ value as $ k => $ v ){
$ Path [$ key] [$ k] = $ k;
$ Distance [$ key] [$ k] = $ v;
}
}

For ($ j = 0; $ j <count ($ this-> vexs); $ j ++ ){
For ($ I = 0; $ I <count ($ this-> vexs); $ I ++ ){
For ($ k = 0; $ k <count ($ this-> vexs); $ k ++ ){
If ($ distance [$ this-> vexs [$ I] [$ this-> vexs [$ k]> $ distance [$ this-> vexs [$ I] [$ this-> vexs [$ j] + $ distance [$ this-> vexs [$ j] [$ this-> vexs [$ k]) {
$ Path [$ this-> vexs [$ I] [$ this-> vexs [$ k] = $ path [$ this-> vexs [$ I] [$ this-> vexs [$ j];
$ Distance [$ this-> vexs [$ I] [$ this-> vexs [$ k] = $ distance [$ this-> vexs [$ I] [$ this-> vexs [$ j] + $ distance [$ this-> vexs [$ j] [$ this-> vexs [$ k];
}
}
}
}

Return array ($ path, $ distance );
// Return $ path;
}

Public function output ($ I, $ j)
{
If ($ I = $ j)
Return;

If ($ path [$ this-> vexs [$ I] [$ this-> vexs [$ j] = 0)
Echo $ j;
Else {
Output ($ I, $ path [$ this-> vexs [$ I] [$ this-> vexs [$ j]);
Output ($ path [$ this-> vexs [$ I] [$ this-> vexs [$ j], $ j );
}
}



}

?>

Require 'mgraph. php ';
$ A = array ('A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H ', 'I ');
$ B = array ('AB' => '10', 'AF' => '11', 'bg '=> '16 ', 'fg' => '17', 'BC' => '18', 'bi' => '12', 'ci' => '8 ', 'CD' => '22', 'di' => '21', 'dg '=> '24', 'GH' => '19 ', 'DH '=> '16', 'DE' => '20', 'eh' => '7', 'Fe '=> '26 ');
// The key is the edge and the value is the weight.
$ Test = new MGraph ($ a, $ B );
Echo"
";
Print_r ($ test-> floyd ());
Echo"
";
/* $ U =;
$ V = g;
If ($ distance [$ this-> vexs [$ u] [$ this-> vexs [$ v] = $ infinity)
Echo "NO Path ";
Else {
Echo $ u;
Output ($ u, $ v );
}*/
$ Test-> output (a, f );

?>


Reply to discussion (solution)

There are a large number of warnings for using undefined variables.

In particular, $ path in the output method

Is it floyd?
Not studied

Can you write down the output function for me?

$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');$b = array('ab'=>'10', 'af'=>'11', 'bg'=>'16', 'fg'=>'17', 'bc'=>'18', 'bi'=>'12', 'ci'=>'8', 'cd'=>'22', 'di'=>'21', 'dg'=>'24', 'gh'=>'19', 'dh'=>'16', 'de'=>'20', 'eh'=>'7','fe'=>'26');$d = floyd($a, $b);echo $d['a']['g']; //26echo $d['b']['h']; //35function floyd($a, $b) {  $a = array_flip($a);  $n = count($a);  $D = array_fill(0, $n, array_fill(0, $n, 0xffff));  foreach($b as $k=>$v) {    $D[$a[$k{0}]][$a[$k{1}]] = $v;    $D[$a[$k{1}]][$a[$k{0}]] = $v;  }  for($k=0; $k<$n; $k++) {    for($i=0; $i<$n; $i++) {      if($k == $i) $D[$k][$i] = 0;      for($j=0; $j<$n; $j++) {        if($D[$i][$k]+$D[$k][$j]<$D[$i][$j]) $D[$i][$j]=$D[$i][$k]+$D[$k][$j];      }    }  }  $a = array_flip($a);  for($i=0; $i<$n; $i++) $D[$i] = array_combine($a, $D[$i]);  $D = array_combine($a, $D);  return $D;}

Hello, moderator, I can implement the output shortest path weight. I 'd like to ask you to help me to output the shortest path, for example, a-> B-> d-> f. thank you!

Adjust the algorithm

$ A = array ('A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H ', 'I'); $ B = array ('AB' => '10', 'AF' => '11', 'bg '=> '16 ', 'fg' => '17', 'BC' => '18', 'bi' => '12', 'ci' => '8 ', 'CD' => '22', 'di' => '21', 'dg '=> '24', 'GH' => '19 ', 'DH '=> '16', 'DE' => '20', 'eh' => '7', 'Fe '=> '26 '); $ d = floyd ($ a, $ B); // view it; foreach ($ a as $ I) {foreach ($ a as $ j) {$ t = $ d [$ I] [$ j]; printf ("% s to % s (% d): % s \ n", $ I, $ j, $ t ['V'], join ('->', $ t ['st']);} Function floyd ($ a, $ B) {foreach ($ a as $ I) {foreach ($ a as $ j) $ d [$ I] [$ j] = array ('V' => $ I = $ j? 0: 0 xffff, 'st' => array ($ I, $ j);} foreach ($ B as $ k => $ v) {$ d [$ k {0}] [$ k {1}] ['V'] = $ v; $ d [$ k {1}] [$ k {0}] ['V'] = $ v;} foreach ($ a as $ k) {foreach ($ a as $ I) foreach ($ a as $ j) if ($ d [$ I] [$ j] ['V']> $ d [$ I] [$ k] ['V'] + $ d [$ k] [$ j] ['V']) {$ d [$ I] [$ j] ['V'] = $ d [$ I] [$ k] ['V'] + $ d [$ k] [$ j] ['V']; array_splice ($ d [$ I] [$ j] ['st'],-1, 0, $ k) ;}} return $ d ;}
a to a (0): a -> aa to b (10): a -> ba to c (28): a -> b -> ca to d (43): a -> c -> i -> da to e (37): a -> d -> f -> ea to f (11): a -> fa to g (26): a -> b -> ga to h (44): a -> d -> f -> ha to i (22): a -> b -> ib to a (10): b -> ab to b (0): b -> bb to c (18): b -> cb to d (33): b -> c -> i -> db to e (42): b -> d -> f -> h -> eb to f (21): b -> a -> fb to g (16): b -> gb to h (35): b -> d -> f -> g -> hb to i (12): b -> ic to a (28): c -> b -> ac to b (18): c -> bc to c (0): c -> cc to d (22): c -> dc to e (42): c -> d -> ec to f (39): c -> b -> fc to g (34): c -> b -> gc to h (38): c -> d -> hc to i (8): c -> id to a (43): d -> c -> i -> ad to b (33): d -> c -> i -> bd to c (22): d -> cd to d (0): d -> dd to e (20): d -> ed to f (41): d -> c -> e -> g -> fd to g (24): d -> gd to h (16): d -> hd to i (21): d -> ie to a (37): e -> d -> f -> ae to b (42): e -> d -> f -> h -> be to c (42): e -> d -> ce to d (20): e -> de to e (0): e -> ee to f (26): e -> fe to g (26): e -> d -> f -> h -> ge to h (7): e -> he to i (41): e -> d -> if to a (11): f -> af to b (21): f -> a -> bf to c (39): f -> b -> cf to d (41): f -> c -> e -> g -> df to e (26): f -> ef to f (0): f -> ff to g (17): f -> gf to h (33): f -> d -> e -> hf to i (33): f -> b -> ig to a (26): g -> b -> ag to b (16): g -> bg to c (34): g -> b -> cg to d (24): g -> dg to e (26): g -> d -> f -> h -> eg to f (17): g -> fg to g (0): g -> gg to h (19): g -> hg to i (28): g -> b -> ih to a (44): h -> d -> f -> ah to b (35): h -> d -> f -> g -> bh to c (38): h -> d -> ch to d (16): h -> dh to e (7): h -> eh to f (33): h -> d -> e -> fh to g (19): h -> gh to h (0): h -> hh to i (37): h -> d -> ii to a (22): i -> b -> ai to b (12): i -> bi to c (8): i -> ci to d (21): i -> di to e (41): i -> d -> ei to f (33): i -> b -> fi to g (28): i -> b -> gi to h (37): i -> d -> hi to i (0): i -> i

Thank you! let me have a try today!

Hello, Lord, I just read the code and results and found that the results are not completely correct, such as a to d (43): a-> c-> I-> d, it should actually be a-> B-> I-> d. Besides, although the returned weights are correct, the paths are incorrect.

Array_splice ($ d [$ I] [$ j] ['st'],-1, 0, $ k );
Change
$ D [$ I] [$ j] ['st'] = array_merge ($ d [$ I] [$ k] ['st'], array_slice ($ d [$ k] [$ j] ['st'], 1 ));
Get

a to a (0): a -> aa to b (10): a -> ba to c (28): a -> b -> ca to d (43): a -> b -> i -> da to e (37): a -> f -> ea to f (11): a -> fa to g (26): a -> b -> ga to h (44): a -> f -> e -> ha to i (22): a -> b -> ib to a (10): b -> ab to b (0): b -> bb to c (18): b -> cb to d (33): b -> i -> db to e (42): b -> g -> h -> eb to f (21): b -> a -> fb to g (16): b -> gb to h (35): b -> g -> hb to i (12): b -> ic to a (28): c -> b -> ac to b (18): c -> bc to c (0): c -> cc to d (22): c -> dc to e (42): c -> d -> ec to f (39): c -> b -> a -> fc to g (34): c -> b -> gc to h (38): c -> d -> hc to i (8): c -> id to a (43): d -> i -> b -> ad to b (33): d -> i -> bd to c (22): d -> cd to d (0): d -> dd to e (20): d -> ed to f (41): d -> g -> fd to g (24): d -> gd to h (16): d -> hd to i (21): d -> ie to a (37): e -> f -> ae to b (42): e -> h -> g -> be to c (42): e -> d -> ce to d (20): e -> de to e (0): e -> ee to f (26): e -> fe to g (26): e -> h -> ge to h (7): e -> he to i (41): e -> d -> if to a (11): f -> af to b (21): f -> a -> bf to c (39): f -> a -> b -> cf to d (41): f -> g -> df to e (26): f -> ef to f (0): f -> ff to g (17): f -> gf to h (33): f -> e -> hf to i (33): f -> a -> b -> ig to a (26): g -> b -> ag to b (16): g -> bg to c (34): g -> b -> cg to d (24): g -> dg to e (26): g -> h -> eg to f (17): g -> fg to g (0): g -> gg to h (19): g -> hg to i (28): g -> b -> ih to a (44): h -> e -> f -> ah to b (35): h -> g -> bh to c (38): h -> d -> ch to d (16): h -> dh to e (7): h -> eh to f (33): h -> e -> fh to g (19): h -> gh to h (0): h -> hh to i (37): h -> d -> ii to a (22): i -> b -> ai to b (12): i -> bi to c (8): i -> ci to d (21): i -> di to e (41): i -> d -> ei to f (33): i -> b -> a -> fi to g (28): i -> b -> gi to h (37): i -> d -> hi to i (0): i -> i

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.