3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Graph { private $graph = []; private $nodes = []; public function __construct(array $edges) { foreach ($edges as $edge => $weight) { $start = $edge[0]; $end = $edge[1]; $this->graph[$start][$end] = $weight; $this->graph[$end][$start] = $weight; $this->nodes[$start] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; $this->nodes[$end] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; } } public function find($start, $end, $initStartNode = true) { if ($initStartNode) { $this->initStartNode($start); } $nodes = $this->getNextNodes($start); $startLength = $this->getLength($start); foreach($nodes as $node => $weight) { $currentLength = $this->getLength($node); if ($startLength + $weight < $currentLength) { $this->setLength($node, $startLength + $weight); $this->setFromNode($node, $start); } } $nextNode = $this->getNextMinimumNode(); if (is_null($nextNode)) { return $this->getNodes(); } $this->setFinal($nextNode); $this->find($nextNode, $end, false); // var_dump($this->getGraph()); var_dump($this->getNodes()); // die(); } protected function getNextMinimumNode() { $nodes = $this->getNodes(); $min = INF; $minNode = null; foreach ($nodes as $node => $data) { if ($data['final']) { continue; } $nodeLength = $this->getLength($node); if ($nodeLength < $min) { $min = $nodeLength; $minNode = $node; } } return $minNode; } protected function initStartNode($node) { $this->setLength($node, 0); $this->setFromNode($node, '-'); $this->setFinal($node); } protected function getNextNodes($node) { return $this->graph[$node]; } protected function setLength($node, $value) { $this->nodes[$node]['L'] = $value; } protected function getLength($node) { return $this->nodes[$node]['L']; } protected function setFromNode($node, $value) { $this->nodes[$node]['Q'] = $value; } protected function setFinal($node) { $this->nodes[$node]['final'] = true; } public function getGraph() { return $this->graph; } public function getNodes() { return $this->nodes; } } $g = new Graph([ 'AB' => 5, 'AC' => 2, 'CB' => 2, 'CD' => 8, 'BE' => 7, 'DE' => 1, ]); $result = $g->find('A', 'E'); var_dump($result); // var_dump($g->getGraph()); // var_dump($g->getNodes());
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } NULL
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } array(5) { ["A"]=> array(3) { ["L"]=> int(0) ["Q"]=> string(1) "-" ["final"]=> bool(true) } ["B"]=> array(3) { ["L"]=> int(4) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["C"]=> array(3) { ["L"]=> int(2) ["Q"]=> string(1) "A" ["final"]=> bool(true) } ["D"]=> array(3) { ["L"]=> int(10) ["Q"]=> string(1) "C" ["final"]=> bool(true) } ["E"]=> array(3) { ["L"]=> int(11) ["Q"]=> string(1) "B" ["final"]=> bool(true) } } NULL

preferences:
235.57 ms | 409 KiB | 291 Q