Skip to content

Commit 645a08d

Browse files
author
Release Manager
committed
sagemathgh-39754: Fix bipartite graph constructor for reduced adjacency matrix with `immutable=True` Related to issue `sagemath#39295` Previously, when a reduced adjacency matrix was provided and `immutable=True` was set, the graph construction failed. This fix ensures that the graph is correctly constructed and behaves as expected. This issue occurred because the parent class `Graph` constructor was called with `immutable=True` and then attempted to add vertices and edges after construction, which is not allowed. The fix prepares the vertex list and edge list before calling the parent class constructor with the precomputed vertices and edges. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#39754 Reported by: Ziad Tarek Reviewer(s): David Coudert, user202729, Ziad Tarek
2 parents 35b81b8 + 297a4b1 commit 645a08d

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/sage/graphs/bipartite_graph.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ def __init__(self, data=None, partition=None, check=True, hash_labels=None, *arg
391391
Traceback (most recent call last):
392392
...
393393
LookupError: vertex (7) is not a vertex of the graph
394+
395+
Check that :issue:`39295` is fixed::
396+
397+
sage: B = BipartiteGraph(matrix([[1, 1], [1, 1]]), immutable=True)
398+
sage: print(B.vertices(), B.edges())
399+
[0, 1, 2, 3] [(0, 2, None), (0, 3, None), (1, 2, None), (1, 3, None)]
400+
sage: B.add_vertices([4], left=True)
401+
Traceback (most recent call last):
402+
...
403+
ValueError: graph is immutable; please change a copy instead (use function copy())
394404
"""
395405
if kwds is None:
396406
kwds = {'loops': False}
@@ -467,32 +477,31 @@ def __init__(self, data=None, partition=None, check=True, hash_labels=None, *arg
467477
if kwds.get("multiedges", False) and kwds.get("weighted", False):
468478
raise TypeError("weighted multi-edge bipartite graphs from "
469479
"reduced adjacency matrix not supported")
470-
Graph.__init__(self, *args, **kwds)
471480
ncols = data.ncols()
472481
nrows = data.nrows()
473482
self.left = set(range(ncols))
474483
self.right = set(range(ncols, nrows + ncols))
475484

476-
# ensure that the vertices exist even if there
477-
# are no associated edges (trac #10356)
478-
self.add_vertices(self.left)
479-
self.add_vertices(self.right)
480-
481-
if kwds.get("multiedges", False):
482-
for ii in range(ncols):
483-
for jj in range(nrows):
484-
if data[jj, ii]:
485-
self.add_edges([(ii, jj + ncols)] * data[jj, ii])
486-
elif kwds.get("weighted", False):
487-
for ii in range(ncols):
488-
for jj in range(nrows):
489-
if data[jj, ii]:
490-
self.add_edge((ii, jj + ncols, data[jj, ii]))
491-
else:
492-
for ii in range(ncols):
493-
for jj in range(nrows):
494-
if data[jj, ii]:
495-
self.add_edge((ii, jj + ncols))
485+
def edges():
486+
if kwds.get("multiedges", False):
487+
for ii in range(ncols):
488+
for jj in range(nrows):
489+
for _ in range(data[jj, ii]):
490+
yield (ii, jj + ncols)
491+
elif kwds.get("weighted", False):
492+
for ii in range(ncols):
493+
for jj in range(nrows):
494+
if data[jj, ii]:
495+
yield (ii, jj + ncols, data[jj, ii])
496+
else:
497+
for ii in range(ncols):
498+
for jj in range(nrows):
499+
if data[jj, ii]:
500+
yield (ii, jj + ncols)
501+
502+
# ensure that construction works
503+
# when immutable=True (issue #39295)
504+
Graph.__init__(self, data=[range(nrows + ncols), edges()], format='vertices_and_edges', *args, **kwds)
496505
else:
497506
if partition is not None:
498507
left, right = set(partition[0]), set(partition[1])

0 commit comments

Comments
 (0)