Skip to content
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

Improve PolynomialSequence.connection_graph() implementation #35512

Merged
merged 3 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18371,6 +18371,9 @@ def add_clique(self, vertices, loops=False):
True
sage: G.vertices(sort=True)
[0, 1, 2, 3]
sage: G.add_clique({4})
sage: G.vertices(sort=True)
[0, 1, 2, 3, 4]
sage: D = DiGraph(4, loops=True)
sage: D.add_clique(range(4), loops=True)
sage: D.is_clique(directed_clique=True, loops=True)
Expand All @@ -18383,6 +18386,7 @@ def add_clique(self, vertices, loops=False):
else:
self.add_edges(itertools.combinations_with_replacement(vertices, 2))
else:
self.add_vertices(vertices)
if self.is_directed():
self.add_edges(itertools.permutations(vertices, 2))
else:
Expand Down
13 changes: 7 additions & 6 deletions src/sage/rings/polynomial/multi_polynomial_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,16 +907,17 @@ def connection_graph(self):
sage: F = Sequence([x*y + y + 1, z + 1])
sage: F.connection_graph()
Graph on 3 vertices
sage: F = Sequence([x*y*z])
sage: F.connection_graph().is_clique()
True
sage: F = Sequence([x*y, y*z])
sage: F.connection_graph().is_clique()
False
"""
V = sorted(self.variables())
from sage.graphs.graph import Graph
g = Graph()
g.add_vertices(sorted(V))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is a good idea to remove this line. The point being that you want vertices to represent the variables of the underlying polynomial ring. With this removal it is not the case

sage: B.<x,y,z> = BooleanPolynomialRing()
sage: F = Sequence([B.one()])
sage: F.connection_graph()   # must be a graph on three vertices and no edges

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's not what this line does, since V = self.variables(). Current behaviour:

$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.8, Release Date: 2023-02-11                     │
│ Using Python 3.11.3. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: B.<x,y,z> = BooleanPolynomialRing()
sage: Sequence([x]).connection_graph()
Graph on 1 vertex

In fact, your example is currently broken:

sage: Sequence([B.one()]).connection_graph()
...
IndexError: tuple index out of range

With my patch:

sage: Sequence([B.one()]).connection_graph()
Graph on 0 vertices

I grant that these two examples should be added to the doctest. The "correct" behaviour depends on interpretation of "Return the graph which has the variables of this system as vertices...".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added all of these tests to document behaviour:

            sage: F = Sequence([], B)
            sage: F.connection_graph()
            Graph on 0 vertices
            sage: F = Sequence([1], B)
            sage: F.connection_graph()
            Graph on 0 vertices
            sage: F = Sequence([x])
            sage: F.connection_graph()
            Graph on 1 vertex
            sage: F = Sequence([x, y])
            sage: F.connection_graph()
            Graph on 2 vertices

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@videlec if you are satisfied, please don't forget to approve / positive_review. Thanks!

for f in self:
v = f.variables()
tornaria marked this conversation as resolved.
Show resolved Hide resolved
a,tail = v[0],v[1:]
for b in tail:
g.add_edge((a,b))
g.add_clique(f.variables())
return g

def connected_components(self):
Expand Down