return new FrozenGraph<>(graph1, shortestPaths);
/** * Returns an iterator of all paths between two nodes, shortest first. * * <p>The current implementation is not optimal.</p> */ public List<List<V>> getPaths(V from, V to) { List<List<V>> list = new ArrayList<>(); findPaths(from, to, list); return list; }
/** * 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); } }
return new FrozenGraph<>(graph1, shortestPaths);
final Graphs.FrozenGraph<String, DefaultEdge> frozenGraph = Graphs.makeImmutable(graph); assertEquals("[A, B]", frozenGraph.getShortestPath("A", "B").toString()); assertEquals("[[A, B]]", frozenGraph.getPaths("A", "B").toString()); assertEquals("[A, D, E]", frozenGraph.getShortestPath("A", "E").toString()); assertEquals("[[A, B, C, E], [A, D, E]]", frozenGraph.getPaths("A", "E").toString()); assertNull(frozenGraph.getShortestPath("B", "A")); assertNull(frozenGraph.getShortestPath("D", "C")); assertEquals("[[D, E]]", frozenGraph.getPaths("D", "E").toString()); assertEquals("[D, E]", frozenGraph.getShortestPath("D", "E").toString());
final Graphs.FrozenGraph<String, DefaultEdge> frozenGraph = Graphs.makeImmutable(graph); assertEquals("[A, B]", frozenGraph.getShortestPath("A", "B").toString()); assertEquals("[[A, B]]", frozenGraph.getPaths("A", "B").toString()); assertEquals("[A, D, E]", frozenGraph.getShortestPath("A", "E").toString()); assertEquals("[[A, B, C, E], [A, D, E]]", frozenGraph.getPaths("A", "E").toString()); assertNull(frozenGraph.getShortestPath("B", "A")); assertNull(frozenGraph.getShortestPath("D", "C")); assertEquals("[[D, E]]", frozenGraph.getPaths("D", "E").toString()); assertEquals("[D, E]", frozenGraph.getShortestPath("D", "E").toString());
/** * 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); } }
/** * Returns whether {@code table} uses one or more of the tables in * {@code usedTables}. */ private static boolean usesTable( List<String> qualifiedName, Set<RelOptTable> usedTables, Graphs.FrozenGraph<List<String>, DefaultEdge> usesGraph) { for (RelOptTable queryTable : usedTables) { if (usesGraph.getShortestPath(queryTable.getQualifiedName(), qualifiedName) != null) { return true; } } return false; } }
/** * Returns whether {@code table} uses one or more of the tables in * {@code usedTables}. */ private static boolean usesTable( List<String> qualifiedName, Set<RelOptTable> usedTables, Graphs.FrozenGraph<List<String>, DefaultEdge> usesGraph) { for (RelOptTable queryTable : usedTables) { if (usesGraph.getShortestPath(queryTable.getQualifiedName(), qualifiedName) != null) { return true; } } return false; } }
private <V> List<List<V>> paths(DirectedGraph<V, DefaultEdge> g, V source, V target) { return Graphs.makeImmutable(g).getPaths(source, target); }
private <V> List<List<V>> paths(DirectedGraph<V, DefaultEdge> g, V source, V target) { return Graphs.makeImmutable(g).getPaths(source, target); }
private <V> List<V> shortestPath(DirectedGraph<V, DefaultEdge> g, V source, V target) { return Graphs.makeImmutable(g).getShortestPath(source, target); }
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); }
public List<Convention> getShortestPath( Convention fromConvention, Convention toConvention) { return getPathMap().getShortestPath(fromConvention, toConvention); } }
public List<Convention> getShortestPath( Convention fromConvention, Convention toConvention) { return getPathMap().getShortestPath(fromConvention, toConvention); } }
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 <V> List<V> shortestPath(DirectedGraph<V, DefaultEdge> g, V source, V target) { return Graphs.makeImmutable(g).getShortestPath(source, target); }
public List<List<Convention>> getPaths( Convention fromConvention, Convention toConvention) { return getPathMap().getPaths(fromConvention, toConvention); }
/** * Returns an iterator of all paths between two nodes, shortest first. * * <p>The current implementation is not optimal.</p> */ public List<List<V>> getPaths(V from, V to) { List<List<V>> list = new ArrayList<>(); findPaths(from, to, list); return list; }
public List<List<Convention>> getPaths( Convention fromConvention, Convention toConvention) { return getPathMap().getPaths(fromConvention, toConvention); }