Skip to content

Commit

Permalink
Trac #23275: Bipartite graphs should not accept loops
Browse files Browse the repository at this point in the history
Loops should not be allowed in a bipartite graph, but we can currently
do:
{{{
sage: B = BipartiteGraph(loops=True, multiedges=True)
sage: B.add_edge(0, 0)
sage: B.is_bipartite()
False
sage: B.add_edge(1, 1)
sage: B.add_edge(2, 2)
sage: B.edges(labels=0)
[(0, 0), (1, 1), (2, 2)]
sage: B.add_edge(0, 1, 'a')
------------------------------------------------------------------------
---
RuntimeError                              Traceback (most recent call
last)
...
RuntimeError: Edge vertices must lie in different partitions.
sage: B.add_edge(3, 3)
sage: B.edges()
[(0, 0, None), (1, 1, None), (2, 2, None), (3, 3, None)]
}}}
When adding edges in a different order, the behavior is more consistent.
{{{
sage: B = BipartiteGraph(loops=True, multiedges=True)
sage: B.add_edge(0, 1, 'a')
sage: B.add_edge(0, 1, 'b')
sage: B.is_bipartite()
True
sage: B.add_edge(0,0)
------------------------------------------------------------------------
---
RuntimeError                              Traceback (most recent call
last)
...
RuntimeError: Edge vertices must lie in different partitions.
}}}

URL: https://trac.sagemath.org/23275
Reported by: dcoudert
Ticket author(s): David Coudert
Reviewer(s): Zachary Gershkoff
  • Loading branch information
Release Manager authored and vbraun committed Sep 5, 2017
2 parents f34394d + 3c0c3d4 commit 5fdf5a3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/sage/graphs/bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,20 @@ class BipartiteGraph(Graph):
sage: sorted(g.left.union(g.right))
[0, 1, 2, 3, 4, 5, 6, 7]
Make sure that loops are not allowed (:trac:`23275`)::
sage: B = BipartiteGraph(loops=True)
Traceback (most recent call last):
...
ValueError: loops are not allowed in bipartite graphs
sage: B = BipartiteGraph(loops=None)
sage: B.allows_loops()
False
sage: B.add_edge(0,0)
Traceback (most recent call last):
...
ValueError: cannot add edge from 0 to 0 in graph without loops
"""

def __init__(self, data=None, partition=None, check=True, *args, **kwds):
Expand All @@ -264,6 +277,13 @@ def __init__(self, data=None, partition=None, check=True, *args, **kwds):
sage: partition = [list(range(5)), list(range(5,10))]
sage: B = BipartiteGraph(P, partition, check=False)
"""
if kwds is None:
kwds = {'loops': False}
else:
if kwds.has_key('loops') and kwds['loops'] is True:
raise ValueError('loops are not allowed in bipartite graphs')
kwds['loops'] = False

if data is None:
if partition is not None and check:
if partition[0] or partition[1]:
Expand Down Expand Up @@ -798,6 +818,31 @@ def add_edge(self, u, v=None, label=None):
Graph.add_edge(self, u, v, label)
return

def allow_loops(self, new, check=True):
"""
Change whether loops are permitted in the (di)graph
.. NOTE::
This method overwrite the
:meth:`~sage.graphs.generic_graph.GenericGraph.allow_loops` method
to ensure that loops are forbidden in :class:`~BipartiteGraph`.
INPUT:
- ``new`` - boolean.
EXAMPLES::
sage: B = BipartiteGraph()
sage: B.allow_loops(True)
Traceback (most recent call last):
...
ValueError: loops are not allowed in bipartite graphs
"""
if new is True:
raise ValueError("loops are not allowed in bipartite graphs")

def complement(self):
"""
Return a complement of this graph.
Expand Down

0 comments on commit 5fdf5a3

Please sign in to comment.