1- <?php 
2- 
3- /*  
4-  * Author: Mizanur rahman <[email protected] > 5-  *  
6-  */ 
7- 
8- function  dijkstra (array  $ graphstring  $ sourcestring  $ targetarray  {
9-     $ dist
10-     $ pred
11-     $ Queuenew  SplPriorityQueue ();
12- 
13-     foreach  ($ graphas  $ v$ adj
14- 	$ dist$ vPHP_INT_MAX ;
15- 	$ pred$ vnull ;
16- 	$ Queueinsert ($ vmin ($ adj
17-     }
18- 
19-     $ dist$ source0 ;
20- 
21-     while  (!$ QueueisEmpty ()) {
22- 	$ u$ Queueextract ();
23- 	if  (!empty ($ graph$ u
24- 	    foreach  ($ graph$ uas  $ v$ cost
25- 		if  ($ dist$ u$ cost$ dist$ v
26- 		    $ dist$ v$ dist$ u$ cost
27- 		    $ pred$ v$ u
28- 		}
29- 	    }
30- 	}
31-     }
32- 
33-     $ Snew  SplStack ();
34-     $ u$ target
35-     $ distance0 ;
36- 
37-     while  (isset ($ pred$ u$ pred$ u
38- 	$ Spush ($ u
39- 	$ distance$ graph$ u$ pred$ u
40- 	$ u$ pred$ u
41-     }
42- 
43-     if  ($ SisEmpty ()) {
44- 	return  ["distance "  => 0 , "path "  => $ S
45-     } else  {
46- 	$ Spush ($ source
47- 	return  ["distance "  => $ distance"path "  => $ S
48-     }
49- }
50- 
51- $ graph
52-     'A '  => ['B '  => 3 , 'C '  => 5 , 'D '  => 9 ],
53-     'B '  => ['A '  => 3 , 'C '  => 3 , 'D '  => 4 , 'E '  => 7 ],
54-     'C '  => ['A '  => 5 , 'B '  => 3 , 'D '  => 2 , 'E '  => 6 , 'F '  => 3 ],
55-     'D '  => ['A '  => 9 , 'B '  => 4 , 'C '  => 2 , 'E '  => 2 , 'F '  => 2 ],
56-     'E '  => ['B '  => 7 , 'C '  => 6 , 'D '  => 2 , 'F '  => 5 ],
57-     'F '  => ['C '  => 3 , 'D '  => 2 , 'E '  => 5 ],
58- ];
59- 
60- $ source"A " ;
61- $ target"F " ;
62- 
63- $ resultdijkstra ($ graph$ source$ target
64- extract ($ result
65- 
66- echo  "Distance from  $ source to  $ target is  $ distance  \n" ;
67- echo  "Path to follow :  " ;
68- 
69- while  (!$ pathisEmpty ()) {
70-     echo  $ pathpop () . "\t" ;
1+ <?php 
2+ 
3+ /* 
4+  * Author: Mizanur rahman <[email protected] > 5+  *  
6+  */ 
7+ 
8+ function  dijkstra (array  $ graphstring  $ sourcestring  $ targetarray  {
9+     $ dist
10+     $ pred
11+     $ Queuenew  SplPriorityQueue ();
12+ 
13+     foreach  ($ graphas  $ v$ adj
14+         $ dist$ vPHP_INT_MAX ;
15+         $ pred$ vnull ;
16+         foreach  ($ adjas  $ w$ cost
17+             $ Queueinsert ($ w$ cost
18+         }
19+     }
20+ 
21+     $ dist$ source0 ;
22+ 
23+     while  (!$ QueueisEmpty ()) {
24+         $ u$ Queueextract ();
25+         if  (!empty ($ graph$ u
26+             foreach  ($ graph$ uas  $ v$ cost
27+                 if  ($ dist$ u$ cost$ dist$ v
28+                     $ dist$ v$ dist$ u$ cost
29+                     $ pred$ v$ u
30+                 }
31+             }
32+         }
33+     }
34+ 
35+     $ Snew  SplStack ();
36+     $ u$ target
37+     $ distance0 ;
38+ 
39+     while  (isset ($ pred$ u$ pred$ u
40+         $ Spush ($ u
41+         $ distance$ graph$ u$ pred$ u
42+         $ u$ pred$ u
43+     }
44+ 
45+     if  ($ SisEmpty ()) {
46+         return  ["distance "  => 0 , "path "  => $ S
47+     } else  {
48+         $ Spush ($ source
49+         return  ["distance "  => $ distance"path "  => $ S
50+     }
51+ }
52+ 
53+ $ graph
54+     'A '  => ['B '  => 3 , 'C '  => 5 , 'D '  => 9 ],
55+     'B '  => ['A '  => 3 , 'C '  => 3 , 'D '  => 4 , 'E '  => 7 ],
56+     'C '  => ['A '  => 5 , 'B '  => 3 , 'D '  => 2 , 'E '  => 6 , 'F '  => 3 ],
57+     'D '  => ['A '  => 9 , 'B '  => 4 , 'C '  => 2 , 'E '  => 2 , 'F '  => 2 ],
58+     'E '  => ['B '  => 7 , 'C '  => 6 , 'D '  => 2 , 'F '  => 5 ],
59+     'F '  => ['C '  => 3 , 'D '  => 2 , 'E '  => 5 ],
60+ ];
61+ 
62+ $ source"E " ;
63+ $ target"F " ;
64+ 
65+ $ resultdijkstra ($ graph$ source$ target
66+ extract ($ result
67+ 
68+ echo  "Distance from  $ source to  $ target is  $ distance  \n" ;
69+ echo  "Path to follow :  " ;
70+ 
71+ while  (!$ pathisEmpty ()) {
72+     echo  $ pathpop () . "\t" ;
7173}
0 commit comments