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

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

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

Graph.nodes介绍

[英]Returns all nodes in this graph, in the order specified by #nodeOrder().
[中]按#nodeOrder()指定的顺序返回此图中的所有节点。

代码示例

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

private static <N> ImmutableMap<N, GraphConnections<N, Presence>> getNodeConnections(
  Graph<N> graph) {
 // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
 // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
 // input nodes are sorted.
 ImmutableMap.Builder<N, GraphConnections<N, Presence>> nodeConnections = ImmutableMap.builder();
 for (N node : graph.nodes()) {
  nodeConnections.put(node, connectionsOf(graph, node));
 }
 return nodeConnections.build();
}

代码示例来源: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/guava

/**
 * Returns the set of nodes that are reachable from {@code node}. Node B is defined as reachable
 * from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A
 * and ending at node B. Note that a node is always reachable from itself via a zero-length path.
 *
 * <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view
 * of the set of nodes reachable from {@code node}. In other words, the returned {@link Set} will
 * not be updated after modifications to {@code graph}.
 *
 * @throws IllegalArgumentException if {@code node} is not present in {@code graph}
 */
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
 checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node);
 Set<N> visitedNodes = new LinkedHashSet<N>();
 Queue<N> queuedNodes = new ArrayDeque<N>();
 visitedNodes.add(node);
 queuedNodes.add(node);
 // Perform a breadth-first traversal rooted at the input node.
 while (!queuedNodes.isEmpty()) {
  N currentNode = queuedNodes.remove();
  for (N successor : graph.successors(currentNode)) {
   if (visitedNodes.add(successor)) {
    queuedNodes.add(successor);
   }
  }
 }
 return Collections.unmodifiableSet(visitedNodes);
}

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

private static <N> ImmutableMap<N, GraphConnections<N, Presence>> getNodeConnections(
  Graph<N> graph) {
 // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
 // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
 // input nodes are sorted.
 ImmutableMap.Builder<N, GraphConnections<N, Presence>> nodeConnections = ImmutableMap.builder();
 for (N node : graph.nodes()) {
  nodeConnections.put(node, connectionsOf(graph, node));
 }
 return nodeConnections.build();
}

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

private static <N> ImmutableMap<N, GraphConnections<N, Presence>> getNodeConnections(
  Graph<N> graph) {
 // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
 // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
 // input nodes are sorted.
 ImmutableMap.Builder<N, GraphConnections<N, Presence>> nodeConnections = ImmutableMap.builder();
 for (N node : graph.nodes()) {
  nodeConnections.put(node, connectionsOf(graph, node));
 }
 return nodeConnections.build();
}

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

/**
 * 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 the set of nodes that are reachable from {@code node}. Node B is defined as reachable
 * from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A
 * and ending at node B. Note that a node is always reachable from itself via a zero-length path.
 *
 * <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view
 * of the set of nodes reachable from {@code node}. In other words, the returned {@link Set} will
 * not be updated after modifications to {@code graph}.
 *
 * @throws IllegalArgumentException if {@code node} is not present in {@code graph}
 */
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
 checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node);
 Set<N> visitedNodes = new LinkedHashSet<N>();
 Queue<N> queuedNodes = new ArrayDeque<N>();
 visitedNodes.add(node);
 queuedNodes.add(node);
 // Perform a breadth-first traversal rooted at the input node.
 while (!queuedNodes.isEmpty()) {
  N currentNode = queuedNodes.remove();
  for (N successor : graph.successors(currentNode)) {
   if (visitedNodes.add(successor)) {
    queuedNodes.add(successor);
   }
  }
 }
 return Collections.unmodifiableSet(visitedNodes);
}

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

/** Creates a mutable copy of {@code graph} with the same nodes and edges. */
public static <N> MutableGraph<N> copyOf(Graph<N> graph) {
 MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build();
 for (N node : graph.nodes()) {
  copy.addNode(node);
 }
 for (EndpointPair<N> edge : graph.edges()) {
  copy.putEdge(edge.nodeU(), edge.nodeV());
 }
 return copy;
}

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

