Skip to content

Commit b423822

Browse files
committed
add parameter key to method multiple_edges
1 parent 853d070 commit b423822

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,7 @@ def allow_multiple_edges(self, new, check=True, keep_label='any'):
35153515

35163516
self._backend.multiple_edges(new)
35173517

3518-
def multiple_edges(self, to_undirected=False, labels=True, sort=False):
3518+
def multiple_edges(self, to_undirected=False, labels=True, sort=False, key=None):
35193519
"""
35203520
Return any multiple edges in the (di)graph.
35213521

@@ -3525,7 +3525,11 @@ def multiple_edges(self, to_undirected=False, labels=True, sort=False):
35253525

35263526
- ``labels`` -- boolean (default: ``True``); whether to include labels
35273527

3528-
- ``sort`` - boolean (default: ``False``); whether to sort the result
3528+
- ``sort`` -- boolean (default: ``False``); whether to sort the result
3529+
3530+
- ``key`` -- a function (default: ``None``); a function that takes an
3531+
edge as its one argument and returns a value that can be used for
3532+
comparisons in the sorting algorithm (we must have ``sort=True``)
35293533

35303534
EXAMPLES::
35313535

@@ -3574,7 +3578,36 @@ def multiple_edges(self, to_undirected=False, labels=True, sort=False):
35743578
[]
35753579
sage: G.multiple_edges(to_undirected=True, sort=True)
35763580
[(1, 2, 'h'), (2, 1, 'g')]
3581+
3582+
Using the ``key`` argument to order multiple edges of incomparable
3583+
types::
3584+
3585+
sage: G = Graph([('A', 'B', 3), (1, 2, 1), ('A', 'B', 4), (1, 2, 2)], multiedges=True)
3586+
sage: G.multiple_edges(sort=True)
3587+
Traceback (most recent call last):
3588+
...
3589+
TypeError: unsupported operand parent(s) for <: 'Integer Ring' and '<class 'str'>'
3590+
sage: G.multiple_edges(labels=False, sort=True, key=str)
3591+
[('A', 'B'), ('A', 'B'), (1, 2), (1, 2)]
3592+
sage: G.multiple_edges(sort=True, key=str)
3593+
[('A', 'B', 3), ('A', 'B', 4), (1, 2, 1), (1, 2, 2)]
3594+
sage: G.multiple_edges(labels=True, sort=True, key=lambda e:e[2])
3595+
[(1, 2, 1), (1, 2, 2), ('A', 'B', 3), ('A', 'B', 4)]
3596+
sage: G.multiple_edges(labels=False, sort=True, key=lambda e:e[2])
3597+
Traceback (most recent call last):
3598+
...
3599+
IndexError: tuple index out of range
3600+
3601+
TESTS::
3602+
3603+
sage: Graph().multiple_edges(sort=False, key=str)
3604+
Traceback (most recent call last):
3605+
...
3606+
ValueError: sort keyword is False, yet a key function is given
35773607
"""
3608+
if (not sort) and key:
3609+
raise ValueError('sort keyword is False, yet a key function is given')
3610+
35783611
multi_edges = []
35793612
seen = set()
35803613

@@ -3647,7 +3680,7 @@ def multiple_edges(self, to_undirected=False, labels=True, sort=False):
36473680
multi_edges.extend((u, v) for _ in L)
36483681

36493682
if sort:
3650-
multi_edges.sort()
3683+
return sorted(multi_edges, key=key)
36513684
return multi_edges
36523685

36533686
def name(self, new=None):

0 commit comments

Comments
 (0)