Skip to content

Commit

Permalink
Add regular_bipartite_graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
junkawahara committed Sep 15, 2024
1 parent 16a21db commit ea9fd8f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
39 changes: 38 additions & 1 deletion graphillion/graphset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2422,8 +2422,10 @@ def regular_graphs(degree=None, is_connected=True, graphset=None):
if degree == None:
degree = (1, len(GraphSet._vertices))

ss = None if graphset is None else graphset._ss

ss = _graphillion._regular_graphs(graph=graph,
degree=degree, is_connected=is_connected,graphset=graphset)
degree=degree, is_connected=is_connected,graphset=ss)
return GraphSet(ss)

@staticmethod
Expand Down Expand Up @@ -2537,6 +2539,41 @@ def bipartite_graphs(graphset=None):

return graphset.non_supergraphs(odd_cycle_gs)

@staticmethod
def regular_bipartite_graphs(degree=None, is_connected=True, graphset=None):
"""Returns a GraphSet of regular bipartite subgraphs.
Example:
>>> GraphSet.regular_bipartite_graphs()
# The degree is 3 and the graphs are not necessarily connected.
>>> GraphSet.regular_graphs(3, False)
# The degree is at least 2 and at most 5, and
# the graphs are connected.
>>> GraphSet.regular_graphs((2, 5), True)
Args:
degree: Tuple or Integer. If it is a tuple (l, u),
the degree is at least l and at most u.
If it is an integer d, the degree is d.
If it is None, the degree is arbitrary.
is_connected: Bool. If it is True, the graphs are
connected. If it is False, the graphs are
not necessarily connected.
graphset: Optional. A GraphSet object. Subgraphs to be stored
are selected from this object.
Returns:
A new GraphSet object.
See Also:
regular_graphs()
bipartite_graphs()
"""
bi_graphs = GraphSet.bipartite_graphs(graphset=graphset)
return GraphSet.regular_graphs(degree=degree, is_connected=is_connected, graphset=bi_graphs)

@staticmethod
def show_messages(flag=True):
"""Enables/disables status messages.
Expand Down
16 changes: 16 additions & 0 deletions graphillion/test/graphset.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,22 @@ def test_regular_graphs(self):
self.assertTrue([(2, 4), (2, 5), (3, 4), (3, 5)] in gs)
self.assertTrue([(1, 2)] not in gs)

def test_regular_bipartite_graphs(self):
GraphSet.set_universe([(1, 2), (1, 4), (1, 5), (2, 3), (2, 5),
(3, 6), (4, 5), (5, 6)])

gs = GraphSet.regular_bipartite_graphs(is_connected=False)
self.assertEqual(len(gs), 27)
self.assertTrue([(1, 2), (1, 4), (2, 3), (3, 6), (4, 5), (5, 6)] in gs)
self.assertTrue([(1, 4), (2, 5), (3, 6)] in gs)
self.assertTrue([(1, 2), (1, 5), (2, 3), (3, 6), (5, 6)] not in gs)

gs = GraphSet.regular_bipartite_graphs(is_connected=True)
self.assertEqual(len(gs), 11)
self.assertTrue([(1, 2), (1, 4), (2, 3), (3, 6), (4, 5), (5, 6)] in gs)
self.assertTrue([(1, 4), (2, 5), (3, 6)] not in gs)
self.assertTrue([(1, 2), (1, 5), (2, 3), (3, 6), (5, 6)] not in gs)

def test_partitions(self):
GraphSet.set_universe([(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5),
(5, 6)])
Expand Down

0 comments on commit ea9fd8f

Please sign in to comment.