/**
 * 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/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: google/guava

for (N node : graph.nodes()) {
 for (N reachableNode : reachableNodes(graph, node)) {
  transitiveClosure.putEdge(node, reachableNode);
for (N node : graph.nodes()) {
 if (!visitedNodes.contains(node)) {
  Set<N> reachableNodes = reachableNodes(graph, node);

代码示例来源:origin: Graylog2/graylog2-server

private MutableGraph<EntityDescriptor> resolveDependencyGraph(Graph<EntityDescriptor> dependencyGraph, Set<EntityDescriptor> resolvedEntities) {
  final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.from(dependencyGraph).build();
  Graphs.merge(mutableGraph, dependencyGraph);
  for (EntityDescriptor entityDescriptor : dependencyGraph.nodes()) {
    LOG.debug("Resolving entity {}", entityDescriptor);
    if (resolvedEntities.contains(entityDescriptor)) {
      LOG.debug("Entity {} already resolved, skipping.", entityDescriptor);
      continue;
    }
    final EntityFacade<?> facade = entityFacades.getOrDefault(entityDescriptor.type(), UnsupportedEntityFacade.INSTANCE);
    final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(entityDescriptor);
    LOG.trace("Dependencies of entity {}: {}", entityDescriptor, graph);
    Graphs.merge(mutableGraph, graph);
    LOG.trace("New dependency graph: {}", mutableGraph);
    resolvedEntities.add(entityDescriptor);
    final Graph<EntityDescriptor> result = resolveDependencyGraph(mutableGraph, resolvedEntities);
    Graphs.merge(mutableGraph, result);
  }
  return mutableGraph;
}

代码示例来源: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

/** Creates a mutable copy of {@code graph} with the same nodes and edges. */
public static <N> MutableGraph<N> copyOf(Graph<N> graph) {
 MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build();
 for (N node : graph.nodes()) {
  copy.addNode(node);
 }
 for (EndpointPair<N> edge : graph.edges()) {
  copy.putEdge(edge.nodeU(), edge.nodeV());
 }
 return copy;
}

代码示例来源: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/guava

private static <N> void checkTransitiveClosure(Graph<N> originalGraph, Graph<N> expectedClosure) {
 for (N node : originalGraph.nodes()) {
  assertThat(reachableNodes(originalGraph, node)).isEqualTo(expectedClosure.successors(node));
 }
 assertThat(transitiveClosure(originalGraph)).isEqualTo(expectedClosure);
}

代码示例来源:origin: Graylog2/graylog2-server

/**
   * Merge all nodes and edges of two graphs.
   *
   * @param graph1 A {@link MutableGraph} into which all nodes and edges of {@literal graph2} will be merged
   * @param graph2 The {@link Graph} whose nodes and edges will be merged into {@literal graph1}
   * @param <N>    The class of the nodes
   */
  public static <N> void merge(MutableGraph<N> graph1, Graph<N> graph2) {
    for (N node : graph2.nodes()) {
      graph1.addNode(node);
    }
    for (EndpointPair<N> edge : graph2.edges()) {
      graph1.putEdge(edge.nodeU(), edge.nodeV());
    }
  }
}

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

/** Creates a mutable copy of {@code graph} with the same nodes, edges, and edge values. */
public static <N, V> MutableValueGraph<N, V> copyOf(ValueGraph<N, V> graph) {
 MutableValueGraph<N, V> copy =
   ValueGraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build();
 for (N node : graph.nodes()) {
  copy.addNode(node);
 }
 for (EndpointPair<N> edge : graph.edges()) {
  copy.putEdgeValue(
    edge.nodeU(), edge.nodeV(), graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null));
 }
 return copy;
}

代码示例来源: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);
  }
 }
}

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

for (N node : sanityCheckSet(graph.nodes())) {
 assertThat(nodeString).contains(node.toString());

相关文章