def main(*argv): "Demostration program number 2." print Demo2.main.__doc__ StackAsArray.main(*argv) StackAsLinkedList.main(*argv) QueueAsArray.main(*argv) QueueAsLinkedList.main(*argv) DequeAsArray.main(*argv) DequeAsLinkedList.main(*argv) return 0
def search(self, initial): """ (BreadthFirstSolver, Solution) -> Solution Does a breadth-first traversal of the solution space starting from the given node. """ queue = QueueAsLinkedList() queue.enqueue(initial) while not queue.isEmpty: current = queue.dequeue() if current.isComplete: self.updateBest(current) else: for soln in current.successors: queue.enqueue(soln)
def breadthFirstGenerator(self): """ (Tree) -> generator Yields the keys in this tree in depth-first traversal order. """ queue = QueueAsLinkedList() if not self.isEmpty: queue.enqueue(self) while not queue.isEmpty: head = queue.dequeue() yield head.key for i in xrange(head.degree): child = head.getSubtree(i) if not child.isEmpty: queue.enqueue(child)
def breadthFirstTraversal(self, visitor): """ (Tree, Visitor) -> None Makes the given visitor do a breadth-first traversal of this tree. """ assert isinstance(visitor, Visitor) queue = QueueAsLinkedList() if not self.isEmpty: queue.enqueue(self) while not queue.isEmpty and not visitor.isDone: head = queue.dequeue() visitor.visit(head.key) for i in xrange(head.degree): child = head.getSubtree(i) if not child.isEmpty: queue.enqueue(child)
def search(self, initial): """ (BreadthFirstBranchAndBoundSolver, Solution) -> Solution Does a breadth-first traversal of the solution space starting from the given node. """ queue = QueueAsLinkedList() if initial.isFeasible: queue.enqueue(initial) while not queue.isEmpty: current = queue.dequeue() if current.isComplete: self.updateBest(current) else: for successor in current.successors: if successor.isFeasible and \ successor.bound < self._bestObjective: queue.enqueue(successor)
def breadthFirstTraversal(tree): """ (Tree) -> None Does a breadth-first traversal of a tree and prints the keys. """ queue = QueueAsLinkedList() #! queue.enqueue(tree) #[ if not tree.isEmpty: queue.enqueue(tree) #] while not queue.isEmpty: t = queue.dequeue() print((t.key)) for i in range(t.degree): subTree = t.getSubtree(i) #! queue.enqueue(subTree) #[ if not subTree.isEmpty: queue.enqueue(subTree)
def breadthFirstTraversal(self, visitor, start): """ (Graph, Visitor, Vertex) -> None Makes the given visitor visit the vertices of this graph in breadth-first traversal order starting from the given vertex. """ assert isinstance(visitor, Visitor) enqueued = Array(self._numberOfVertices) for v in xrange(self._numberOfVertices): enqueued[v] = False queue = QueueAsLinkedList() queue.enqueue(self[start]) enqueued[start] = True while not queue.isEmpty and not visitor.isDone: v = queue.dequeue() visitor.visit(v) for to in v.successors: if not enqueued[to.number]: queue.enqueue(to) enqueued[to.number] = True
def topologicalOrderTraversal(self, visitor): """ (Graph, Visitor) -> None Makes the given visitor visit the vertices of this graph in topological order. """ inDegree = Array(self.numberOfVertices) for v in xrange(self.numberOfVertices): inDegree[v] = 0 for e in self.edges: inDegree[e.v1.number] += 1 queue = QueueAsLinkedList() for v in xrange(self.numberOfVertices): if inDegree[v] == 0: queue.enqueue(self[v]) while not queue.isEmpty and not visitor.isDone: v = queue.dequeue() visitor.visit(v) for to in v.successors: inDegree[to.number] -= 1 if inDegree[to.number] == 0: queue.enqueue(to)
def breadthFirstTraversal(tree): """ (Tree) -> None Does a breadth-first traversal of a tree and prints the keys. """ queue = QueueAsLinkedList() #! queue.enqueue(tree) #[ if not tree.isEmpty: queue.enqueue(tree) #] while not queue.isEmpty: t = queue.dequeue() print t.key for i in xrange(t.degree): subTree = t.getSubtree(i) #! queue.enqueue(subTree) #[ if not subTree.isEmpty: queue.enqueue(subTree)