Repository navigation
Adds PathValidator functionality to YenShortestPathsIterator - #891
Conversation
ee417ce to
69e2385
Compare
Toptachamann
left a comment
There was a problem hiding this comment.
The first review of this PR. There're 2 necessary components to complete this PR:
- Benchmark the code and find out if there's a performance change.
- Do a big stress test with some other implementation to ensure correctness. If it's going to be done using
KSimpleShortestPaths, then I guess we're blocked by #892
| * Constructs an instance of the algorithm for given {@code graph}, {@code source}, {@code sink} | ||
| * and {@code heapSupplier}. | ||
| * Constructs an instance of the algorithm for given {@code graph}, {@code source}, | ||
| * {@code sink} and {@code pathValidator}. The {@code pathValidator} can be $null$, |
There was a problem hiding this comment.
The formatting for null word should be made consistent, i.e. if you use
| * | ||
| * <p> | ||
| * Additionally, the algorithm supports path validation by means of {@link PathValidator}. | ||
| * To do so, there is a boolean flag for every candidate in the queue, which indicates, if |
There was a problem hiding this comment.
The information about the boolean flag seems to be a particular implementation detail, which should not be included in the public Javadoc. You can move this information to the place where the corresponding variables are declared.
| */ | ||
| public YenShortestPathIterator( | ||
| Graph<V, E> graph, V source, V sink, | ||
| Supplier<AddressableHeap<Double, GraphPath<V, E>>> heapSupplier) |
There was a problem hiding this comment.
This is a breaking change in the sense that the constructor YenShortestPathIterator(graph, source, sink, heapSupplier) gets removed. You should keep this constructor
| candidatePaths.insert(shortestPath.getWeight(), shortestPath); | ||
| deviations.put(shortestPath, source); | ||
| weightsFrequencies.put(shortestPath.getWeight(), 1); | ||
| V lastValidDeviation = getLastValidDeviation(shortestPath, source); |
There was a problem hiding this comment.
This if statement is a heavy computation and should be moved to the hasNext or next methods. The reason is that the user doesn't expect the constructor to do potentially all the computation.
374e2c6 to
a1b7c3b
Compare
|
Updates summary
Benchmarks were performed on the Results before changes of the PR: Results after changes of this PR: The 4th and 6th bars on the last plot are to high to be shown. |
|
After the bug fix for Since there is such performance gap, I think it is either worth mentioning in the documentation or deprecating the |
| * If the overall number of paths between {@code source} and {@code target} is denoted by $n$ | ||
| * and the value of {@code #NUMBER_OF_PATH_TO_ITERATE} is denoted by $m$ then the method | ||
| * iterates over $p = min\{n, m\}$ such paths and verifies that they are built correctly. The | ||
| * method uses the {@link KShortestSimplePaths} implementation to verify the order of paths |
There was a problem hiding this comment.
Remove KShortestSimplePaths
| } | ||
|
|
||
| @Benchmark | ||
| public List<List<GraphPath<Integer, DefaultWeightedEdge>>> testKShortestSimplePaths(RandomGraphState state) { |
There was a problem hiding this comment.
Potentially, we will need to remove this method
|
Please, remove the code related to the |
Toptachamann
left a comment
There was a problem hiding this comment.
LGTM. I've left some more comments regarding Javadoc
| private final Graph<V, E> graph; | ||
|
|
||
| /** | ||
| * Provides validation for the paths which will be computed. Is the validator |
There was a problem hiding this comment.
'Is the validator' -> 'If the validator'
| /** | ||
| * Heap of the candidate path generated so far and sorted my their weights. | ||
| * There is a boolean flag for every candidate in the queue, which indicates, if | ||
| * the path is valid ot not. Invalid paths are kept in the queue, because it is possible |
There was a problem hiding this comment.
Let's mention that an invalid path is a path that contains an edge which fails the PathValidator check.
| private AddressableHeap<Double, Pair<GraphPath<V, E>, Boolean>> candidatePaths; | ||
|
|
||
| /** | ||
| * Keeps track of the vertex at which each path deviates from its "parent" path. |
There was a problem hiding this comment.
For each path $P$, stores its deviation point.
A path deviation point is a first node in the path that doesn't belong to the parent path. If the path doesn't have a
parent (which is only possible for one shortest path between the {@code source} and the {@code sink}), this map
stores its first node.
| * many candidates with minimum weight exist. If the amount is greater or equal to the amount of | ||
| * path needed to complete the execution, the algorithm retrieves the rest of the path from the | ||
| * candidates heap and adds them to the resulting list. | ||
| * {@code sink} and forms the resulting list. It is possible to provide validation for paths, which |
There was a problem hiding this comment.
'It is possible to provide validation for paths ...' -> 'It is possible to provide a {@link PathValidator} to filter the resulting path list'
| @Override | ||
| public GraphPath<V, E> next() | ||
| { | ||
| lazyComputeShortestPath(); |
There was a problem hiding this comment.
You can simplify the code by removing this line and substituting the check in the next line with !hasNext()
| * If the shortest path is not valid, proceeds computing deviations | ||
| * until first valid path is computed. | ||
| */ | ||
| private void lazyComputeShortestPath() { |
There was a problem hiding this comment.
The current name of this method doesn't reflect the fact that it basically performs a lazy initialization of the path heap. I would suggest to rename it to lazyInitializePathHeap.
| } | ||
|
|
||
| /** | ||
| * Computes shortest path between {@code source} and {@code sink}. |
There was a problem hiding this comment.
|
|
||
| /** | ||
| * Computes all simple shortest paths between {@code source} and {@code target} | ||
| * without {@code pathValidator}. The computes all shortest paths between {@code source} |
| /** | ||
| * Provides possibility to validate computed paths and exclude invalid ones. | ||
| */ | ||
| private PathValidator<V, E> pathValidator; |
There was a problem hiding this comment.
I would recommend adding some explanation about the way we enable path validation (path validity checks, first & last deviations, etc.). The original paper doesn't contain this information and it can't be deduced just by reading the method Javadoc comments.
Toptachamann
left a comment
There was a problem hiding this comment.
Except for the typos, this PR is ready to get merged
|
|
||
| /** | ||
| * Provides possibility to validate computed paths and exclude invalid ones. | ||
| * Whenever a candidate path $P$ first deviation vertex $u$ is produces by this |
There was a problem hiding this comment.
'is produces by this algorithm " -> "is produced by this algorithm, "
| * the path is valid ot not. Invalid paths are kept in the queue, because it is possible | ||
| * to build a valid path by deviating from an invalid one. | ||
| * the path is valid ot not. An invalid path is a path which contains an edge which | ||
| * fails the {@code pathValidator} check. Invalid paths are kept in the queue, because |
There was a problem hiding this comment.
" Invalid" -> " Invalid" (2 spaces)
|
Done. |
|
Great job, thanks! |


This pull request resolves #827.
HISTORY.mdorCONTRIBUTORS.md