1
I would like to know how to modify such code so that it reads the file data .txt
to carry out the search from them.
Code:
<?php
function dijkstra($graph_array, $source, $target) {
$vertices = array();
$neighbours = array();
foreach ($graph_array as $edge) {
array_push($vertices, $edge[0], $edge[1]);
$neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
$neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
}
$vertices = array_unique($vertices);
echo $vertices[5]."<br>";
foreach ($vertices as $vertex) {
$dist[$vertex] = INF;
$previous[$vertex] = NULL;
}
$dist[$source] = 0;
$Q = $vertices;
while (count($Q) > 0) {
// TODO - Find faster way to get minimum
$min = INF;
foreach ($Q as $vertex){
if ($dist[$vertex] < $min) {
$min = $dist[$vertex];
$u = $vertex;
echo $u;
}
}
$Q = array_diff($Q, array($u));
if ($dist[$u] == INF or $u == $target) {
break;
}
if (isset($neighbours[$u])) {
foreach ($neighbours[$u] as $arr) {
$alt = $dist[$u] + $arr["cost"];
if ($alt < $dist[$arr["end"]]) {
$dist[$arr["end"]] = $alt;
$previous[$arr["end"]] = $u;
}
}
}
}
$path = array();
$u = $target;
while (isset($previous[$u])) {
array_unshift($path, $u);
$u = $previous[$u];
}
array_unshift($path, $u);
return $path;
}
$graph_array = array(
array("a", "b", 7),
array("a", "c", 9),
array("a", "f", 14),
array("b", "c", 10),
array("b", "d", 15),
array("c", "h", 11),
array("c", "f", 2),
array("d", "e", 6),
array("e", "f", 9),
array("f", "h", 7),
array("h", "i", 3)
);
$path = dijkstra($graph_array, "a", "i");
echo "path is: ".implode(", ", $path)."\n";
?>
txt file:
city=A(100,80);
city=B(160,70);
city=C(110,50);
city=D(140,120);
city=F(155,40);
city=G(210,60);
city=H(190,10);
city=I(170,110);
route=A-C;140;
route=A-D;155;
route=C-F;125;
route=D-B;115;
route=D-I;152;
route=B-F;119;
route=B-G;136;
route=G-F;133;
route=F-H;163;
route=I-H;197;
It would be important [Dit] to post and show how tried to do, and what was the stage that had difficulty, so it makes it easier for those who answer.
– Bacco
From what I understand, you want to put the code file reading, that?
– William Aparecido Brandino
Yes, put the file read instead of typing what I want to search!
– Luiz Felipe
@Luizfelipe Do you want to pass only the "routes" that are in the file to this function? the "cities" not? something else, for example, on the last line you want to pick up
I
,H
and 197 separately or the whole line?– stderr
@stderr I want both "routes" and "cities"! Since the values that are next to the cities and routes are to perform the calculation to determine the shortest distance!
– Luiz Felipe
@Luizfelipe Ok.. in the case of "cities", you want to take the letter and the two numbers separated by
,
right? and "routes" you want to get the letters separated by-
and the number, correct?– stderr
@stderr That is correct !
– Luiz Felipe
@stderr I thought I had evaluated that day, I’m sorry! Yes, it worked the way I needed it! And again, I’m sorry I didn’t give you a feedback earlier.
– Luiz Felipe