function Find($NodeStart, $NodeEnd) { $Queue = new PriorityQueue(); // Open Nodes ordered based on F cost $Queue->setExtractFlags(PriorityQueue::EXTR_DATA); $Closed = 0; $Found = FALSE; $this->Debug = ''; $this->Cache = array($NodeStart => array('G' => 0, 'F' => 0, 'Parent' => $NodeStart, 'Status' => STATUS_OPEN)); $Queue->insert($NodeStart, $this->Cache[$NodeStart]['F']); while (!$Queue->isEmpty()) { $Node = $Queue->extract(); if ($this->Cache[$Node]['Status'] == STATUS_CLOSED) { continue; } if ($Node == $NodeEnd) { $this->Cache[$Node]['Status'] = STATUS_CLOSED; $Found = TRUE; break; } if ($Closed > $this->Limit) { $this->Debug = 'Hit limit. (' . $this->Limit . ')'; return NULL; } $Neighbours = $this->Graph->Neighbours($Node); foreach ($Neighbours as $Neighbour) { $G = $this->Cache[$Node]['G'] + $this->Graph->G($Node, $Neighbour); if (isset($this->Cache[$Neighbour]) && $this->Cache[$Neighbour]['Status'] && $this->Cache[$Neighbour]['G'] <= $G) { continue; } $F = $G + $this->Graph->H($Neighbour, $NodeEnd); $this->Cache[$Neighbour] = array('G' => $G, 'F' => $F, 'Parent' => $Node, 'Status' => STATUS_OPEN); $Queue->insert($Neighbour, $F); } ++$Closed; $this->Cache[$Node]['Status'] = STATUS_CLOSED; } if ($Found) { $Path = array(); $Node = $NodeEnd; while ($NodeStart != $Node) { $Path[] = $Node; $Node = $this->Cache[$Node]['Parent']; } $Path[] = $Node; return array_reverse($Path); } $this->Debug = 'Path not found, ran out of open nodes.'; return NULL; }