Skip to content

Commit

Permalink
gh-35512: Improve PolynomialSequence.connection_graph() implementation
Browse files Browse the repository at this point in the history
    
Simplify the code; also, the previous code has to iterate over variables
of the sequence twice (this is really bad before #35510).

This affects mainly the method `connected_components()`.

EDIT:

Moreover, now we add a clique between the variables of each polynomial,
so it agrees with the description (the code before used to add just a
spanning tree of the clique -- a star).

This makes this method a little bit slower for the purposes of
`connected_components()` (for which adding a star is equivalent).

However, #35518 will rewrite `connected_components()` without using
`connection_graph()` so this is not really a problem.


### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] New tests added to check that a clique is added for the variables
in each polynomial.

### Dependencies
 - #35511
    
URL: #35512
Reported by: Gonzalo Tornaría
Reviewer(s): David Coudert, Gonzalo Tornaría, Vincent Delecroix
  • Loading branch information
Release Manager committed Apr 21, 2023
2 parents db2fa5d + 850c883 commit 64c205c
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/sage/rings/polynomial/multi_polynomial_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,18 +905,39 @@ def connection_graph(self):
sage: B.<x,y,z> = BooleanPolynomialRing()
sage: F = Sequence([x*y + y + 1, z + 1])
sage: F.connection_graph()
sage: G = F.connection_graph(); G
Graph on 3 vertices
sage: G.is_connected()
False
sage: F = Sequence([x])
sage: F.connection_graph()
Graph on 1 vertex
TESTS::
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
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))
for f in self:
v = f.variables()
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

0 comments on commit 64c205c

Please sign in to comment.