Skip to content

graph/path: implement standard improvements to Yen's KSP algorithm#2005

Open
BarrensZeppelin wants to merge 3 commits into
gonum:masterfrom
BarrensZeppelin:graph/yenksp-opt
Open

graph/path: implement standard improvements to Yen's KSP algorithm#2005
BarrensZeppelin wants to merge 3 commits into
gonum:masterfrom
BarrensZeppelin:graph/yenksp-opt

Conversation

@BarrensZeppelin

@BarrensZeppelin BarrensZeppelin commented Nov 28, 2024

Copy link
Copy Markdown
Contributor

I implemented two common optimizations for Yen's algorithm that can reduce the number of calls to the shortest path subroutine: https://en.wikipedia.org/wiki/Yen%27s_algorithm#Improvements

Split from #2004

@kortschak kortschak changed the title graph/path: Implement standard improvements to Yen's KSP algorithm graph/path: implement standard improvements to Yen's KSP algorithm Nov 29, 2024

@kortschak kortschak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please revise this to be the two optimisations in separate commits with no other changes. We can do the other changes later if they merit it.

Note that the format of commit messages starts the sentence fragment with a lower case.

{1, 4, 5, 2, 3, 6},
},
},
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is this new test for? It passes without the implementation change.

@BarrensZeppelin BarrensZeppelin Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It catches an incorrect implementation of the early termination optimisation.
When I read the description of the optimisation, I thought that it was maybe possible to terminate once there is enough paths in the list of potential shortest paths.
I tried to check this idea with the existing tests, and found that there was no case where a new potential path is shorter than all the remaining potential paths computed in previous iterations.
However, this case can actually occur in practice, as demonstrated by the test case.

Comment thread graph/path/yen_ksp.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd like the changes that are not required for the optimisations to be backed out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread graph/path/yen_ksp.go Outdated
if len(best.path) <= 1 || best.weight > cost {
break
}
if neededPaths := k - i; k >= 0 && len(pot) >= neededPaths && best.weight == pot[neededPaths-1].weight {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the second optimisation? These should be in separate commits.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Done.

This optimisation skips computation of some shortest paths that are
definitely redundant (already computed earlier).
See https://en.wikipedia.org/wiki/Yen%27s_algorithm#Improvements for
details.
Potential paths computed in future iterations can never be shorter than
the shortest path computed in the current iteration, so if we have
already computed `k-i` potential paths with the same minimal weight, we
can stop early.
See also https://en.wikipedia.org/wiki/Yen%27s_algorithm#Improvements
This test shows that a potential path computed in a future iteration can
be shorter than the 2nd shortest computed in the current iteration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants