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

Conversation

tornaria
Copy link
Contributor

@tornaria tornaria commented Apr 15, 2023

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

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

Dependencies

Before the change:

    sage: g = Graph()
    sage: g.add_clique([1,2])
    sage: g.add_clique([3])
    sage: g.vertices(sort=True)
    [1, 2]

After the change:

    sage: g = Graph()
    sage: g.add_clique([1,2])
    sage: g.add_clique([3])
    sage: g.vertices(sort=True)
    [1, 2, 3]
@tornaria tornaria requested a review from malb April 15, 2023 03:27
Simplify the code; also, the previous code has to iterate over variables
of the sequence twice (this is really bad before sagemath#35510)

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, sagemath#35518 will rewrite `connected_components()` without using
`connection_graph()` so this is not really a problem.
@tornaria tornaria force-pushed the mps-connection_graph branch from abf1c80 to 8d8289b Compare April 15, 2023 21:28
@tornaria
Copy link
Contributor Author

I changed the PR to use add_clique() in the end, I did not add a use_clique option to the method, since #35518 makes it unnecessary.

A couple of tests show that a clique is added for each polynomial (but not for each connected component).

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!

@github-actions
Copy link

Documentation preview for this PR is ready! 🎉
Built with commit: 850c883

@vbraun vbraun merged commit 64c205c into sagemath:develop Apr 23, 2023
vbraun pushed a commit that referenced this pull request Apr 23, 2023
    
As discussed in #35512, we propose an implementation of method
`PolynomialSequence.connected_components()` that avoids the effective
construction of the graph connecting variables that appear in a same
polynomial.

### 📚 Description

We use a union-find data structure to encode the connected components.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x
]`. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

See discussion in #35512
    
URL: #35518
Reported by: David Coudert
Reviewer(s): David Coudert, Gonzalo Tornaría, Vincent Delecroix
@mkoeppe mkoeppe added this to the sage-10.0 milestone Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants