Skip to content

Commit

Permalink
Limit the size of shortest paths discovered.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Apr 5, 2014
1 parent e2253d3 commit 222a959
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Expand Up @@ -94,7 +94,7 @@ public final class ShortestPaths {
private final boolean[] precedes;


private final int start;
private final int start, limit;
private final IAtomContainer container;

/**
Expand Down Expand Up @@ -139,13 +139,29 @@ public ShortestPaths(IAtomContainer container, IAtom start) {
* @param start the start atom index of the shortest paths
* @param ordering vertex ordering for preceding path (null = don't use)
*/
ShortestPaths(int[][] adjacent, IAtomContainer container, int start,
int[] ordering) {
ShortestPaths(int[][] adjacent, IAtomContainer container, int start, int[] ordering) {
this(adjacent, container, start, container.getAtomCount(), ordering);
}

/**
* Create a new shortest paths search for the given graph from the {@literal
* start} vertex. The ordering for use by {@link #isPrecedingPathTo(int)}
* can also be specified.
*
* @param adjacent adjacency list representation - built from {@link
* GraphUtil#toAdjList(IAtomContainer)}
* @param container container used to access atoms and their indices
* @param start the start atom index of the shortest paths
* @param limit the maximum length path to find
* @param ordering vertex ordering for preceding path (null = don't use)
*/
ShortestPaths(int[][] adjacent, IAtomContainer container, int start, int limit, int[] ordering) {

int n = adjacent.length;

this.container = container;
this.start = start;
this.limit = limit;

this.distTo = new int[n];
this.routeTo = new Route[n];
Expand Down Expand Up @@ -194,6 +210,9 @@ private void compute(final int[][] adjacent) {
int dist = distTo[v] + 1;
for (int w : adjacent[v]) {

if (dist > limit)
continue;

// distance is less then the current closest distance
if (dist < distTo[w]) {
distTo[w] = dist;
Expand Down
Expand Up @@ -1128,6 +1128,19 @@ public void testDistanceTo_Int_Benzene() {
assertThat(paths.distanceTo(5), is(1));

}

@Test public void testDistanceTo_Int_Benzene_limited() {
IAtomContainer benzene = MoleculeFactory.makeBenzene();

ShortestPaths paths = new ShortestPaths(GraphUtil.toAdjList(benzene), benzene, 0, 2, null);

assertThat(paths.distanceTo(0), is(0));
assertThat(paths.distanceTo(1), is(1));
assertThat(paths.distanceTo(2), is(2));
assertThat(paths.distanceTo(3), is(Integer.MAX_VALUE)); // dist > 2 (our limit)
assertThat(paths.distanceTo(4), is(2));
assertThat(paths.distanceTo(5), is(1));
}

@Test
public void testDistanceTo_Atom_Spiroundecane() {
Expand Down

0 comments on commit 222a959

Please sign in to comment.