perf(yen): skip the deviations an ancestor has already produced - #812
Merged
Conversation
Every accepted route is spurred at each of its nodes in turn, but a route coincides with the one it was derived from up to the point where it deviates. For a spur index before that point the root path, and the set of edges banned at it, are the same as they were for the parent, so the search runs again and produces a candidate that was already generated and rejected. Record where each route deviated and start from there, which is Lawler's refinement of Yen's algorithm. The candidate's cost also no longer needs `make_cost`. Filtering removes whole node pairs rather than individual parallel edges, so the cheapest edge between two surviving nodes is the same in the filtered graph as in the full one, and the cost of the root path plus the cost the search reports for the spur is exactly the cost of the concatenation. The prefix costs of the route being explored are computed once per route rather than once per candidate. Checked against a brute-force enumeration of every loopless path over 150 random graphs, comparing the k cheapest costs. About 39% off twenty shortest paths across a 60x60 weighted grid.
samueltardieu
force-pushed
the
perf/yen-lawler
branch
from
September 9, 2026 07:37
6ea5ea4 to
769d983
Compare
samueltardieu
enabled auto-merge
September 9, 2026 07:38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #808.
Records where each route deviated from its parent and starts spurring from there — Lawler's refinement of Yen's algorithm.
make_costgoes away with it. Filtering removes whole node pairs rather than individual parallel edges, so the cheapest edge between two surviving nodes is the same in the filtered graph as in the full one, which means the cost of the root path plus the cost the search reports for the spur is exactly the cost of the concatenation. Prefix costs are computed once per route rather than once per candidate.Checking
Verified against a brute-force enumeration of every loopless path over 150 random graphs, comparing the k cheapest costs, so a skipped deviation that should have produced a candidate would show up as a missing or more expensive path.
About 39% off twenty shortest paths across a 60x60 weighted grid, measured against this branch's parent.
Built on #798, which is included here as the first commit because this replaces the cost calculation that fix corrects. Review that one first; once it lands I will rebase this so only the second commit remains.
All timings measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.