private DefaultDirectedGraph<String, DefaultEdge> createDag() { // A - B - C - D // \ / // +- E - F final DefaultDirectedGraph<String, DefaultEdge> graph = DefaultDirectedGraph.create(); graph.addVertex("A"); graph.addVertex("B"); graph.addVertex("C"); graph.addVertex("D"); graph.addVertex("E"); graph.addVertex("F"); graph.addEdge("A", "B"); graph.addEdge("B", "C"); graph.addEdge("C", "D"); graph.addEdge("A", "E"); graph.addEdge("E", "C"); graph.addEdge("E", "F"); return graph; }
public static <V, E extends DefaultEdge> DefaultDirectedGraph<V, E> create( EdgeFactory<V, E> edgeFactory) { return new DefaultDirectedGraph<>(edgeFactory); }
: graph1.vertexMap.values()) { for (E edge : arc.outEdges) { final V source = graph1.source(edge); final V target = graph1.target(edge); shortestPaths.put(Pair.of(source, target), ImmutableList.of(source, target)); for (Pair<V, V> edge2 : previous) { if (edge.target.equals(edge2.left)) { final Pair<V, V> key = Pair.of(graph1.source(edge), edge2.right); List<V> bestPath = shortestPaths.get(key); List<V> arc2Path = shortestPaths.get(edge2); || (bestPath.size() > (arc2Path.size() + 1))) { shortestPaths.put(key, cons(graph1.source(edge), arc2Path)); changeCount++;
public static <V> DefaultDirectedGraph<V, DefaultEdge> create() { return create(DefaultEdge.factory()); }
graph.addEdge("D", "E"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( graph.addEdge("D", "C"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( ImmutableSet.of("C", "D", "E", "F"))); graph.removeEdge("D", "E"); graph.removeEdge("D", "C"); graph.addEdge("C", "B"); graph.removeEdge("C", "B"); graph.addEdge("C", "C"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( graph.removeAllVertices(graph.vertexSet()); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo(ImmutableSet.of()));
/** Unit test for * {@link DefaultDirectedGraph#removeAllVertices(java.util.Collection)}. */ @Test public void testRemoveAllVertices() { final DefaultDirectedGraph<String, DefaultEdge> graph = createDag(); graph.removeAllVertices(Arrays.asList("B", "E")); assertEquals("[A, C, D, F]", graph.vertexSet().toString()); }
/** @deprecated Use {@link #addEdge(Object, Object, Object...)}. */ @Deprecated public E addEdge(V vertex, V targetVertex) { return super.addEdge(vertex, targetVertex); }
/** * 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); } }
public static <V> DefaultDirectedGraph<V, DefaultEdge> create() { return create(DefaultEdge.factory()); }
graph.addEdge("D", "E"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( graph.addEdge("D", "C"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( ImmutableSet.of("C", "D", "E", "F"))); graph.removeEdge("D", "E"); graph.removeEdge("D", "C"); graph.addEdge("C", "B"); graph.removeEdge("C", "B"); graph.addEdge("C", "C"); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo( graph.removeAllVertices(graph.vertexSet()); assertThat(new CycleDetector<String, DefaultEdge>(graph).findCycles(), CoreMatchers.equalTo(ImmutableSet.of()));
/** Unit test for * {@link DefaultDirectedGraph#removeAllVertices(java.util.Collection)}. */ @Test public void testRemoveAllVertices() { final DefaultDirectedGraph<String, DefaultEdge> graph = createDag(); graph.removeAllVertices(Arrays.asList("B", "E")); assertEquals("[A, C, D, F]", graph.vertexSet().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); } }
private DefaultDirectedGraph<String, DefaultEdge> createDag() { // A - B - C - D // \ / // +- E - F final DefaultDirectedGraph<String, DefaultEdge> graph = DefaultDirectedGraph.create(); graph.addVertex("A"); graph.addVertex("B"); graph.addVertex("C"); graph.addVertex("D"); graph.addVertex("E"); graph.addVertex("F"); graph.addEdge("A", "B"); graph.addEdge("B", "C"); graph.addEdge("C", "D"); graph.addEdge("A", "E"); graph.addEdge("E", "C"); graph.addEdge("E", "F"); return graph; }
/** * Creates a new HepPlanner with the option to keep the graph a * tree(noDAG=true) or allow DAG(noDAG=false). * * @param program program controlling rule application * @param onCopyHook Function to call when a node is copied */ public HepPlanner( HepProgram program, Context context, boolean noDAG, Function2<RelNode, RelNode, Void> onCopyHook, RelOptCostFactory costFactory) { super(costFactory, context); this.mainProgram = program; this.onCopyHook = Util.first(onCopyHook, Functions.ignore2()); mapDigestToVertex = new HashMap<>(); graph = DefaultDirectedGraph.create(); // NOTE jvs 24-Apr-2006: We use LinkedHashSet here and below // in order to provide deterministic behavior. allRules = new LinkedHashSet<>(); this.noDAG = noDAG; }
: graph1.vertexMap.values()) { for (E edge : arc.outEdges) { final V source = graph1.source(edge); final V target = graph1.target(edge); shortestPaths.put(Pair.of(source, target), ImmutableList.of(source, target)); for (Pair<V, V> edge2 : previous) { if (edge.target.equals(edge2.left)) { final Pair<V, V> key = Pair.of(graph1.source(edge), edge2.right); List<V> bestPath = shortestPaths.get(key); List<V> arc2Path = shortestPaths.get(edge2); || (bestPath.size() > (arc2Path.size() + 1))) { shortestPaths.put(key, cons(graph1.source(edge), arc2Path)); changeCount++;
public static <V, E extends DefaultEdge> DefaultDirectedGraph<V, E> create( EdgeFactory<V, E> edgeFactory) { return new DefaultDirectedGraph<>(edgeFactory); }
DefaultDirectedGraph.create(); graph.addVertex("A"); graph.addVertex("B"); graph.addVertex("C"); graph.addVertex("D"); graph.addVertex("E"); graph.addVertex("F"); graph.addEdge("A", "B"); graph.addEdge("B", "C"); graph.addEdge("A", "D"); graph.addEdge("D", "E"); graph.addEdge("C", "E"); final Graphs.FrozenGraph<String, DefaultEdge> frozenGraph = Graphs.makeImmutable(graph);
private DirectedGraph<Integer, DefaultEdge> createGraphFromExpression( final List<RexNode> exprs) { final DirectedGraph<Integer, DefaultEdge> graph = DefaultDirectedGraph.create(); for (int i = 0; i < exprs.size(); i++) { graph.addVertex(i); } for (final Ord<RexNode> expr : Ord.zip(exprs)) { expr.e.accept( new RexVisitorImpl<Void>(true) { public Void visitLocalRef(RexLocalRef localRef) { graph.addEdge(localRef.getIndex(), expr.i); return null; } }); } assert graph.vertexSet().size() == exprs.size(); return graph; } }
DefaultDirectedGraph.create(); graph.addVertex("A"); graph.addVertex("B"); graph.addVertex("C"); graph.addVertex("D"); graph.addVertex("E"); graph.addVertex("F"); graph.addEdge("A", "B"); graph.addEdge("B", "C"); graph.addEdge("A", "D"); graph.addEdge("D", "E"); graph.addEdge("C", "E"); final Graphs.FrozenGraph<String, DefaultEdge> frozenGraph = Graphs.makeImmutable(graph);
private DirectedGraph<Integer, DefaultEdge> createGraphFromExpression( final List<RexNode> exprs) { final DirectedGraph<Integer, DefaultEdge> graph = DefaultDirectedGraph.create(); for (int i = 0; i < exprs.size(); i++) { graph.addVertex(i); } for (final Ord<RexNode> expr : Ord.zip(exprs)) { expr.e.accept( new RexVisitorImpl<Void>(true) { public Void visitLocalRef(RexLocalRef localRef) { graph.addEdge(localRef.getIndex(), expr.i); return null; } }); } assert graph.vertexSet().size() == exprs.size(); return graph; } }