Dijkstra’s algorithm php
$INT_MAX = 0x7FFFFFFF;
function MinimumDistance($distance, $shortestPathTreeSet, $verticesCount)
{
global $INT_MAX;
$min = $INT_MAX;
$minIndex = 0;
for ($v = 0; $v < $verticesCount; ++$v)
{
if ($shortestPathTreeSet[$v] == false && $distance[$v] <= $min)
{
$min = $distance[$v];
$minIndex = $v;
}
}
return $minIndex;
}
function PrintResult($distance, $verticesCount)
{
echo "<pre>" . "Vertex Distance from source" . "</pre>";
for ($i = 0; $i < $verticesCount; ++$i)
echo "<pre>" . $i . "\t " . $distance[$i] . "</pre>";
}
function Dijkstra($graph, $source, $verticesCount)
{
global $INT_MAX;
$distance = array();
$shortestPathTreeSet = array();
for ($i = 0; $i < $verticesCount; ++$i)
{
$distance[$i] = $INT_MAX;
$shortestPathTreeSet[$i] = false;
}
$distance[$source] = 0;
for ($count = 0; $count < $verticesCount - 1; ++$count)
{
$u = MinimumDistance($distance, $shortestPathTreeSet, $verticesCount);
$shortestPathTreeSet[$u] = true;
for ($v = 0; $v < $verticesCount; ++$v)
if (!$shortestPathTreeSet[$v] && $graph[$u][$v] && $distance[$u] != $INT_MAX && $distance[$u] + $graph[$u][$v] < $distance[$v])
$distance[$v] = $distance[$u] + $graph[$u][$v];
}
PrintResult($distance, $verticesCount);
}
Are there any code examples left?
New code examples in category PHP
-
PHP 2022-03-28 00:40:03 index:of curiosa 2019 mp4
-
PHP 2022-03-28 00:35:12 get_declared_classes
-
PHP 2022-03-28 00:30:23 php move index of a value to first position in array
-
PHP 2022-03-28 00:25:01 wordpress get_date
-
PHP 2022-03-27 23:30:38 php shorten string with dots
-
PHP 2022-03-27 23:10:46 php header 500
-
PHP 2022-03-27 23:05:06 laravel schedule run
-
PHP 2022-03-27 22:25:12 Get page title, excerpt or content by id
-
PHP 2022-03-27 22:00:27 php convert date from dd/mm/yyyy to yyyy-mm-dd