From e8362fd451d8ffac6c8156d6fc6b061f972e6681 Mon Sep 17 00:00:00 2001 From: kappybar Date: Thu, 12 Jun 2025 21:40:22 +0900 Subject: [PATCH 1/2] report weight if s=t in yen_k_shortest_simple_paths --- src/sage/graphs/path_enumeration.pyx | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/sage/graphs/path_enumeration.pyx b/src/sage/graphs/path_enumeration.pyx index f77d1fd239a..b97dc2c13ca 100644 --- a/src/sage/graphs/path_enumeration.pyx +++ b/src/sage/graphs/path_enumeration.pyx @@ -734,6 +734,12 @@ def yen_k_shortest_simple_paths(self, source, target, weight_function=None, [1, 6, 9, 10, 5], [1, 6, 9, 3, 4, 5], [1, 6, 9, 11, 10, 5]] + + When s = t and report_edge=True and report_weight=True (issue 40247):: + + sage: g = DiGraph([(1, 2)]) + sage: list(yen_k_shortest_simple_paths(g, 1, 1, report_edges=True, report_weight=True)) + [(0, [])] """ if source not in self: raise ValueError("vertex '{}' is not in the graph".format(source)) @@ -741,12 +747,16 @@ def yen_k_shortest_simple_paths(self, source, target, weight_function=None, raise ValueError("vertex '{}' is not in the graph".format(target)) if source == target: - if report_edges: - yield [] - elif report_weight: - yield (0, [source]) + if report_weight: + if report_edges: + yield (0, []) + else: + yield (0, [source]) else: - yield [source] + if report_edges: + yield [] + else: + yield [source] return if self.has_loops() or self.allows_multiple_edges(): From f2ef3533ea943e4e543eb2502c6d83b3e0bf7d8b Mon Sep 17 00:00:00 2001 From: kappybar Date: Thu, 12 Jun 2025 22:48:28 +0900 Subject: [PATCH 2/2] change to shorter code --- src/sage/graphs/path_enumeration.pyx | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/sage/graphs/path_enumeration.pyx b/src/sage/graphs/path_enumeration.pyx index b97dc2c13ca..28a42b185e9 100644 --- a/src/sage/graphs/path_enumeration.pyx +++ b/src/sage/graphs/path_enumeration.pyx @@ -735,7 +735,7 @@ def yen_k_shortest_simple_paths(self, source, target, weight_function=None, [1, 6, 9, 3, 4, 5], [1, 6, 9, 11, 10, 5]] - When s = t and report_edge=True and report_weight=True (issue 40247):: + When ``s == t`` and ``report_edge == True`` and ``report_weight == True`` (:issue:`40247`):: sage: g = DiGraph([(1, 2)]) sage: list(yen_k_shortest_simple_paths(g, 1, 1, report_edges=True, report_weight=True)) @@ -747,16 +747,8 @@ def yen_k_shortest_simple_paths(self, source, target, weight_function=None, raise ValueError("vertex '{}' is not in the graph".format(target)) if source == target: - if report_weight: - if report_edges: - yield (0, []) - else: - yield (0, [source]) - else: - if report_edges: - yield [] - else: - yield [source] + P = [] if report_edges else [source] + yield (0, P) if report_weight else P return if self.has_loops() or self.allows_multiple_edges():