private void findPaths(V from, V to, List<List<V>> list) { final List<V> shortestPath = shortestPaths.get(Pair.of(from, to)); if (shortestPath == null) { return; } // final E edge = graph.getEdge(from, to); // if (edge != null) { // list.add(ImmutableList.of(from, to)); // } final List<V> prefix = new ArrayList<>(); prefix.add(from); findPathsExcluding(from, to, list, new HashSet<>(), prefix); }
private void findPaths(V from, V to, List<List<V>> list) { final List<V> shortestPath = shortestPaths.get(Pair.of(from, to)); if (shortestPath == null) { return; } // final E edge = graph.getEdge(from, to); // if (edge != null) { // list.add(ImmutableList.of(from, to)); // } final List<V> prefix = new ArrayList<>(); prefix.add(from); findPathsExcluding(from, to, list, new HashSet<>(), prefix); }
/** * Finds all paths from "from" to "to" of length 2 or greater, such that the * intermediate nodes are not contained in "excludedNodes". */ private void findPathsExcluding(V from, V to, List<List<V>> list, Set<V> excludedNodes, List<V> prefix) { excludedNodes.add(from); for (E edge : graph.edges) { if (edge.source.equals(from)) { final V target = graph.target(edge); if (target.equals(to)) { // We found a path. prefix.add(target); list.add(ImmutableList.copyOf(prefix)); prefix.remove(prefix.size() - 1); } else if (excludedNodes.contains(target)) { // ignore it } else { prefix.add(target); findPathsExcluding(target, to, list, excludedNodes, prefix); prefix.remove(prefix.size() - 1); } } } excludedNodes.remove(from); } }
/** * Finds all paths from "from" to "to" of length 2 or greater, such that the * intermediate nodes are not contained in "excludedNodes". */ private void findPathsExcluding(V from, V to, List<List<V>> list, Set<V> excludedNodes, List<V> prefix) { excludedNodes.add(from); for (E edge : graph.edges) { if (edge.source.equals(from)) { final V target = graph.target(edge); if (target.equals(to)) { // We found a path. prefix.add(target); list.add(ImmutableList.copyOf(prefix)); prefix.remove(prefix.size() - 1); } else if (excludedNodes.contains(target)) { // ignore it } else { prefix.add(target); findPathsExcluding(target, to, list, excludedNodes, prefix); prefix.remove(prefix.size() - 1); } } } excludedNodes.remove(from); } }