Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Parse a length limit to the cycle finding algorithm.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information...
commit f2adc5570f89679eab2e74bf05270485736237fd 1 parent 7b4ef80
John May johnmay authored egonw committed
18 base/core/src/main/java/org/openscience/cdk/graph/CycleFinder.java
View
@@ -48,18 +48,32 @@
* @return an instance for querying the cycles (rings) in the molecule
*/
Cycles find(IAtomContainer molecule) throws Intractable;
+
+ /**
+ * Find the cycles of the provided molecule.
+ *
+ * @param molecule a molecule, can be disconnected.
+ * @param length maximum length cycle to find (set to
+ * molecule.getAtomCount() for all)
+ * @throws Intractable thrown if problem could not be solved within some
+ * predefined bounds.
+ * @return an instance for querying the cycles (rings) in the molecule
+ */
+ Cycles find(IAtomContainer molecule, int length) throws Intractable;
/**
* Find the cycles of the provided molecule when an adjacent relation
* (graph) is already available. The graph can be obtained through {@link
* GraphUtil#toAdjList(IAtomContainer)}, for convenience {@link
- * #find(IAtomContainer)} will automatically create the graph.
+ * #find(IAtomContainer, int)} will automatically create the graph.
*
* @param molecule input structure
* @param graph adjacency list representation for fast traversal
+ * @param length maximum length cycle to find (set to
+ * molecule.getAtomCount() for all)
* @return an instance for querying the cycles (rings) in the molecule
* @throws Intractable thrown if problem could not be solved within some
* predefined bounds.
*/
- Cycles find(IAtomContainer molecule, int[][] graph) throws Intractable;
+ Cycles find(IAtomContainer molecule, int[][] graph, int length) throws Intractable;
}
52 base/core/src/main/java/org/openscience/cdk/graph/Cycles.java
View
@@ -482,7 +482,7 @@ public static CycleFinder or(CycleFinder primary, CycleFinder auxiliary) {
*/
@TestMethod("all")
public static Cycles all(IAtomContainer container) throws Intractable {
- return all().find(container);
+ return all().find(container, container.getAtomCount());
}
/**
@@ -495,7 +495,7 @@ public static Cycles all(IAtomContainer container) throws Intractable {
*/
@TestMethod("allUpToLength")
public static Cycles all(IAtomContainer container, int length) throws Intractable {
- return all(length).find(container);
+ return all(length).find(container, container.getAtomCount());
}
/**
@@ -658,8 +658,23 @@ public static Cycles edgeShort(IAtomContainer container) {
* @return the cycles of the molecule
*/
private static Cycles _invoke(CycleFinder finder, IAtomContainer container) {
+ return _invoke(finder, container, container.getAtomCount());
+ }
+
+ /**
+ * Internal method to wrap cycle computations which <i>should</i> be
+ * tractable. That is they currently won't throw the exception - if the
+ * method does throw an exception an internal error is triggered as a sanity
+ * check.
+ *
+ * @param finder the cycle finding method
+ * @param container the molecule to find the cycles of
+ * @param length maximum size or cycle to find
+ * @return the cycles of the molecule
+ */
+ private static Cycles _invoke(CycleFinder finder, IAtomContainer container, int length) {
try {
- return finder.find(container);
+ return finder.find(container, length);
} catch (Intractable e) {
throw new RuntimeException("Cycle computation should not be intractable: ",
e);
@@ -762,8 +777,13 @@ private static Cycles _invoke(CycleFinder finder, IAtomContainer container) {
*/
abstract int[][] apply(int[][] graph) throws Intractable;
- /** {@inheritDoc} */
+ /** @inheritDoc */
@Override public Cycles find(IAtomContainer molecule) throws Intractable {
+ return find(molecule, molecule.getAtomCount());
+ }
+
+ /** {@inheritDoc} */
+ @Override public Cycles find(IAtomContainer molecule, int length) throws Intractable {
EdgeToBondMap bondMap = EdgeToBondMap.withSpaceFor(molecule);
int[][] graph = GraphUtil.toAdjList(molecule, bondMap);
@@ -794,7 +814,7 @@ private static Cycles _invoke(CycleFinder finder, IAtomContainer container) {
}
/** @inheritDoc */
- @Override public Cycles find(IAtomContainer molecule, int[][] graph) throws Intractable {
+ @Override public Cycles find(IAtomContainer molecule, int[][] graph, int length) throws Intractable {
RingSearch ringSearch = new RingSearch(molecule, graph);
@@ -931,11 +951,16 @@ private AllUpToLength(int length) {
/** @inheritDoc */
@Override public Cycles find(IAtomContainer molecule) throws Intractable {
- return find(molecule, GraphUtil.toAdjList(molecule));
+ return find(molecule, molecule.getAtomCount());
}
/** @inheritDoc */
- @Override public Cycles find(IAtomContainer molecule, int[][] graph) throws Intractable {
+ @Override public Cycles find(IAtomContainer molecule, int length) throws Intractable {
+ return find(molecule, GraphUtil.toAdjList(molecule), length);
+ }
+
+ /** @inheritDoc */
+ @Override public Cycles find(IAtomContainer molecule, int[][] graph, int length) throws Intractable {
RingSearch ringSearch = new RingSearch(molecule, graph);
List<int[]> walks = new ArrayList<int[]>(6);
@@ -1002,16 +1027,21 @@ private Fallback(CycleFinder primary, CycleFinder auxiliary) {
/** @inheritDoc */
@Override public Cycles find(IAtomContainer molecule) throws Intractable {
- return find(molecule, GraphUtil.toAdjList(molecule));
+ return find(molecule, molecule.getAtomCount());
+ }
+
+ /** @inheritDoc */
+ @Override public Cycles find(IAtomContainer molecule, int length) throws Intractable {
+ return find(molecule, GraphUtil.toAdjList(molecule), length);
}
/** @inheritDoc */
- @Override public Cycles find(IAtomContainer molecule, int[][] graph) throws Intractable {
+ @Override public Cycles find(IAtomContainer molecule, int[][] graph, int length) throws Intractable {
try {
- return primary.find(molecule, graph);
+ return primary.find(molecule, graph, length);
} catch (Intractable e) {
// auxiliary may still thrown an exception
- return auxiliary.find(molecule, graph);
+ return auxiliary.find(molecule, graph, length);
}
}
}
2  base/standard/src/main/java/org/openscience/cdk/aromaticity/Aromaticity.java
View
@@ -199,7 +199,7 @@ public Aromaticity(ElectronDonation model,
// for each cycle if the electron sum is valid add the bonds of the
// cycle to the set or aromatic bonds
- for (final int[] cycle : cycles.find(molecule, subgraph).paths()) {
+ for (final int[] cycle : cycles.find(molecule, subgraph, subgraph.length).paths()) {
if (checkElectronSum(cycle, electrons, subset)) {
for (int i = 1; i < cycle.length; i++) {
bonds.add(bondMap.get(subset[cycle[i]], subset[cycle[i-1]]));
37 base/test-core/src/test/java/org/openscience/cdk/graph/CyclesTest.java
View
@@ -116,29 +116,29 @@
}
@Test public void cdkAromaticSet_withGraph() throws Exception {
- checkSize(Cycles.cdkAromaticSet().find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl())), 2);
- checkSize(Cycles.cdkAromaticSet().find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings())), 3);
- checkSize(Cycles.cdkAromaticSet().find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene())), 3);
- checkSize(Cycles.cdkAromaticSet().find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene())), 6);
- checkSize(Cycles.cdkAromaticSet().find(makeCyclophaneLike(), GraphUtil.toAdjList(makeCyclophaneLike())), 8);
- checkSize(Cycles.cdkAromaticSet().find(makeGappedCyclophaneLike(), GraphUtil.toAdjList(makeGappedCyclophaneLike())), 8);
+ checkSize(Cycles.cdkAromaticSet().find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl()), Integer.MAX_VALUE), 2);
+ checkSize(Cycles.cdkAromaticSet().find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings()), Integer.MAX_VALUE), 3);
+ checkSize(Cycles.cdkAromaticSet().find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene()), Integer.MAX_VALUE), 3);
+ checkSize(Cycles.cdkAromaticSet().find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene()), Integer.MAX_VALUE), 6);
+ checkSize(Cycles.cdkAromaticSet().find(makeCyclophaneLike(), GraphUtil.toAdjList(makeCyclophaneLike()), Integer.MAX_VALUE), 8);
+ checkSize(Cycles.cdkAromaticSet().find(makeGappedCyclophaneLike(), GraphUtil.toAdjList(makeGappedCyclophaneLike()), Integer.MAX_VALUE), 8);
}
@Test public void allOrVertexShort_withGraph() throws Exception {
- checkSize(Cycles.allOrVertexShort().find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl())), 2);
- checkSize(Cycles.allOrVertexShort().find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings())), 3);
- checkSize(Cycles.allOrVertexShort().find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene())), 3);
- checkSize(Cycles.allOrVertexShort().find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene())), 6);
- checkSize(Cycles.allOrVertexShort().find(makeCyclophaneLike(), GraphUtil.toAdjList(makeCyclophaneLike())), 135);
- checkSize(Cycles.allOrVertexShort().find(makeGappedCyclophaneLike(), GraphUtil.toAdjList(makeGappedCyclophaneLike())), 135);
- checkSize(Cycles.allOrVertexShort().find(fullerene(), GraphUtil.toAdjList(fullerene())), 120);
+ checkSize(Cycles.allOrVertexShort().find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl()), Integer.MAX_VALUE), 2);
+ checkSize(Cycles.allOrVertexShort().find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings()), Integer.MAX_VALUE), 3);
+ checkSize(Cycles.allOrVertexShort().find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene()), Integer.MAX_VALUE), 3);
+ checkSize(Cycles.allOrVertexShort().find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene()), Integer.MAX_VALUE), 6);
+ checkSize(Cycles.allOrVertexShort().find(makeCyclophaneLike(), GraphUtil.toAdjList(makeCyclophaneLike()), Integer.MAX_VALUE), 135);
+ checkSize(Cycles.allOrVertexShort().find(makeGappedCyclophaneLike(), GraphUtil.toAdjList(makeGappedCyclophaneLike()), Integer.MAX_VALUE), 135);
+ checkSize(Cycles.allOrVertexShort().find(fullerene(), GraphUtil.toAdjList(fullerene()), Integer.MAX_VALUE), 120);
}
@Test public void allUpToLength() throws Exception {
- checkSize(Cycles.all(6).find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl())), 2);
- checkSize(Cycles.all(6).find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings())), 3);
- checkSize(Cycles.all(6).find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene())), 2);
- checkSize(Cycles.all(6).find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene())), 3);
+ checkSize(Cycles.all(6).find(makeBiphenyl(), GraphUtil.toAdjList(makeBiphenyl()), Integer.MAX_VALUE), 2);
+ checkSize(Cycles.all(6).find(makeBicycloRings(), GraphUtil.toAdjList(makeBicycloRings()), Integer.MAX_VALUE), 3);
+ checkSize(Cycles.all(6).find(makeNaphthalene(), GraphUtil.toAdjList(makeNaphthalene()), Integer.MAX_VALUE), 2);
+ checkSize(Cycles.all(6).find(makeAnthracene(), GraphUtil.toAdjList(makeAnthracene()), Integer.MAX_VALUE), 3);
}
@Test public void pathsAreCopy() throws Exception {
@@ -189,7 +189,8 @@
@Test public void or() throws Exception {
CycleFinder cf = Cycles.or(Cycles.all(), Cycles.all(3));
- checkSize(cf.find(fullerene()), 120);
+ IAtomContainer fullerene = fullerene();
+ checkSize(cf.find(fullerene, fullerene.getAtomCount()), 120);
}
// load a boron fullerene
Please sign in to comment.
Something went wrong with that request. Please try again.