Skip to content

Fix reporting weight in yen_k_shortest_simple_paths #40248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/sage/graphs/path_enumeration.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -734,19 +734,21 @@ 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))
if target not in self:
raise ValueError("vertex '{}' is not in the graph".format(target))

if source == target:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A shorter version could be

if source == target:
    P = [] if report_edges else [source]
    yield (0, P) if report_weight else P

if report_edges:
yield []
elif report_weight:
yield (0, [source])
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():
Expand Down
Loading