/** * Returns a view of {@code graph} with the direction (if any) of every edge reversed. All other * properties remain intact, and further updates to {@code graph} will be reflected in the view. */ public static <N> Graph<N> transpose(Graph<N> graph) { if (!graph.isDirected()) { return graph; // the transpose of an undirected graph is an identical graph } if (graph instanceof TransposedGraph) { return ((TransposedGraph<N>) graph).graph; } return new TransposedGraph<N>(graph); }
/** * Determines whether an edge has already been used during traversal. In the directed case a cycle * is always detected before reusing an edge, so no special logic is required. In the undirected * case, we must take care not to "backtrack" over an edge (i.e. going from A to B and then going * from B to A). */ private static boolean canTraverseWithoutReusingEdge( Graph<?> graph, Object nextNode, @Nullable Object previousNode) { if (graph.isDirected() || !Objects.equal(previousNode, nextNode)) { return true; } // This falls into the undirected A->B->A case. The Graph interface does not support parallel // edges, so this traversal would require reusing the undirected AB edge. return false; }
/** * Returns a view of {@code graph} with the direction (if any) of every edge reversed. All other * properties remain intact, and further updates to {@code graph} will be reflected in the view. */ public static <N> Graph<N> transpose(Graph<N> graph) { if (!graph.isDirected()) { return graph; // the transpose of an undirected graph is an identical graph } if (graph instanceof TransposedGraph) { return ((TransposedGraph<N>) graph).graph; } return new TransposedGraph<N>(graph); }
/** Returns an {@link EndpointPair} representing the endpoints of an edge in {@code graph}. */ static <N> EndpointPair<N> of(Graph<?> graph, N nodeU, N nodeV) { return graph.isDirected() ? ordered(nodeU, nodeV) : unordered(nodeU, nodeV); }
/** * Determines whether an edge has already been used during traversal. In the directed case a cycle * is always detected before reusing an edge, so no special logic is required. In the undirected * case, we must take care not to "backtrack" over an edge (i.e. going from A to B and then going * from B to A). */ private static boolean canTraverseWithoutReusingEdge( Graph<?> graph, Object nextNode, @NullableDecl Object previousNode) { if (graph.isDirected() || !Objects.equal(previousNode, nextNode)) { return true; } // This falls into the undirected A->B->A case. The Graph interface does not support parallel // edges, so this traversal would require reusing the undirected AB edge. return false; }
/** * Returns true if {@code graph} has at least one cycle. A cycle is defined as a non-empty subset * of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) starting * and ending with the same node. * * <p>This method will detect any non-empty cycle, including self-loops (a cycle of length 1). */ public static <N> boolean hasCycle(Graph<N> graph) { int numEdges = graph.edges().size(); if (numEdges == 0) { return false; // An edge-free graph is acyclic by definition. } if (!graph.isDirected() && numEdges >= graph.nodes().size()) { return true; // Optimization for the undirected case: at least one cycle must exist. } Map<Object, NodeVisitState> visitedNodes = Maps.newHashMapWithExpectedSize(graph.nodes().size()); for (N node : graph.nodes()) { if (subgraphHasCycle(graph, visitedNodes, node, null)) { return true; } } return false; }
/** Returns an {@link EndpointPair} representing the endpoints of an edge in {@code graph}. */ static <N> EndpointPair<N> of(Graph<?> graph, N nodeU, N nodeV) { return graph.isDirected() ? ordered(nodeU, nodeV) : unordered(nodeU, nodeV); }
@Override public final boolean equals(@Nullable Object obj) { if (obj == this) { return true; } if (!(obj instanceof Graph)) { return false; } Graph<?> other = (Graph<?>) obj; return isDirected() == other.isDirected() && nodes().equals(other.nodes()) && edges().equals(other.edges()); }
/** Returns an {@link EndpointPair} representing the endpoints of an edge in {@code graph}. */ static <N> EndpointPair<N> of(Graph<?> graph, N nodeU, N nodeV) { return graph.isDirected() ? ordered(nodeU, nodeV) : unordered(nodeU, nodeV); }
/** * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. * * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, * are not set in the new builder. */ public static <N> GraphBuilder<N> from(Graph<N> graph) { return new GraphBuilder<N>(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); }
@Override public final boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof Graph)) { return false; } Graph<?> other = (Graph<?>) obj; return isDirected() == other.isDirected() && nodes().equals(other.nodes()) && edges().equals(other.edges()); }
@Override public final boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof Graph)) { return false; } Graph<?> other = (Graph<?>) obj; return isDirected() == other.isDirected() && nodes().equals(other.nodes()) && edges().equals(other.edges()); }
/** * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. * * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, * are not set in the new builder. */ public static <N> GraphBuilder<N> from(Graph<N> graph) { return new GraphBuilder<N>(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); }
if (graph.isDirected()) {
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)); }
/** * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. * * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, * are not set in the new builder. */ public static <N> GraphBuilder<N> from(Graph<N> graph) { return new GraphBuilder<N>(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); }
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(graphString).contains("isDirected: " + graph.isDirected()); assertThat(graphString).contains("allowsSelfLoops: " + graph.allowsSelfLoops()); assertThat(nodeString).contains(node.toString()); if (graph.isDirected()) { assertThat(graph.degree(node)).isEqualTo(graph.inDegree(node) + graph.outDegree(node)); assertThat(graph.predecessors(node)).hasSize(graph.inDegree(node)); if (graph.isDirected()) { assertThat(graph.hasEdgeConnecting(endpoints.source(), endpoints.target())).isTrue(); } else {
@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); } } }