function calcShortestPaths(vertex $start, &$adjLists) { // define an empty queue $q = new PriorityQueue(); // push the starting vertex into the queue $q->insert($start, 0); $q->rewind(); // mark the distance to it 0 $start->distance = 0; // the path to the starting vertex $start->path = array($start->key); while ($q->valid()) { $vertex = $q->extract(); $vertex->visited = 1; $list = $adjLists[$vertex->key]; while ($list->valid()) { $item = $list->current(); if (!$item['vertex']->visited) { if ($item['vertex']->distance > $vertex->distance + $item['distance']) { $item['vertex']->distance = $vertex->distance + $item['distance']; $item['vertex']->parent = $vertex; } $item['vertex']->path = array_merge($vertex->path, array($item['vertex']->key)); $q->insert($item["vertex"], $item["vertex"]->distance); } $list->next(); } $q->recoverFromCorruption(); $q->rewind(); } }
function calcDistances($start) { // define an empty queue $q = new PriorityQueue(); // push the starting vertex into the queue $q->insert($start, 0); $q->rewind(); // mark the distance to it 0 $start->distance = 0; while ($q->valid()) { $t = $q->extract(); $t->visited = 1; foreach ($t->getRelations() as $key => $relation) { foreach ($relation as $object) { if (!$object->visited) { if ($object->distance > $t->distance + 1) { $object->distance = $t->distance + 1; $object->parent = $t; } $q->insert($object, $object->distance); } } } $q->recoverFromCorruption(); $q->rewind(); } }