Skip to content

Commit 10e5e9e

Browse files
committed
add parameter key to method edge_boundary
1 parent b423822 commit 10e5e9e

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12572,7 +12572,7 @@ def edges(self, vertices=None, labels=True, sort=None, key=None,
1257212572
return EdgesView(self, vertices=vertices, labels=labels, sort=sort, key=key,
1257312573
ignore_direction=ignore_direction, sort_vertices=sort_vertices)
1257412574

12575-
def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
12575+
def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False, key=None):
1257612576
r"""
1257712577
Return a list of edges ``(u,v,l)`` with ``u`` in ``vertices1``
1257812578
and ``v`` in ``vertices2``.
@@ -12590,6 +12590,10 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
1259012590

1259112591
- ``sort`` -- boolean (default: ``False``); whether to sort the result
1259212592

12593+
- ``key`` -- a function (default: ``None``); a function that takes an
12594+
edge as its one argument and returns a value that can be used for
12595+
comparisons in the sorting algorithm (we must have ``sort=True``)
12596+
1259312597
EXAMPLES::
1259412598

1259512599
sage: K = graphs.CompleteBipartiteGraph(9, 3)
@@ -12614,6 +12618,23 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
1261412618
sage: D.edge_boundary([0], labels=False, sort=True)
1261512619
[(0, 1), (0, 2)]
1261612620

12621+
Using the ``key`` argument to order multiple edges of incomparable
12622+
types::
12623+
12624+
sage: G = Graph([(1, 'A', 4), (1, 2, 3)])
12625+
sage: G.edge_boundary([1], sort=True)
12626+
Traceback (most recent call last):
12627+
...
12628+
TypeError: unsupported operand parent(s) for <: 'Integer Ring' and '<class 'str'>'
12629+
sage: G.edge_boundary([1], sort=True, key=str)
12630+
[('A', 1, 4), (1, 2, 3)]
12631+
sage: G.edge_boundary([1], sort=True, key=lambda e:e[2])
12632+
[(1, 2, 3), ('A', 1, 4)]
12633+
sage: G.edge_boundary([1], labels=False, sort=True, key=lambda e:e[2])
12634+
Traceback (most recent call last):
12635+
...
12636+
IndexError: tuple index out of range
12637+
1261712638
TESTS::
1261812639

1261912640
sage: G = graphs.DiamondGraph()
@@ -12623,7 +12644,14 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
1262312644
[]
1262412645
sage: G.edge_boundary([2], [0])
1262512646
[(0, 2, None)]
12647+
sage: G.edge_boundary([2], [0], sort=False, key=str)
12648+
Traceback (most recent call last):
12649+
...
12650+
ValueError: sort keyword is False, yet a key function is given
1262612651
"""
12652+
if (not sort) and key:
12653+
raise ValueError('sort keyword is False, yet a key function is given')
12654+
1262712655
vertices1 = set(v for v in vertices1 if v in self)
1262812656
if self._directed:
1262912657
if vertices2 is not None:
@@ -12643,7 +12671,7 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
1264312671
output = [e for e in self.edges(vertices=vertices1, labels=labels, sort=False)
1264412672
if e[1] not in vertices1 or e[0] not in vertices1]
1264512673
if sort:
12646-
output.sort()
12674+
return sorted(output, key=key)
1264712675
return output
1264812676

1264912677
def edge_iterator(self, vertices=None, labels=True, ignore_direction=False, sort_vertices=True):

0 commit comments

Comments
 (0)