Exemplo n.º 1
0
    def bfs(self, rootId):
        """
        Execute a Breadth-First Search (BFS) in the graph starting from the
        specified node.
        :param rootId: the root node ID (integer).
        :return: the BFS list of nodes.
        """
        # if the root does not exists, return None
        if rootId not in self.nodes:
            return None

        # BFS nodes initialization
        bfs_nodes = []

        # queue initialization
        q = Queue()
        q.enqueue(rootId)

        explored = {rootId}  # nodes already explored

        while not q.isEmpty():  # while there are nodes to explore ...
            node = q.dequeue()  # get the node from the queue
            explored.add(node)  # mark the node as explored
            # add all adjacent unexplored nodes to the queue
            for adj_node in self.getAdj(node):
                if adj_node not in explored:
                    q.enqueue(adj_node)
            bfs_nodes.append(node)

        return bfs_nodes
Exemplo n.º 2
0
 def BFS(self):
     """
     Permette di restituire una lista di elementi ottenuta da una visita
     in ampiezza dell'albero.
     :return: lista nodi
     """
     res = []
     q = Queue()
     if self.root is not None:
         q.enqueue(self.root)
     while not q.isEmpty():
         current = q.dequeue()
         res.append(current.info)
         if current.leftSon is not None:
             q.enqueue(current.leftSon)
         if current.rightSon is not None:
             q.enqueue(current.rightSon)
     return res
Exemplo n.º 3
0
    print("Required time: ", elapsed, "seconds.")


# to run this module directly (NOT imported in another one)
if __name__ == "__main__":

    print("\nEnqueueing elements...\n")

    print("CodaListaCollegata")
    ql = CodaListaCollegata()
    enqueueTest(ql)

    print("Coda - ArrayList")
    qal = CodaArrayList()
    enqueueTest(qal)

    print("Coda - ArrayList_deque")
    qald = CodaArrayList_deque()
    enqueueTest(qald)

    print("\nDequeueing elements\n")

    print("Coda - ListaCollegata")
    dequeueTest(ql)

    print("Coda - ArrayList")
    dequeueTest(qal)

    print("Coda - ArrayList_deque")
    dequeueTest(qald)