コード例 #1
0
 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)
コード例 #2
0
 def __bfsImpl(self, visited: Set[V], queue: Queue):
     while len(queue) > 0:
         v, _ = queue.dequeue()
         for neighbor, _ in self.__adjacencyMatrix.allSuccessors(v):
             if neighbor in visited:
                 continue
             visited.add(neighbor)
             queue.enqueue(neighbor)
         yield v
コード例 #3
0
 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])
コード例 #4
0
 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")
コード例 #5
0
 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
コード例 #6
0
    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)
コード例 #7
0
    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)
コード例 #8
0
    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)
コード例 #9
0
    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)
コード例 #10
0
class QueueTest(unittest.TestCase):
    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 testDequeue(self) -> None:
        self.assertIn(1, self.__queue.dequeue())
        self.assertEqual(4, len(self.__queue))

    def testEnqueue(self) -> None:
        self.__queue.enqueue(6)
        self.assertIn(1, self.__queue.dequeue())
        self.assertEqual(5, len(self.__queue))

    def testFindKey(self) -> None:
        self.assertTrue(self.__queue.findKey(1))
        self.assertFalse(self.__queue.findKey(999999))

    def testEnqueueEmptyKey(self) -> None:
        with self.assertRaises(Exception):
            self.__queue.enqueue(None)

    def testDequeueAll(self) -> None:
        for _ in range(6):
            item = self.__queue.dequeue()
        self.assertTrue(not item)
コード例 #11
0
 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])
コード例 #12
0
 def __init__(self):
     self.mainQueue = Queue.Queue()
     self.childQueues = {}