Ejemplo n.º 1
0
    def breadth_search(self, initial_vertex):
        for key in self.__adjacent_list:
            if key != initial_vertex:
                # seta cor branca p/ todos, menos o vertex inicial
                key.set_color(0)
                self.__distance[key] = float("inf")
                self.__predecessors[key] = None

        if not initial_vertex:
            return self.__distance

        initial_vertex.set_color(1)  # seta cor do initial_vertex p cinza
        self.__distance[initial_vertex] = 0
        q = queue.Queue()
        q.put(initial_vertex)  # enfileiro o mocinho inicial

        while not q.empty():  # enquanto a fila nao estiver vazia
            vertex = q.get()

            for v in self.__adjacent_list[vertex]:
                if v.get_color() == 0:  # igual a branco
                    v.set_color(1)  # seta p/ cinza
                    self.__distance[v] = self.__distance[vertex] + 1
                    self.__predecessors[v] = vertex
                    q.put(v)
            vertex.set_color(2)  # seta p/ preto

        return self.__distance
Ejemplo n.º 2
0
    def __dfs_visit(self, vertex):
        self.__time = self.__time + 1
        self.__firstSee[vertex] = self.__time
        vertex.set_color(1)

        for adjacent in self.__adjacent_list[vertex]:
            if adjacent.get_color() == 0:
                self.__predecessors[adjacent] = vertex
                self.__dfs_visit(adjacent)
        vertex.set_color(2)
        self.__time += 1
        self.__close[vertex] = self.__time
Ejemplo n.º 3
0
    def breadth_first_search(self, initial_vertex):
        """Calculate the distance of all vertex from one
        if a vertex can't be reach by the initial vertex,
        the distance will be infinity.. (float("inf"))

        Args:
            initial_vertex (Vetex): calculate the distance of
                all vertices up to this initial vertex

        Returns:
            Dict: dictionaty with the key as the vertex and the body
                the distance from the initial vertex
        """

        # colors:
        #   white: not visited
        #   grey: in the queue
        #   black: nothing more to do
        for key in self.__adjacent_list:
            if key != initial_vertex:
                # set color for all vertices except the initial one to white
                key.set_color(0)
                self.__distance[key] = float("inf")
                self.__predecessors[key] = None

        # if the initial_vertex is not a valid one,
        # all the vertex will have distance equals to infinity
        if not initial_vertex:
            return self.__distance

        initial_vertex.set_color(1)  # inital vertex color to grey
        self.__distance[initial_vertex] = 0
        q = queue.Queue()
        q.put(initial_vertex)  # insert in the queue the initial vertex

        while not q.empty():
            vertex = q.get()

            for v in self.__adjacent_list[vertex]:
                if v.get_color() == 0:  # if a vertex color is white
                    v.set_color(1)  # turn to grey
                    self.__distance[v] = self.__distance[
                        vertex] + self.get_edge_from_souce_destination(
                            v, vertex).get_value()
                    self.__predecessors[v] = vertex
                    q.put(v)
            vertex.set_color(2)  # color to black

        return self.__distance