Fuente: https://github.com/chamilo/chamilo-lms/releases/download/v1.11.40/chamilo-1.11.40.zip sha256: 1cf4bf2cc7bae1ef1a1eff643235db1d552f78ddf4b6dd1e2d2dac9868679439 Snapshot independiente (rama huerfana); diffable vs 1.11.38. vendor incluido.
62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Graphp\Algorithms\Search;
|
|
|
|
use Fhaculty\Graph\Set\Vertices;
|
|
|
|
class BreadthFirst extends Base
|
|
{
|
|
/**
|
|
* @param int $maxDepth
|
|
* @return Vertices
|
|
*/
|
|
public function getVertices($maxDepth = PHP_INT_MAX)
|
|
{
|
|
$queue = array($this->vertex);
|
|
// to not add vertices twice in array visited
|
|
$mark = array($this->vertex->getId() => true);
|
|
// visited vertices
|
|
$visited = array();
|
|
|
|
// keep track of depth
|
|
$currentDepth = 0;
|
|
$nodesThisLevel = 1;
|
|
$nodesNextLevel = 0;
|
|
|
|
do {
|
|
// get first from queue
|
|
$t = \array_shift($queue);
|
|
// save as visited
|
|
$visited[$t->getId()] = $t;
|
|
|
|
// get next vertices
|
|
$children = $this->getVerticesAdjacent($t);
|
|
|
|
// track depth
|
|
$nodesNextLevel = $children->count();
|
|
if (--$nodesThisLevel === 0) {
|
|
if (++$currentDepth > $maxDepth) {
|
|
return new Vertices($visited);
|
|
}
|
|
$nodesThisLevel = $nodesNextLevel;
|
|
$nodesNextLevel = 0;
|
|
}
|
|
|
|
// process next vertices
|
|
foreach ($children->getMap() as $id => $vertex) {
|
|
// if not "touched" before
|
|
if (!isset($mark[$id])) {
|
|
// add to queue
|
|
$queue[] = $vertex;
|
|
// and mark
|
|
$mark[$id] = true;
|
|
}
|
|
}
|
|
|
|
// until queue is empty
|
|
} while ($queue);
|
|
|
|
return new Vertices($visited);
|
|
}
|
|
}
|