コード例 #1
0
    def create_graph_from_edges(self, edges):

        graph = undirected_graph.Graph()

        for e in edges:
            graph.add_edge(edge=e)

        return graph
コード例 #2
0
    def test_1(self):

        graph = undirected_graph.Graph()
        graph.add_edge(edge=undirected_graph.Edge(vertex1=1, vertex2=1))

        self.assertEqual(
            list(undirected_graph.connected_components(graph=graph)),
            [[1]],
        )
コード例 #3
0
    def test_2(self):

        graph = undirected_graph.Graph()
        graph.add_edge(edge=undirected_graph.Edge(vertex1=1, vertex2=2))

        self.assertEqual(
            list(
                sorted(c)
                for c in undirected_graph.connected_components(graph=graph)),
            [[1, 2]],
        )

        graph = undirected_graph.Graph()
        graph.add_vertex(1)
        graph.add_vertex(2)

        self.assertEqual(
            sorted(undirected_graph.connected_components(graph=graph)),
            [[1], [2]],
        )
コード例 #4
0
    def test_3(self):

        graph = undirected_graph.Graph()
        graph.add_edge(undirected_graph.Edge(vertex1=1, vertex2=2))
        graph.add_edge(undirected_graph.Edge(vertex1=2, vertex2=3))
        graph.add_edge(undirected_graph.Edge(vertex1=4, vertex2=5))
        graph.add_edge(undirected_graph.Edge(vertex1=5, vertex2=6))
        graph.add_edge(undirected_graph.Edge(vertex1=5, vertex2=7))
        graph.add_edge(undirected_graph.Edge(vertex1=6, vertex2=7))

        self.assertEqual(
            sorted(
                sorted(c)
                for c in undirected_graph.connected_components(graph=graph)),
            [[1, 2, 3], [4, 5, 6, 7]],
        )
コード例 #5
0
    def test_graph(self):

        self.build_and_test_graph(graph=undirected_graph.Graph())