com.google.common.graph.Graph.isDirected()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中com.google.common.graph.Graph.isDirected()方法的一些代码示例,展示了Graph.isDirected()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph.isDirected()方法的具体详情如下:
包路径:com.google.common.graph.Graph
类名称:Graph
方法名:isDirected

Graph.isDirected介绍

[英]Returns true if the edges in this graph are directed. Directed edges connect a EndpointPair#source() to a EndpointPair#target(), while undirected edges connect a pair of nodes to each other.
[中]如果此图中的边是定向的,则返回true。有向边将端点对#source()连接到端点对#target(),而无向边将一对节点相互连接。

代码示例

代码示例来源:origin: google/guava

/**
 * 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);
}

代码示例来源:origin: google/guava

/**
 * 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;
}

代码示例来源:origin: google/j2objc

/**
 * 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);
}

代码示例来源:origin: google/guava

/** 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);
}

代码示例来源:origin: google/j2objc

/**
 * 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;
}

代码示例来源:origin: google/guava

/**
 * 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;
}

代码示例来源:origin: google/j2objc

/** 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);
}

代码示例来源:origin: google/guava

@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());
}

代码示例来源:origin: wildfly/wildfly

/** 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);
}

代码示例来源:origin: google/guava

/**
 * 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());
}

代码示例来源:origin: google/j2objc

@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());
}

代码示例来源:origin: wildfly/wildfly

@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());
}

代码示例来源:origin: google/j2objc

/**
 * 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());
}

代码示例来源:origin: google/guava

if (graph.isDirected()) {

代码示例来源:origin: google/guava

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));
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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());
}

代码示例来源:origin: google/j2objc

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));
}

代码示例来源:origin: wildfly/wildfly

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));
}

代码示例来源:origin: google/guava

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 {

代码示例来源:origin: google/guava

@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);
  }
 }
}

相关文章