本文整理汇总了Python中graphillion.GraphSet.graphs方法的典型用法代码示例。如果您正苦于以下问题:Python GraphSet.graphs方法的具体用法?Python GraphSet.graphs怎么用?Python GraphSet.graphs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphillion.GraphSet
的用法示例。
在下文中一共展示了GraphSet.graphs方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _enumerate_forests
# 需要导入模块: from graphillion import GraphSet [as 别名]
# 或者: from graphillion.GraphSet import graphs [as 别名]
def _enumerate_forests(self, suspicious_cut):
vg = [[r] for r in self.graph.roots]
dc = {}
l = len(self.graph.graph.nodes())
for v in self.graph.graph.nodes():
if v in suspicious_cut: dc[v] = 0
elif v in self.graph.roots: dc[v] = xrange(l)
else: dc[v] = xrange(1, l)
return GraphSet.graphs(vertex_groups=vg, degree_constraints=dc,
no_loop=True)
示例2: test_linear_constraints
# 需要导入模块: from graphillion import GraphSet [as 别名]
# 或者: from graphillion.GraphSet import graphs [as 别名]
def test_linear_constraints(self):
GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
(5, 6)])
gs = GraphSet.graphs(linear_constraints=[([(2, 3)], (2, 1))])
self.assertEqual(gs, GraphSet())
gs = GraphSet.graphs(linear_constraints=[([], (1, 2))])
self.assertEqual(gs, GraphSet())
gs = GraphSet.graphs(linear_constraints=[([(5, 6, -1.5)], (-1.5, 0))])
self.assertEqual(gs, GraphSet.graphs())
gs = GraphSet.graphs(linear_constraints=[([(1, 2, 3.14)], (0, float("inf")))])
self.assertEqual(gs, GraphSet.graphs())
gs = GraphSet.graphs(linear_constraints=[(GraphSet.universe(), (0, float("inf")))])
self.assertEqual(gs, GraphSet.graphs())
gs = GraphSet.graphs(linear_constraints=[(GraphSet.universe(), (7, 7))])
self.assertEqual(gs, GraphSet([GraphSet.universe()]))
gs = GraphSet.graphs(linear_constraints=[(GraphSet.universe(), (1, 7))])
self.assertEqual(gs, GraphSet.graphs() - GraphSet([[]]))
gs = GraphSet.graphs(linear_constraints=[([(2, 3, -1), (2, 5, 10), (4, 1),
(3, 6), (4, 5, -1), (5, 6)],
(10, 100))])
self.assertEqual(len(gs), 52)
gs = GraphSet.graphs(linear_constraints=[([(2, 3), (2, 5, -10), (4, 1, -1),
(3, 6, -1), (4, 5), (5, 6, -1)],
(-100, -10))])
self.assertEqual(len(gs), 52)
gs = GraphSet.graphs(linear_constraints=[([(1, 2)], (1, 1)),
([(1, 4)], (1, 2)),
([(2, 3)], (1, 3)),
([(2, 5)], (1, 4)),
([(4, 5)], (1, 6)),
([(5, 6)], (1, 7))])
self.assertEqual(len(gs), 2)
示例3: test_graphs
# 需要导入模块: from graphillion import GraphSet [as 别名]
# 或者: from graphillion.GraphSet import graphs [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)
#.........这里部分代码省略.........
示例4: choose
# 需要导入模块: from graphillion import GraphSet [as 别名]
# 或者: from graphillion.GraphSet import graphs [as 别名]
if a[i][j] != 0:
degs[cellId[i][j]] = 1
if a[i][j] in clue:
clue[a[i][j]].append(cellId[i][j])
else:
clue[a[i][j]] = [cellId[i][j]]
else:
degs[cellId[i][j]] = 2
grps = []
for _,p in clue.items():
grps.append(p)
GraphSet.set_universe(edges)
sols = GraphSet.graphs(vertex_groups=grps,
degree_constraints=degs,
no_loop=True)
print('# of Solutions: %d' % sols.len())
def choose(gs):
for g in gs.rand_iter():
return g
sol = choose(sols)
def output(sol):
sh,sw = h*2+1,w*4+1
s = [[' ' for j in range(sw)] for i in range(sh)]
for i in range(sh):
s[i][0] = s[i][-1] = '|'