/** * Constructs a QueueAsLinkedList_Iterator for the given queue. * * @param object QueueAsLinkedList $queue A queue. */ public function __construct(QueueAsLinkedList $queue) { parent::__construct(); $this->queue = $queue; $this->position = $queue->getList()->getHead(); $this->key = 0; }
/** * Searches the solution space starting from the specified initial node. * * @param object ISolution $initial The root node of the solution space. */ protected function search(ISolution $initial) { $queue = new QueueAsLinkedList(); $queue->enqueue($initial); while (!$queue->isEmpty()) { $solution = $queue->dequeue(); if ($solution->isComplete()) { $this->updateBest($solution); } else { foreach ($solution->getSuccessors() as $successor) { $queue->enqueue($successor); } } } }
/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on succes; non-zero on failure. */ public static function main($args) { printf("Demonstration program number 2.\n"); $status = 0; StackAsArray::main($args); StackAsLinkedList::main($args); QueueAsArray::main($args); QueueAsLinkedList::main($args); DequeAsArray::main($args); DequeAsLinkedList::main($args); return $status; }
/** * Causes a visitor to visit the nodes of this tree * in breadth-first traversal order starting from this node. * This method invokes the <code>visit</code> method of the visitor * for each node in this tree. * Uses a queue to keep track of the nodes to be visited. * The traversal continues as long as the <code>isDone</code> * method of the visitor returns false. * * @param object IVisitor $visitor The visitor to accept. */ public function breadthFirstTraversal(IVisitor $visitor) { $queue = new QueueAsLinkedList(); if (!$this->isEmpty()) { $queue->enqueue($this); } while (!$queue->isEmpty()) { $head = $queue->dequeue(); for ($i = 1; $i <= $head->getDegree() - 1; ++$i) { $visitor->visit($head->getKeyN($i)); } for ($i = 0; $i <= $head->getDegree() - 1; ++$i) { $child = $head->getSubtree($i); if (!$child->isEmpty()) { $queue->enqueue($child); } } } }
/** * Causes a visitor to visit the vertices of this graph * in topological order. * This method takes a visitor and, * as long as the IsDone method of that visitor returns false, * this method invokes the Visit method of the visitor * for each vertex in the graph. * The order in which the vertices are visited * is given by a topological sort of the vertices. * * @param object IVisitor $visitor The visitor to accept. */ public function topologicalOrderTraversal(IVisitor $visitor) { $inDegree = new BasicArray($this->numberOfVertices); for ($v = 0; $v < $this->numberOfVertices; ++$v) { $inDegree[$v] = 0; } foreach ($this->getEdges() as $edge) { $to = $edge->getV1(); $inDegree[$to->getNumber()] += 1; } $queue = new QueueAsLinkedList(); for ($v = 0; $v < $this->numberOfVertices; ++$v) { if ($inDegree[$v] == 0) { $queue->enqueue($this->vertex[$v]); } } while (!$queue->isEmpty() && !$visitor->isDone()) { $v = $queue->dequeue(); $visitor->visit($v); foreach ($v->getSuccessors() as $to) { $inDegree[$to->getNumber()] -= 1; if ($inDegree[$to->getNumber()] == 0) { $queue->enqueue($to); } } } }
/** * Destructor. */ public function __destruct() { parent::__destruct(); }
/** * Traverses a tree breadth-first, printing each node as it is visited. * Uses a queue to keep track of the nodes to be visited. * * @param object ITree $tree The tree to traverse. */ public static function breadthFirstTraversal(ITree $tree) { $queue = new QueueAsLinkedList(); if (!$tree->isEmpty()) { $queue->enqueue($tree); } while (!$queue->isEmpty()) { $t = $queue->dequeue(); printf("%s\n", str($t->getKey())); for ($i = 0; $i < $t->getDegree(); ++$i) { $subTree = $t->getSubtree($i); if (!$subTree->isEmpty()) { $queue->enqueue($subTree); } } } }