当前位置: 首页>>代码示例>>Python>>正文


Python GraphSet.connected_components方法代码示例

本文整理汇总了Python中graphillion.GraphSet.connected_components方法的典型用法代码示例。如果您正苦于以下问题:Python GraphSet.connected_components方法的具体用法?Python GraphSet.connected_components怎么用?Python GraphSet.connected_components使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在graphillion.GraphSet的用法示例。


在下文中一共展示了GraphSet.connected_components方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_graphs

# 需要导入模块: from graphillion import GraphSet [as 别名]
# 或者: from graphillion.GraphSet import connected_components [as 别名]
    def test_graphs(self):
        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # any subgraph
        gs = GraphSet.graphs()
        self.assertTrue(isinstance(gs, GraphSet))
        self.assertEqual(len(gs), 2**7)
        self.assertTrue([(1, 2)] in gs)

        # subgraphs separating [1, 5] and [2]
        gs = GraphSet.graphs(vertex_groups=[[1, 5], [2]])
        self.assertEqual(len(gs), 6)
        self.assertTrue([(1, 4), (4, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (4, 5)] not in gs)

        # matching
        dc = {}
        for v in range(1, 7):
            dc[v] = range(0, 2)
        gs = GraphSet.graphs(degree_constraints=dc)
        self.assertEqual(len(gs), 22)
        self.assertTrue([(1, 2), (3, 6)] in gs)
        self.assertTrue([(1, 2), (2, 3), (3, 6)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 4)

        # subgraphs with 1 or 2 edges
        gs = GraphSet.graphs(num_edges=range(1, 3))
        self.assertEqual(len(gs), 28)
        for g in gs:
            self.assertTrue(1 <= len(g) and len(g) < 3)

        # single connected component and vertex islands
        gs = GraphSet.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 80)
        self.assertTrue([(1, 2), (2, 3)] in gs)
        self.assertTrue([(1, 2), (2, 3), (4, 5)] not in gs)

        # any forest
        gs = GraphSet.graphs(no_loop=True)
        self.assertEqual(len(gs), 112)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertTrue(len(g) < 6)

        # constrained by GraphSet
        gs = GraphSet.graphs(no_loop=True)
        gs = gs.graphs(vertex_groups=[[]])
        self.assertEqual(len(gs), 66)
        self.assertTrue([(1, 2), (1, 4), (2, 5)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # single connected components across 1, 3, and 5
        gs = GraphSet.connected_components([1, 3, 5])
        self.assertEqual(len(gs), 35)
        self.assertTrue([(1, 2), (2, 3), (2, 5)] in gs)
        self.assertTrue([(1, 2), (2, 3), (5, 6)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4),
                               (2, 5), (3, 4), (3, 5), (4, 5)])

        # cliques with 4 vertices
        gs = GraphSet.cliques(4)
        self.assertEqual(len(gs), 5)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] in gs)
        self.assertTrue([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 5)] not in gs)

        GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
                               (5, 6)])

        # trees rooted at 1
        gs = GraphSet.trees(1)
        self.assertEqual(len(gs), 45)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (4, 5)] not in gs)

        # spanning trees
        gs = GraphSet.trees(is_spanning=True)
        self.assertEqual(len(gs), 15)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5), (4, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 5)

        # forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3])
        self.assertEqual(len(gs), 54)
        self.assertTrue([] in gs)
        self.assertTrue([(1, 2), (2, 3)] not in gs)

        # spanning forests rooted at 1 and 3
        gs = GraphSet.forests([1, 3], is_spanning=True)
        self.assertEqual(len(gs), 20)
        self.assertTrue([(1, 2), (1, 4), (2, 5), (3, 6)] in gs)
        self.assertTrue([(1, 2), (1, 4), (2, 3), (2, 5)] not in gs)
        for g in gs:
            self.assertEqual(len(g), 4)

#.........这里部分代码省略.........
开发者ID:takemaru,项目名称:graphillion,代码行数:103,代码来源:graphset.py


注:本文中的graphillion.GraphSet.connected_components方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。