Skip to content

fix issue with immutable graphs in src/sage/graphs/chrompoly.pyx #39896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/sage/graphs/chrompoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
Traceback (most recent call last):
...
ValueError: algorithm must be "C" or "Python"

Check the behavior with immutable graphs::

sage: G = Graph([(0, 1), (0, 1)], multiedges=True, immutable=True)
sage: G.chromatic_polynomial()
x^2 - x
"""
algorithm = algorithm.lower()
if algorithm not in ['c', 'python']:
Expand Down Expand Up @@ -168,9 +174,8 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
cdef mpz_t m, coeff
cdef mpz_t *tot
cdef mpz_t *coeffs
G = G.relabel(inplace=False)
G = G.relabel(inplace=False, immutable=False)
G.remove_multiple_edges()
G.remove_loops()
nverts = G.num_verts()
nedges = G.num_edges()

Expand Down Expand Up @@ -436,6 +441,12 @@ def chromatic_polynomial_with_cache(G, cache=None):
Traceback (most recent call last):
...
TypeError: parameter cache must be a dictionary or None

Check the behavior with immutable graphs::

sage: G = Graph([(0, 1), (0, 1)], multiedges=True, immutable=True)
sage: chromatic_polynomial_with_cache(G)
x^2 - x
"""
cdef CommutativeRing R = PolynomialRing(ZZ, "x", implementation="FLINT")
cdef Polynomial_integer_dense_flint one = R.one()
Expand All @@ -448,7 +459,7 @@ def chromatic_polynomial_with_cache(G, cache=None):
return zero

# Make a copy of the input graph and ensure that it's labeled [0..n-1]
G = G.relabel(inplace=False)
G = G.relabel(inplace=False, immutable=False)
G.remove_multiple_edges()

# We use a cache to avoid computing twice the chromatic polynomial of
Expand Down
Loading