@@ -12572,7 +12572,7 @@ def edges(self, vertices=None, labels=True, sort=None, key=None,
12572
12572
return EdgesView(self, vertices=vertices, labels=labels, sort=sort, key=key,
12573
12573
ignore_direction=ignore_direction, sort_vertices=sort_vertices)
12574
12574
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 ):
12576
12576
r"""
12577
12577
Return a list of edges ``(u,v,l)`` with ``u`` in ``vertices1``
12578
12578
and ``v`` in ``vertices2``.
@@ -12590,6 +12590,10 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
12590
12590
12591
12591
- ``sort`` -- boolean (default: ``False``); whether to sort the result
12592
12592
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
+
12593
12597
EXAMPLES::
12594
12598
12595
12599
sage: K = graphs.CompleteBipartiteGraph(9, 3)
@@ -12614,6 +12618,23 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
12614
12618
sage: D.edge_boundary([0], labels=False, sort=True)
12615
12619
[(0, 1), (0, 2)]
12616
12620
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
+
12617
12638
TESTS::
12618
12639
12619
12640
sage: G = graphs.DiamondGraph()
@@ -12623,7 +12644,14 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
12623
12644
[]
12624
12645
sage: G.edge_boundary([2], [0])
12625
12646
[(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
12626
12651
"""
12652
+ if (not sort) and key:
12653
+ raise ValueError('sort keyword is False, yet a key function is given')
12654
+
12627
12655
vertices1 = set(v for v in vertices1 if v in self)
12628
12656
if self._directed:
12629
12657
if vertices2 is not None:
@@ -12643,7 +12671,7 @@ def edge_boundary(self, vertices1, vertices2=None, labels=True, sort=False):
12643
12671
output = [e for e in self.edges(vertices=vertices1, labels=labels, sort=False)
12644
12672
if e[1] not in vertices1 or e[0] not in vertices1]
12645
12673
if sort:
12646
- output.sort( )
12674
+ return sorted(output, key=key )
12647
12675
return output
12648
12676
12649
12677
def edge_iterator(self, vertices=None, labels=True, ignore_direction=False, sort_vertices=True):
0 commit comments