ArbitraryMatching { static int initial(final Graph g, Matching m, final BitSet s) { int nMatched = 0; for (int v = s.nextSetBit(0); v >= 0; v = s.nextSetBit(v + 1)) { if (m.matched(v)) continue; int d = g.degree(v); for (int j = 0; j < d; ++j) { Edge e = g.edgeAt(v, j); int w = e.other(v); if ((e.bond() != Bond.SINGLE) && m.unmatched(w) && s.get(w)) { m.match(v, w); nMatched += 2; break; } } } return nMatched; }    }
@Test public void furan() throws Exception { Graph g = Graph.fromSmiles("o1cccc1"); Matching m = Matching.empty(g); ArbitraryMatching.initial(g, m, allOf(1, 2, 3, 4)); assertThat(m.matches(), IsIterableWithSize.<Tuple>iterableWithSize(2)); assertThat(m.matches(), hasItems(Tuple.of(1, 2), Tuple.of(3, 4))); }
@Test public void furan_2() throws Exception { Graph g = Graph.fromSmiles("c1ccoc1"); Matching m = Matching.empty(g); ArbitraryMatching.initial(g, m, allOf(0, 1, 2, 4)); assertThat(m.matches(), IsIterableWithSize.<Tuple>iterableWithSize(1)); assertThat(m.matches(), hasItem(Tuple.of(0, 1))); }
@Test public void benzene() throws Exception { Graph g = Graph.fromSmiles("c1ccccc1"); Matching m = Matching.empty(g); ArbitraryMatching.initial(g, m, allOf(0, 1, 2, 3, 4, 5)); assertThat(m.matches(), IsIterableWithSize.<Tuple>iterableWithSize(3)); assertThat(m.matches(), hasItems(Tuple.of(0, 1), Tuple.of(2, 3), Tuple.of(4, 5))); }