def has_backup_path(self): for node in self.nodes: self.nodes[node].dist = float("inf") self.nodes[node].pre = None self.nodes[node].pre_door = None self.source.dist = 0 que = Queue() que.enqueue(self.source) while que.size(): first = que.dequeue() if first == self.exit_node: if self.max_exit_dist < first.dist: self.max_exit_dist = first.dist return True for key in first.connections: neighbour = self.nodes[key] vertex = first.room + self.delimiter + key if self.vertices[vertex].capacity - self.vertices[vertex].flow: if first.dist + 1 < neighbour.dist: neighbour.dist = first.dist + 1 neighbour.pre = first neighbour.pre_vertex = self.vertices[vertex] que.enqueue(neighbour) return False
def bft(self, starting_point): q = Queue() visited = [] q.enqueue(starting_point) while q.len() is not 0: n = q.dequeue() if n not in visited: visited.append(n) # print(visited) for i in self.vertices[f"{n}"]: q.enqueue(f"{i}") return visited
def bfs_walk(self): que = Queue() que.enqueue(next(iter(self.nodes.values()))) while que.size() > 0: first = que.dequeue() if first.mark: continue else: print(first.name) first.mark = True for neighbour in first.connections: que.enqueue(self.nodes[neighbour])
def path_exists(g, start, target): start = g[start] queue = Queue() for child in start: queue.enqueue(child) for child in queue: if child.seen: return if child == target: return True else: child.seen = True for child_of_child in child: queue.enqueue(child_of_child) else: return False
def test_queue(): q = Queue([48, 3]) q.enqueue(10) q.enqueue(15) q.enqueue(20) q.enqueue(25) print(q.indexof(25)) print(q) q.clear() # q.pop() print(q._head, q._tail) print(q) q.enqueue(25) q.dequeue() print(q)
def print_level_order(self): q = Queue() tmp = OrderedDict() for index in range(self.get_height(), -2, -1): tmp[index] = [] q.enqueue(self.node) level = 0 while q.size() > 0: actual = q.dequeue() if actual: if level != actual.level: level = actual.level tmp[level].append(str(actual.key)) if actual.left: q.enqueue(actual.left.node) if actual.right: q.enqueue(actual.right.node) parent = actual else: if parent: if parent.left.node or parent.right.node: tmp[level - 1].append("_") if not parent.left.node and not parent.right.node: tmp[level - 1].append("_") parent = actual else: tmp[level - 1].append("_") result = self.format_avl(tmp) print(result)