@Override public Set<N> successors(N node) { return delegate().predecessors(node); // transpose }
@Override public Set<N> successors(N node) { return delegate().predecessors(node); // transpose }
@Override public Set<N> successors(N node) { return delegate().predecessors(node); // transpose }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
assertThat(graph.predecessors(node)).hasSize(graph.inDegree(node)); assertThat(graph.successors(node)).hasSize(graph.outDegree(node)); } else { int selfLoopCount = graph.adjacentNodes(node).contains(node) ? 1 : 0; assertThat(graph.degree(node)).isEqualTo(graph.adjacentNodes(node).size() + selfLoopCount); assertThat(graph.predecessors(node)).isEqualTo(graph.adjacentNodes(node)); assertThat(graph.successors(node)).isEqualTo(graph.adjacentNodes(node)); assertThat(graph.inDegree(node)).isEqualTo(graph.degree(node)); graph.predecessors(node).contains(adjacentNode) || graph.successors(node).contains(adjacentNode)) .isTrue(); for (N predecessor : sanityCheckSet(graph.predecessors(node))) { assertThat(graph.successors(predecessor)).contains(node); assertThat(graph.hasEdgeConnecting(predecessor, node)).isTrue(); assertThat(graph.predecessors(successor)).contains(node); assertThat(graph.hasEdgeConnecting(node, successor)).isTrue(); assertThat(graph.incidentEdges(node)).contains(EndpointPair.of(graph, node, successor));
@After public void validateGraphState() { assertStronglyEquivalent(graph, Graphs.copyOf(graph)); assertStronglyEquivalent(graph, ImmutableValueGraph.copyOf(graph)); Graph<Integer> asGraph = graph.asGraph(); AbstractGraphTest.validateGraph(asGraph); assertThat(graph.nodes()).isEqualTo(asGraph.nodes()); assertThat(graph.edges()).isEqualTo(asGraph.edges()); assertThat(graph.nodeOrder()).isEqualTo(asGraph.nodeOrder()); assertThat(graph.isDirected()).isEqualTo(asGraph.isDirected()); assertThat(graph.allowsSelfLoops()).isEqualTo(asGraph.allowsSelfLoops()); for (Integer node : graph.nodes()) { assertThat(graph.adjacentNodes(node)).isEqualTo(asGraph.adjacentNodes(node)); assertThat(graph.predecessors(node)).isEqualTo(asGraph.predecessors(node)); assertThat(graph.successors(node)).isEqualTo(asGraph.successors(node)); assertThat(graph.degree(node)).isEqualTo(asGraph.degree(node)); assertThat(graph.inDegree(node)).isEqualTo(asGraph.inDegree(node)); assertThat(graph.outDegree(node)).isEqualTo(asGraph.outDegree(node)); for (Integer otherNode : graph.nodes()) { boolean hasEdge = graph.hasEdgeConnecting(node, otherNode); assertThat(hasEdge).isEqualTo(asGraph.hasEdgeConnecting(node, otherNode)); assertThat(graph.edgeValueOrDefault(node, otherNode, null) != null).isEqualTo(hasEdge); assertThat(!graph.edgeValueOrDefault(node, otherNode, DEFAULT).equals(DEFAULT)) .isEqualTo(hasEdge); } } }
assertThat(network.predecessors(node)).isEqualTo(asGraph.predecessors(node)); assertThat(network.successors(node)).isEqualTo(asGraph.successors(node));
protected static <N, E> boolean link(Graph<N> g, N a, N b) { return g.predecessors(b).contains(a); }
@Override public Set<EndpointPair<V>> incomingEdgesOf(V vertex) { return graph.predecessors(vertex).stream().map(other -> createEdge(other, vertex)).collect( collectingAndThen(toSet(), Collections::unmodifiableSet)); }
@Override public Set<N> successors(N node) { return delegate().predecessors(node); // transpose }
@Override public Set<N> successors(N node) { return delegate().predecessors(node); // transpose }
public static <N> ImmutableSet<N> roots(Graph<N> graph) { checkNotNull(graph, "graph"); return graph .nodes() .stream() .filter(node -> graph.predecessors(node).isEmpty()) .collect(toImmutableSet()); }
/** * A graph is "forest-shaped" if it is directed, acyclic, and each node has at most one * predecessor. */ public static <N> boolean isForestShaped(Graph<N> graph) { checkNotNull(graph, "graph"); return graph.isDirected() && !Graphs.hasCycle(graph) && graph.nodes().stream().allMatch(node -> graph.predecessors(node).size() <= 1); }
/** * A recursive method for allocating the level for each node. Ensures that all predecessors of v * have a level which is at least one greater than the level of v. * * @param node the node whose minimum level is to be calculated */ public void propagateMinimumLevel(N node) { Graph<N> graph = layoutModel.getGraph(); int level = minLevels.get(node).intValue(); for (N child : graph.predecessors(node)) { int oldLevel, newLevel; Number o = minLevels.get(child); if (o != null) { oldLevel = o.intValue(); } else { oldLevel = 0; } newLevel = Math.max(oldLevel, level + 1); minLevels.put(child, new Integer(newLevel)); if (newLevel > graphHeight) { graphHeight = newLevel; } propagateMinimumLevel(child); } }
/** * @param node the node whose center is to be returned * @return the coordinates of {@code node}'s parent, or the center of this layout's area if it's a * root. */ public Point getCenter(LayoutModel<N> layoutModel, N node) { Graph<N> graph = layoutModel.getGraph(); N parent = Iterables.getOnlyElement(graph.predecessors(node), null); if (parent == null) { return getCenter(layoutModel); } return layoutModel.get(parent); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
@After public void validateGraphState() { assertStronglyEquivalent(graph, Graphs.copyOf(graph)); assertStronglyEquivalent(graph, ImmutableValueGraph.copyOf(graph)); Graph<Integer> asGraph = graph.asGraph(); AbstractGraphTest.validateGraph(asGraph); assertThat(graph.nodes()).isEqualTo(asGraph.nodes()); assertThat(graph.edges()).isEqualTo(asGraph.edges()); assertThat(graph.nodeOrder()).isEqualTo(asGraph.nodeOrder()); assertThat(graph.isDirected()).isEqualTo(asGraph.isDirected()); assertThat(graph.allowsSelfLoops()).isEqualTo(asGraph.allowsSelfLoops()); for (Integer node : graph.nodes()) { assertThat(graph.adjacentNodes(node)).isEqualTo(asGraph.adjacentNodes(node)); assertThat(graph.predecessors(node)).isEqualTo(asGraph.predecessors(node)); assertThat(graph.successors(node)).isEqualTo(asGraph.successors(node)); assertThat(graph.degree(node)).isEqualTo(asGraph.degree(node)); assertThat(graph.inDegree(node)).isEqualTo(asGraph.inDegree(node)); assertThat(graph.outDegree(node)).isEqualTo(asGraph.outDegree(node)); for (Integer otherNode : graph.nodes()) { boolean hasEdge = graph.hasEdgeConnecting(node, otherNode); assertThat(hasEdge).isEqualTo(asGraph.hasEdgeConnecting(node, otherNode)); assertThat(graph.edgeValueOrDefault(node, otherNode, null) != null).isEqualTo(hasEdge); assertThat(!graph.edgeValueOrDefault(node, otherNode, DEFAULT).equals(DEFAULT)) .isEqualTo(hasEdge); } } }