def simulate_hot_potato(names, number_of_passes): queue = QueueLL() for name in names: queue.enqueue(name) for numberOfPeople in range(queue.size()): print("{0} {1}".format(queue.peek(), "has the potato!")) i = number_of_passes while i > 0: queue.enqueue(queue.dequeue()) print("{0} {1}".format("-> pass to", queue.peek())) i -= 1 print("{0} {1}".format(queue.dequeue(), "is out!"))
def topological_sort(self): reverse_order_queue = QueueLL() self.clean_vertices() if self.vertex_count > 0: for v in self.get_vertices(): if v.color == "WHITE": self.__topo_sort_helper(v, reverse_order_queue) items = [] while reverse_order_queue.size() > 0: items.insert(0, reverse_order_queue.dequeue()) print("\nOrder of execution:") print(" -> ".join(x.get_id() for x in items)) return items
def testLLQueueTest(self): queue = QueueLL() self.assertEqual(queue.size(), 0) self.assertTrue(queue.is_empty()) self.assertEqual(queue.dequeue(), None) queue.enqueue("First") queue.enqueue(2) queue.enqueue("Third") self.assertEqual(queue.size(), 3) self.assertFalse(queue.is_empty()) self.assertEqual(queue.dequeue(), "First") self.assertEqual(queue.dequeue(), 2) self.assertEqual(queue.dequeue(), "Third") self.assertEqual(queue.dequeue(), None) queue.enqueue("Fourth") self.assertEqual(queue.dequeue(), "Fourth") queue.enqueue("Uno") queue.enqueue("Dos") queue.clear() self.assertEqual(queue.is_empty(), True)
def testLLQueueTest(self): queue = QueueLL() self.assertEqual(queue.size(),0) self.assertTrue(queue.is_empty()) self.assertEqual(queue.dequeue(), None) queue.enqueue("First") queue.enqueue(2) queue.enqueue("Third") self.assertEqual(queue.size(),3) self.assertFalse(queue.is_empty()) self.assertEqual(queue.dequeue(), "First") self.assertEqual(queue.dequeue(), 2) self.assertEqual(queue.dequeue(), "Third") self.assertEqual(queue.dequeue(), None) queue.enqueue("Fourth") self.assertEqual(queue.dequeue(), "Fourth") queue.enqueue("Uno") queue.enqueue("Dos") queue.clear() self.assertEqual(queue.is_empty(), True)
def bfs(self, start, end): if start not in self.vertices or end not in self.vertices: return start_vertex = self.get_vertex(start) self.clean_vertices() queue = QueueLL() queue.enqueue(start_vertex) start_vertex.color = "GRAY" while queue.size() > 0: iteration_vertex = queue.dequeue() for neighbor in iteration_vertex.get_connections(): if neighbor.color == "WHITE": queue.enqueue(neighbor) neighbor.color = "GRAY" neighbor.distance = iteration_vertex.distance + 1 if neighbor.get_id() == end: print("BFS: Found vertex '{0}' at a distance of {1} from '{2}'" .format(neighbor.get_id(), neighbor.distance, start)) return True iteration_vertex.color = "BLACK"
def bfs(self, start, end): if start not in self.vertices or end not in self.vertices: return start_vertex = self.get_vertex(start) self.clean_vertices() queue = QueueLL() queue.enqueue(start_vertex) start_vertex.color = "GRAY" while queue.size() > 0: iteration_vertex = queue.dequeue() for neighbor in iteration_vertex.get_connections(): if neighbor.color == "WHITE": queue.enqueue(neighbor) neighbor.color = "GRAY" neighbor.distance = iteration_vertex.distance + 1 if neighbor.get_id() == end: print( "BFS: Found vertex '{0}' at a distance of {1} from '{2}'" .format(neighbor.get_id(), neighbor.distance, start)) return True iteration_vertex.color = "BLACK"