<?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)
{
$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);
}
}
// var_dump($this->getGraph());
var_dump($this->getNodes());
die();
}
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 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 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(5)
["Q"]=>
string(1) "A"
["final"]=>
bool(false)
}
["C"]=>
array(3) {
["L"]=>
int(2)
["Q"]=>
string(1) "A"
["final"]=>
bool(false)
}
["D"]=>
array(3) {
["L"]=>
float(INF)
["Q"]=>
NULL
["final"]=>
bool(false)
}
["E"]=>
array(3) {
["L"]=>
float(INF)
["Q"]=>
NULL
["final"]=>
bool(false)
}
}
- 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(5)
["Q"]=>
string(1) "A"
["final"]=>
bool(false)
}
["C"]=>
array(3) {
["L"]=>
int(2)
["Q"]=>
string(1) "A"
["final"]=>
bool(false)
}
["D"]=>
array(3) {
["L"]=>
float(INF)
["Q"]=>
NULL
["final"]=>
bool(false)
}
["E"]=>
array(3) {
["L"]=>
float(INF)
["Q"]=>
NULL
["final"]=>
bool(false)
}
}
preferences:
151.87 ms | 403 KiB | 180 Q