Skip to content

Commit

Permalink
sagemathgh-36073: Stop sorting Graph vertices and edges by default
Browse files Browse the repository at this point in the history
    
Following sagemath#22349 and sagemath#27408, we finally set parameter `sort` to `False`
by default for methods `vertices` and `edges`, and remove the
corresponding deprecation warnings.

### 📝 Checklist

- [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.
- [x] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#36073
Reported by: David Coudert
Reviewer(s): Matthias Köppe
  • Loading branch information
Release Manager committed Aug 12, 2023
2 parents 1e6cd50 + c8670bd commit 32d375d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/sage/geometry/polyhedron/base4.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def vertex_facet_graph(self, labels=True):
sage: P = polytopes.cube()
sage: G = P.vertex_facet_graph(); G
Digraph on 14 vertices
sage: G.vertices(key = lambda v: str(v))
sage: G.vertices(sort=True, key=lambda v: str(v))
[A vertex at (-1, -1, -1),
A vertex at (-1, -1, 1),
A vertex at (-1, 1, -1),
Expand Down
3 changes: 2 additions & 1 deletion src/sage/graphs/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3679,7 +3679,8 @@ def flow_polytope(self, edges=None, ends=None, backend=None):
Using a different order for the edges of the graph::
sage: fl = G.flow_polytope(edges=G.edges(key=lambda x: x[0] - x[1])); fl # needs sage.geometry.polyhedron
sage: ordered_edges = G.edges(sort=True, key=lambda x: x[0] - x[1])
sage: fl = G.flow_polytope(edges=ordered_edges); fl # needs sage.geometry.polyhedron
A 1-dimensional polyhedron in QQ^4 defined as the convex hull of 2 vertices
sage: fl.vertices() # needs sage.geometry.polyhedron
(A vertex at (0, 1, 1, 0), A vertex at (1, 0, 0, 1))
Expand Down
54 changes: 10 additions & 44 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@
from sage.misc.decorators import options
from sage.misc.cachefunc import cached_method
from sage.misc.prandom import random
from sage.misc.superseded import deprecation
from sage.misc.lazy_import import lazy_import, LazyImport

from sage.rings.integer_ring import ZZ
Expand Down Expand Up @@ -11364,19 +11363,15 @@ def neighbor_iterator(self, vertex, closed=False):
for u in self._backend.iterator_nbrs(vertex):
yield u

def vertices(self, sort=None, key=None, degree=None, vertex_property=None):
def vertices(self, sort=False, key=None, degree=None, vertex_property=None):
r"""
Return a list of the vertices.

INPUT:

- ``sort`` -- boolean (default: ``None``); if ``True``, vertices are
sorted according to the default ordering

As of :trac:`22349`, this argument must be explicitly
specified (unless a ``key`` is given); otherwise a warning
is printed and ``sort=True`` is used. The default will
eventually be changed to ``False``.
- ``sort`` -- boolean (default: ``False``); whether to sort vertices
according the ordering specified with parameter ``key``. If ``False``
(default), vertices are not sorted.

- ``key`` -- a function (default: ``None``); a function that takes a
vertex as its one argument and returns a value that can be used for
Expand Down Expand Up @@ -11464,20 +11459,7 @@ def vertices(self, sort=None, key=None, degree=None, vertex_property=None):
Traceback (most recent call last):
...
ValueError: sort keyword is False, yet a key function is given

Deprecation warning for ``sort=None`` (:trac:`22349`)::

sage: G = graphs.HouseGraph()
sage: G.vertices()
doctest:...: DeprecationWarning: parameter 'sort' will be set to False by default in the future
See https://github.com/sagemath/sage/issues/22349 for details.
[0, 1, 2, 3, 4]
"""
if sort is None:
if key is None:
deprecation(22349, "parameter 'sort' will be set to False by default in the future")
sort = True

if (not sort) and key:
raise ValueError('sort keyword is False, yet a key function is given')

Expand Down Expand Up @@ -12403,7 +12385,7 @@ def has_edge(self, u, v=None, label=None):
label = None
return self._backend.has_edge(u, v, label)

def edges(self, vertices=None, labels=True, sort=None, key=None,
def edges(self, vertices=None, labels=True, sort=False, key=None,
ignore_direction=False, sort_vertices=True):
r"""
Return a :class:`~EdgesView` of edges.
Expand All @@ -12427,13 +12409,10 @@ def edges(self, vertices=None, labels=True, sort=None, key=None,
- ``labels`` -- boolean (default: ``True``); if ``False``, each edge is
simply a pair ``(u, v)`` of vertices

- ``sort`` -- boolean (default: ``None``); if ``True``, edges are sorted
according to the default ordering

As of :trac:`22349`, this argument must be explicitly
specified (unless a ``key`` is given); otherwise a warning
is printed and ``sort=True`` is used. The default will
eventually be changed to ``False``.
- ``sort`` -- boolean (default: ``False``); whether to sort edges
according the ordering specified with parameter ``key``. If ``False``
(default), edges are not sorted. This is the fastest and less memory
consuming method for iterating over edges.

- ``key`` -- a function (default: ``None``); a function that takes an
edge (a pair or a triple, according to the ``labels`` keyword) as its
Expand Down Expand Up @@ -12529,7 +12508,7 @@ def edges(self, vertices=None, labels=True, sort=None, key=None,
....: G.set_edge_label(e[0], e[1], chr(ord('A') + e[0] + 5 * e[1]))
sage: G.edges(sort=True)
[(0, 1, 'F'), (0, 4, 'U'), (1, 2, 'L'), (2, 3, 'R'), (3, 4, 'X')]
sage: G.edges(key=lambda x: x[2])
sage: G.edges(sort=True, key=lambda x: x[2])
[(0, 1, 'F'), (1, 2, 'L'), (2, 3, 'R'), (0, 4, 'U'), (3, 4, 'X')]

We can restrict considered edges to those incident to a given set::
Expand Down Expand Up @@ -12580,20 +12559,7 @@ def edges(self, vertices=None, labels=True, sort=None, key=None,
sage: G.edge_label(0, 1)[0] += 1
sage: G.edges(sort=True)
[(0, 1, [8]), (0, 2, [7])]

Deprecation warning for ``sort=None`` (:trac:`27408`)::

sage: G = graphs.HouseGraph()
sage: G.edges(sort=None)
doctest:...: DeprecationWarning: parameter 'sort' will be set to False by default in the future
See https://github.com/sagemath/sage/issues/27408 for details.
[(0, 1, None), (0, 2, None), (1, 3, None), (2, 3, None), (2, 4, None), (3, 4, None)]
"""
if sort is None:
if key is None:
deprecation(27408, "parameter 'sort' will be set to False by default in the future")
sort = True

if vertices is not None and vertices in self:
vertices = [vertices]

Expand Down
18 changes: 5 additions & 13 deletions src/sage/graphs/views.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,10 @@ cdef class EdgesView:
- ``ignore_direction`` -- boolean (default: ``False``); only applies to
directed graphs. If ``True``, searches across edges in either direction.
- ``sort`` -- boolean (default: ``None``); whether to sort edges
- if ``None``, sort edges according to the default ordering and give a
deprecation warning as sorting will be set to ``False`` by default in
the future
- if ``True``, edges are sorted according the ordering specified with
parameter ``key``
- if ``False``, edges are not sorted. This is the fastest and less memory
consuming method for iterating over edges. This will become the default
behavior in the future.
- ``sort`` -- boolean (default: ``False``); whether to sort edges according
the ordering specified with parameter ``key``. If ``False`` (default),
edges are not sorted. This is the fastest and less memory consuming method
for iterating over edges.
- ``key`` -- a function (default: ``None``); a function that takes an edge
(a pair or a triple, according to the ``labels`` keyword) as its one
Expand Down Expand Up @@ -350,7 +342,7 @@ cdef class EdgesView:

def __init__(self, G, vertices=None, vertices2=None, labels=True,
ignore_direction=False,
sort=None, key=None, sort_vertices=True):
sort=False, key=None, sort_vertices=True):
"""
Construction of this :class:`EdgesView`.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/manifolds/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ def subset_digraph(self, loops=False, quotient=False, open_covers=False, points=
sage: U = M.open_subset('U'); V = M.open_subset('V'); W = M.open_subset('W')
sage: D = M.subset_digraph(); D
Digraph on 4 vertices
sage: D.edges(key=lambda e: (e[0]._name, e[1]._name))
sage: D.edges(sort=True, key=lambda e: (e[0]._name, e[1]._name))
[(Set {U} of open subsets of the 3-dimensional differentiable manifold M,
Set {M} of open subsets of the 3-dimensional differentiable manifold M,
None),
Expand Down

0 comments on commit 32d375d

Please sign in to comment.