Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Limit the size of initial cycles discovered.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information...
commit 7b4ef806f38444ab90abdd82670a0cb4af500479 1 parent 222a959
@johnmay johnmay authored egonw committed
View
39 base/core/src/main/java/org/openscience/cdk/graph/InitialCycles.java
@@ -76,6 +76,9 @@
/** Number of vertices which have degree 2. */
private int nDeg2Vertices;
+
+ /** Limit the size of cycles discovered. */
+ private final int limit;
/**
* Is the graph known to be a biconnected component. This allows a small
@@ -90,7 +93,18 @@
* @throws NullPointerException the graph was null
*/
InitialCycles(final int[][] graph) {
- this(graph, false);
+ this(graph, graph.length, false);
+ }
+
+ /**
+ * Create a set of initial cycles for the provided graph.
+ *
+ * @param graph input graph
+ * @param limit the maximum size of cycle found
+ * @throws NullPointerException the graph was null
+ */
+ InitialCycles(final int[][] graph, int limit) {
+ this(graph, limit, false);
}
/**
@@ -101,11 +115,12 @@
* @param biconnected the graph is known to be biconnected
* @throws NullPointerException the graph was null
*/
- private InitialCycles(final int[][] graph, boolean biconnected) {
+ private InitialCycles(final int[][] graph, final int limit, boolean biconnected) {
this.graph = checkNotNull(graph, "no graph provided");
// ordering ensures the number of initial cycles is polynomial
this.biconnected = biconnected;
+ this.limit = limit;
this.ordering = ordering(graph);
// index the edges to allow us to jump between edge and path representation
@@ -259,7 +274,7 @@ private void compute() {
for (int i = first; i < n; i++) {
final int r = vertices[i];
- ShortestPaths pathsFromR = new ShortestPaths(graph, null, r, ordering);
+ ShortestPaths pathsFromR = new ShortestPaths(graph, null, r, limit / 2, ordering);
// we only check the vertices which belong to the set Vr. this
// set is vertices reachable from 'r' by only travelling though
@@ -359,7 +374,8 @@ else if (distToZ == distToY && ordering[z] < ordering[y]) {
* @param cycle the cycle to add
*/
private void add(Cycle cycle) {
- cycles.put(cycle.length(), cycle);
+ if (cycle.length() <= limit)
+ cycles.put(cycle.length(), cycle);
}
/**
@@ -461,7 +477,20 @@ static boolean singletonIntersect(final int[] p, final int[] q) {
*/
@TestMethod("bioconnected_simpleCycle")
static InitialCycles ofBiconnectedComponent(int[][] graph) {
- return new InitialCycles(graph, true);
+ return ofBiconnectedComponent(graph, graph.length);
+ }
+
+ /**
+ * Compute the initial cycles of a biconnected graph.
+ *
+ * @param graph the biconnected graph
+ * @param limit maximum size of the cycle to find
+ * @return computed initial cycles
+ * @throws NullPointerException the graph was null
+ */
+ @TestMethod("bioconnected_simpleCycle_limit_5")
+ static InitialCycles ofBiconnectedComponent(int[][] graph, int limit) {
+ return new InitialCycles(graph, limit, true);
}
/**
View
30 base/test-core/src/test/java/org/openscience/cdk/graph/InitialCyclesTest.java
@@ -180,6 +180,26 @@
assertThat(cycles.get(1)
.path(), is(new int[]{3, 2, 1, 0, 10, 9, 8, 7, 6, 3}));
}
+
+ @Test public void cycles_cyclophane_odd_limit_5() throws IOException {
+ InitialCycles initial = new InitialCycles(cyclophane_odd(), 5);
+ List<InitialCycles.Cycle> cycles = Lists.newArrayList(initial.cycles());
+ assertThat(cycles.size(), is(0));
+ }
+
+ @Test public void cycles_cyclophane_odd_limit_6() throws IOException {
+ InitialCycles initial = new InitialCycles(cyclophane_odd(), 6);
+ List<InitialCycles.Cycle> cycles = Lists.newArrayList(initial.cycles());
+ assertThat(cycles.size(), is(1));
+ assertThat(cycles.get(0).path(), is(new int[]{3, 2, 1, 0, 5, 4, 3}));
+ }
+
+ @Test public void cycles_cyclophane_odd_limit_7() throws IOException {
+ InitialCycles initial = new InitialCycles(cyclophane_odd(), 7);
+ List<InitialCycles.Cycle> cycles = Lists.newArrayList(initial.cycles());
+ assertThat(cycles.size(), is(1));
+ assertThat(cycles.get(0).path(), is(new int[]{3, 2, 1, 0, 5, 4, 3}));
+ }
@Test public void cycles_family_odd() {
InitialCycles initial = new InitialCycles(cyclophane_odd());
@@ -208,6 +228,16 @@
@Test public void bioconnected_simpleCycle() {
InitialCycles ic = InitialCycles.ofBiconnectedComponent(cyclohexane());
assertThat(ic.numberOfCycles(), is(1));
+ }
+
+ @Test public void bioconnected_simpleCycle_limit_5() {
+ InitialCycles ic = InitialCycles.ofBiconnectedComponent(cyclohexane(), 5);
+ assertThat(ic.numberOfCycles(), is(0));
+ }
+
+ @Test public void bioconnected_simpleCycle_limit_6() {
+ InitialCycles ic = InitialCycles.ofBiconnectedComponent(cyclohexane(), 6);
+ assertThat(ic.numberOfCycles(), is(1));
}
@Test public void join() {
Please sign in to comment.
Something went wrong with that request. Please try again.