Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit a62f35e

Browse files
Release Managervbraun
authored andcommitted
Trac #19973: More trouble with immutable graphs
Here is a fix to a few cases where things fail with immutable graphs due to `subgraph` preserving the mutability of the graph. The affected methods are `traveling_salesman_problem` (and then also `is_hamiltonian` and `hamiltonian_cycle`) and `is_clique` with the parameter `directed_clique=True`. These problems have either been missed, or, more likely, introduced in #19526. URL: http://trac.sagemath.org/19973 Reported by: jaanos Ticket author(s): Janoš Vidali Reviewer(s): Nathann Cohen
2 parents 993cf5a + 97ab2e1 commit a62f35e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,17 +7027,21 @@ def traveling_salesman_problem(self, use_edge_labels = False, solver = None, con
70277027
else:
70287028
edges = [(u,v,self.edge_label(u,v)),
70297029
(v,u,self.edge_label(v,u))]
7030-
answer = self.subgraph(edges = edges)
7030+
answer = self.subgraph(edges = edges, immutable = False)
70317031
answer.set_pos(self.get_pos())
70327032
answer.name("TSP from "+self.name())
7033+
if self.is_immutable():
7034+
answer = answer.copy(immutable = True)
70337035
return answer
70347036
else:
70357037
if self.has_multiple_edges() and len(self.edge_label(u,v)) > 1:
70367038
edges = self.edges()
70377039
edges.sort(key=weight)
7038-
answer = self.subgraph(edges = edges[:2])
7040+
answer = self.subgraph(edges = edges[:2], immutable = False)
70397041
answer.set_pos(self.get_pos())
70407042
answer.name("TSP from "+self.name())
7043+
if self.is_immutable():
7044+
answer = answer.copy(immutable = True)
70417045
return answer
70427046

70437047
raise EmptySetError("The given graph is not Hamiltonian")
@@ -7216,9 +7220,11 @@ def traveling_salesman_problem(self, use_edge_labels = False, solver = None, con
72167220
raise EmptySetError("The given graph is not Hamiltonian")
72177221

72187222
# We can now return the TSP !
7219-
answer = self.subgraph(edges = h.edges())
7223+
answer = self.subgraph(edges = h.edges(), immutable = False)
72207224
answer.set_pos(self.get_pos())
72217225
answer.name("TSP from "+g.name())
7226+
if self.is_immutable():
7227+
answer = answer.copy(immutable = True)
72227228
return answer
72237229

72247230
#################################################
@@ -12549,7 +12555,7 @@ def is_clique(self, vertices=None, directed_clique=False):
1254912555
False
1255012556
"""
1255112557
if directed_clique and self._directed:
12552-
subgraph=self.subgraph(vertices)
12558+
subgraph=self.subgraph(vertices, immutable = False)
1255312559
subgraph.allow_loops(False)
1255412560
subgraph.allow_multiple_edges(False)
1255512561
n=subgraph.order()

0 commit comments

Comments
 (0)