示例#1
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        q = Queue()
        q.en(starting_vertex)
        visited = set()

        while q.size() > 0:
            curr = q.de()
            if curr not in visited:
                visited.add(curr)

            neighbors = self.get_neighbors(curr)

            for neighbor in neighbors:
                if neighbor not in visited:
                    q.en(neighbor)

        for x in visited:
            print(x)
示例#2
0
def bfs(index, room_id, next_room_id):
    q = Queue()
    visited = set()
    path_to_room = []
    q.en([room_id])

    while q.size() > 0:
        curr_path = q.de()
        curr = curr_path[-1]
        if room_id != next_room_id and (curr == next_room_id
                                        or next_room_id in curr_path):
            del curr_path[0]
            path_to_room = reversed(curr_path)
            break
        if curr not in visited:
            visited.add(curr)
            neighbors = world.rooms[curr].get_exits_ids()
            for neighbor in neighbors:
                path_copy = curr_path[:]
                path_copy.append(neighbor)
                q.en(path_copy)
    for path in path_to_room:
        traversal_order.insert(index + 1, path)
示例#3
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        q = Queue()
        visited = set()
        path = [starting_vertex]
        q.en(path)

        while q.size() > 0:
            curr_path = q.de()
            curr = curr_path[-1]
            if curr == destination_vertex:
                return curr_path
            if curr not in visited:
                visited.add(curr)
                neighbors = self.get_neighbors(curr)
                for neighbor in neighbors:
                    path_copy = curr_path[:]
                    path_copy.append(neighbor)

                    q.en(path_copy)