Skip to content

Adds PathValidator functionality to YenShortestPathsIterator - #891

Merged
Toptachamann merged 9 commits into
jgrapht:masterfrom
SChudakov:pathvalidator
Apr 21, 2020
Merged

Toptachamann merged 9 commits into
jgrapht:masterfrom
SChudakov:pathvalidator

Conversation

@SChudakov

Copy link
Copy Markdown
Collaborator

@Toptachamann Toptachamann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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$,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The formatting for null word should be made consistent, i.e. if you use $null$, you should use this style across the entire file. I would not recommend using $null$ since this is not a latex formula, so it doesn't make a lot of sense. It's better to use either "{@code null}" or "null".

*
* <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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@SChudakov
SChudakov force-pushed the pathvalidator branch 2 times, most recently from 374e2c6 to a1b7c3b Compare April 3, 2020 08:44
@SChudakov

Copy link
Copy Markdown
Collaborator Author

Updates summary

  1. Added KShortestPathsPerformace benchmark for the implementations of KShortestPathAlgorithm interface.
  2. Performed test for YenKShortestPaths algorithm without PathValidator using networkx library. Tested correctness of YenKShortestPaths with PathValidator by comparing output of algorithm with validation to the output without validation.
  3. Fixed bug in KShortestSimplePaths as suggested in KShortestSimplePaths bug #892.
  4. Performed performance comparison of the changes algorithms.

Benchmarks were performed on the Gnp random graphs with n = 20, and p = 0.1 or p = 0.2. For the path validation 10 random edges were selected from the graph and excluded from the shortest paths computation.

Results before changes of the PR:

before

Results after changes of this PR:

after

The 4th and 6th bars on the last plot are to high to be shown.
The actual values of those bars are 62784 ms and 113901 ms correspondingly.

@SChudakov

Copy link
Copy Markdown
Collaborator Author

After the bug fix for KShortestSimplePaths it performance has significantly worsened.
Comparing to the YenKShortestPaths on the random Gnp graph with n = 20 vertices and edge probability p = 0.2 the performance ratio is::

without path validation:        10641 times
with    path validation:        26488 times

Since there is such performance gap, I think it is either worth mentioning in the documentation or deprecating the KShortestSimplePaths for further removal in later releases.

* 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Remove KShortestSimplePaths

}

@Benchmark
public List<List<GraphPath<Integer, DefaultWeightedEdge>>> testKShortestSimplePaths(RandomGraphState state) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Potentially, we will need to remove this method

@Toptachamann

Copy link
Copy Markdown
Collaborator

Please, remove the code related to the KShortestSimplePath bug fix as it's unrelated to this PR

@Toptachamann Toptachamann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

'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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

'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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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}.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lazily initializes the path heap by computing the shortest path between the {@code source} and the {@code sink} and building a necessary amount of paths until at least one valid path is found.

Note: this computation is done only once before the first path is returned by this iterator.


/**
* Computes all simple shortest paths between {@code source} and {@code target}
* without {@code pathValidator}. The computes all shortest paths between {@code source}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

'The' -> 'Then'

/**
* Provides possibility to validate computed paths and exclude invalid ones.
*/
private PathValidator<V, E> pathValidator;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 Toptachamann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

'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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

" Invalid" -> " Invalid" (2 spaces)

@SChudakov

Copy link
Copy Markdown
Collaborator Author

Done.

@Toptachamann

Copy link
Copy Markdown
Collaborator

Great job, thanks!

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.

Add PathValidator support for YenKShortestPath algorithm

2 participants