private void findSimpleNodePathsBetween(N source, N target, GraphPath<N> path, Set<GraphPath<N>> paths) { for (final N next : getNodes(source, EdgeDirection.OUTGOING)) { if (next == target) { final GraphPath<N> tmp = new GraphPath<>(path); tmp.push(next); paths.add(tmp); } else if (!path.contains(next)) { path.push(next); findSimpleNodePathsBetween(next, target, path, paths); path.pop(); } } }
/** * Returns true when this path contains another path. * * @param other The other path. * @return True when other is a sub-path of this path. */ public boolean includes(GraphPath<T> other) { if (other.size() > size()) { return false; } else { final int delta = size() - other.size(); for (int d = 0; d <= delta; d++) { boolean match = true; for (int index = 0; index < other.size(); index++) { if (items.get(index + d) != other.items.get(index)) { match = false; break; } } if (match) { return true; } } return false; } }
/** * Finds all simple node paths between a source and a target. * * @param source The source node. * @param target The target node. * @return A set of all simple paths between source and target. */ public Set<GraphPath<N>> findSimpleNodePathsBetween(N source, N target) { final Set<GraphPath<N>> paths = new HashSet<>(); final GraphPath<N> path = new GraphPath<>(); path.push(source); findSimpleNodePathsBetween(source, target, path, paths); return paths; }
@Test public void testReduce() { final Set<GraphPath<String>> paths = new HashSet<>(); paths.add(new GraphPath<>("1", "2", "3")); paths.add(new GraphPath<>("1", "2")); paths.add(new GraphPath<>("2", "3")); paths.add(new GraphPath<>("2")); paths.add(new GraphPath<>("2, 3, 4")); paths.add(new GraphPath<>()); LOGGER.debug("Before reduction: " + paths); GraphPath.reduce(paths); LOGGER.debug("After reduction: " + paths); } }
public GraphPath(T item) { push(item); }
if (path.includes(next)) { included = true; break;
@SafeVarargs public GraphPath(T... items) { for (final T item : items) { push(item); } }