def unite(self, vertex_x, vertex_y): """Combine the sets containing. Args: vertex_x: element first set vertex_y: element second set Raises: NotContainsElementException: if dsu not contains vertex DataStructureIsEmptyException: if dsu is empty """ if self.size == 0: raise DataStructureIsEmptyException(data_structure_is_empty_message()) if vertex_x not in self._set: raise NotContainsElementException(not_contains_element_message()) if vertex_y not in self._set: raise NotContainsElementException(not_contains_element_message()) parent_x = self.find(vertex_x) parent_y = self.find(vertex_y) if secrets.randbelow(10) % 2 == 0: self._set[parent_x] = parent_y else: self._set[parent_y] = parent_x
def test_dijkstra_negative1(f): graph = Graph() with pytest.raises(DataStructureIsEmptyException) as exception_info: f(graph, Vertex('A')) assert str(exception_info.value) == data_structure_is_empty_message()
def test_get_vertex_by_id_negative2(): graph = Graph() with pytest.raises(DataStructureIsEmptyException) as exception_info: vertex = graph.get_vertex_by_id('A') assert str(exception_info.value) == data_structure_is_empty_message()
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)
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)
def find(self, vertex): """Return unique id of set. Args: vertex: element for search id Raises: NotContainsElementException: if dsu not contains vertex DataStructureIsEmptyException: if dsu is empty Returns: id: unique set identifier found by arg """ if self.size == 0: raise DataStructureIsEmptyException(data_structure_is_empty_message()) if vertex not in self._set: raise NotContainsElementException(not_contains_element_message()) if self._set[vertex] == vertex: return vertex self._set[vertex] = self.find(self._set[vertex]) return self._set[vertex]
def test_add_edge_in_empty_graph(): graph = Graph() with pytest.raises(DataStructureIsEmptyException) as exception_info: graph.add_edge('A', 'B') assert str(exception_info.value) == data_structure_is_empty_message()