Exemple #1
0
    def add_edge(self, from_vertex, to_vertex, weight=0):
        """Add an edge between two vertices.

        Args:
            from_vertex: start vertex id
            to_vertex: end vertex id
            weight (int): weight of edge

        Raises:
            DataStructureIsEmptyException: if graph is empty
            NotContainsElementException: if vertex not contains in graph
        """
        if self.is_empty:
            raise DataStructureIsEmptyException(data_structure_is_empty_message())
        if from_vertex not in self._vertices:
            raise NotContainsElementException(not_contains_vertex_message(from_vertex))
        if to_vertex not in self._vertices:
            raise NotContainsElementException(not_contains_vertex_message(to_vertex))

        first_vertex = self._vertices[from_vertex]
        second_vertex = self._vertices[to_vertex]

        first_vertex.add_adjacent_vertex(second_vertex, weight)

        edge = Edge(first_vertex, second_vertex, weight)
        self._edges.append(edge)
Exemple #2
0
def test_vertex_not_found2():
    graph = Graph()
    graph.create_vertex_by_id('B')

    with pytest.raises(NotContainsElementException) as exception_info:
        graph.add_edge('A', 'B')
    assert str(exception_info.value) == not_contains_vertex_message('A')
Exemple #3
0
def test_dijkstra_negative2(f):
    graph = Graph()

    graph.create_vertex_by_id('A')

    with pytest.raises(NotContainsElementException) as exception_info:
        f(graph, Vertex('B'))

    assert str(exception_info.value) == not_contains_vertex_message('B')
Exemple #4
0
def test_get_vertex_by_id_negative1():
    graph = Graph()

    graph.create_vertex_by_id('B')

    with pytest.raises(NotContainsElementException) as exception_info:
        vertex = graph.get_vertex_by_id('A')

    assert str(exception_info.value) == not_contains_vertex_message('A')
def test_edge_weight_to_negative():
    vertex_s = Vertex('S')
    vertex_a = Vertex('A')
    vertex_b = Vertex('B')

    vertex_s.add_adjacent_vertex(vertex_a)

    with pytest.raises(NotContainsElementException) as exception_info:
        weight = vertex_s.edge_weight_to(vertex_b)

    assert str(exception_info.value) == not_contains_vertex_message('B')
Exemple #6
0
    def edge_weight_to(self, vertex):
        """Return  edge weight to vertex.

        Args:
            vertex: edge from self to vertex

        Raises:
             NotContainsElementException: if vertex not contains in adjacency list

        Returns:
            weight (int) edge weight from self to vertex
        """
        if vertex not in self:
            raise NotContainsElementException(
                not_contains_vertex_message(vertex.identifier))
        return self._adjacency_list[vertex]
Exemple #7
0
    def get_vertex_by_id(self, vertex_id):
        """Return (not extract) vertex from graph.

        Args:
            vertex_id: vertex for return

        Returns:
            vertex: vertex by id

        Raises:
            DataStructureIsEmptyException: if graph is empty
            NotContainsElementException: if graph not contains vertex
        """
        if self.is_empty:
            raise DataStructureIsEmptyException(data_structure_is_empty_message())
        if vertex_id not in self._vertices:
            raise NotContainsElementException(not_contains_vertex_message(vertex_id))
        return self._vertices[vertex_id]
def bellman_ford(graph, start_vertex):
    """Return the distance from the starting vertex to the rest.

    Args:
        graph: graph for search
        start_vertex: start vertex for search

    Raises:
        DataStructureIsEmptyException: if graph is empty
        NotContainsElementException: if graph not contains start vertex

    Returns:
        distances: collection storing the distance from the starting vertex to the rest
    """
    if graph.is_empty:
        raise DataStructureIsEmptyException(data_structure_is_empty_message())
    if start_vertex.identifier not in graph:
        raise NotContainsElementException(
            not_contains_vertex_message(start_vertex.identifier))
    return _bellman_ford(graph, start_vertex)