Exemplo n.º 1
0
    def test_get_connected_components(self):
        """Get connected components of a graph."""
        graph = Graph(is_directed=False)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_d = graph.add_vertex('D')
        vertex_d = graph.add_vertex('E')
        vertex_d = graph.add_vertex('F')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')
        graph.add_edge('D', 'E')

        expected_components = [['A', 'B', 'C'], ['D', 'E'], ['F']]
        # sort each component for ease of comparison
        actual_components = graph.find_connected_components()
        actual_components = [sorted(comp) for comp in actual_components]

        self.assertCountEqual(expected_components, actual_components)