def putTo(self, queueLabel, func, args, kwargs): if queueLabel in self.childQueues: self.childQueues[queueLabel].put((func, args, kwargs)) else: self.childQueues[queueLabel] = Queue.Queue() self.childQueues[queueLabel].put((func, args, kwargs)) StartDaemonThread(workAt, self.childQueues[queueLabel])
def setUp(self) -> None: self.__queue = Queue() self.__queue.enqueue(1) self.__queue.enqueue(2) self.__queue.enqueue(3) self.__queue.enqueue(4) self.__queue.enqueue(5)
def bfs(self) -> Generator[V, None, None]: visited = set() queue: Queue = Queue() if self.__adjacencyMatrix.isDigraph(): for v in self.__adjacencyMatrix.zeroInDegreeVertexes(): visited.add(v) queue.enqueue(v) for v in self.__bfsImpl(visited, queue): yield v else: raise Exception("This is not a Digraph")
def bfsFromVertex(self, vertex: V, visited: Set[V] = None) -> Generator[V, None, None]: if vertex is None: raise Exception("Vertex is None") if self.__adjacencyMatrix.checkVertexExist(vertex) is None: raise Exception("Vertex: {} not exist in graph".format(vertex)) if visited is None: visited = set() queue: Queue = Queue() queue.enqueue(vertex) visited.add(vertex) for v in self.__bfsImpl(visited, queue): yield v
def minNumberOfEdges(self, node1: int, node2: int): """ Starts at an arbitrary root node and explores all neighboring node at same level """ nodes_to_check = Queue() nodes_to_check.enqueue(self.getNode(node1)) visited_nodes = [] distances_from_node1 = {} # Use a map to keep track of which node we are referring to while not nodes_to_check.isEmpty(): # Dequeue node curr_node = nodes_to_check.dequeue() if curr_node is not None: if curr_node not in visited_nodes: # Don't enqueue nodes we have seen # print(curr_node.data) visited_nodes.append(curr_node)
def print_level_by_level(self): nodes_to_print = Queue() nodes_to_print.enqueue(self.ceo) new_level = Employee(None, None, None) nodes_to_print.enqueue(new_level) while not nodes_to_print.isEmpty(): # Dequeue node employee = nodes_to_print.dequeue() if employee is not None: employee.print_info() # Add the dequeued node's children (if they exist) to the queue if employee.directReports is not None: for report in employee.directReports: if report is not None: nodes_to_print.enqueue(report) if employee == new_level and not nodes_to_print.isEmpty(): nodes_to_print.enqueue(new_level) # Add space if we reached current end of level (and not the tree)
def BFS(self, key: int): """ Starts at an arbitrary root node and explores all neighboring node at same level """ nodes_to_print = Queue() nodes_to_print.enqueue(self.getNode(key)) visited_nodes = [] # Keep track of nodes printed in order to not print the same node twice while not nodes_to_print.isEmpty(): # Dequeue node curr_node = nodes_to_print.dequeue() if curr_node is not None: if curr_node not in visited_nodes: # Don't enqueue nodes we have seen print(curr_node.data) visited_nodes.append(curr_node) children = self.getAdjNodes(curr_node.data) if children: for child in children: if child not in visited_nodes: nodes_to_print.enqueue(child)
def print_num_levels(self): level_count = 0 if self.ceo is not None: nodes_to_check = Queue() nodes_to_check.enqueue(self.ceo) level_count += 1 new_level = Employee(None, None, None) nodes_to_check.enqueue(new_level) while not nodes_to_check.isEmpty(): # Dequeue node employee = nodes_to_check.dequeue() if employee is not None: # Add the dequeued node's children (if they exist) to the queue if employee.directReports is not None: for report in employee.directReports: if report is not None: nodes_to_check.enqueue(report) if employee == new_level and not nodes_to_check.isEmpty(): nodes_to_check.enqueue( new_level ) # Add space if we reached current end of level level_count += 1 print(level_count)
def addWorkerTo(self, queueLabel, n): if queueLabel not in self.childQueues: self.childQueues[queueLabel] = Queue.Queue() for i in range(n): StartDaemonThread(workAt, self.childQueues[queueLabel])
def __init__(self): self.mainQueue = Queue.Queue() self.childQueues = {}