本文整理了Java中com.google.common.graph.Graph.successors()
方法的一些代码示例,展示了Graph.successors()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph.successors()
方法的具体详情如下:
包路径:com.google.common.graph.Graph
类名称:Graph
方法名:successors
暂无
代码示例来源:origin: google/guava
@Override
public Set<N> predecessors(N node) {
return delegate().successors(node); // transpose
}
代码示例来源: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
@Override
public Set<N> predecessors(N node) {
return delegate().successors(node); // transpose
}
代码示例来源:origin: wildfly/wildfly
@Override
public Set<N> predecessors(N node) {
return delegate().successors(node); // transpose
}
代码示例来源:origin: google/guava
/**
* Performs a traversal of the nodes reachable from {@code node}. If we ever reach a node we've
* already visited (following only outgoing edges and without reusing edges), we know there's a
* cycle in the graph.
*/
private static <N> boolean subgraphHasCycle(
Graph<N> graph, Map<Object, NodeVisitState> visitedNodes, N node, @Nullable N previousNode) {
NodeVisitState state = visitedNodes.get(node);
if (state == NodeVisitState.COMPLETE) {
return false;
}
if (state == NodeVisitState.PENDING) {
return true;
}
visitedNodes.put(node, NodeVisitState.PENDING);
for (N nextNode : graph.successors(node)) {
if (canTraverseWithoutReusingEdge(graph, nextNode, previousNode)
&& subgraphHasCycle(graph, visitedNodes, nextNode, node)) {
return true;
}
}
visitedNodes.put(node, NodeVisitState.COMPLETE);
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: wildfly/wildfly
/**
* 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
/**
* Performs a traversal of the nodes reachable from {@code node}. If we ever reach a node we've
* already visited (following only outgoing edges and without reusing edges), we know there's a
* cycle in the graph.
*/
private static <N> boolean subgraphHasCycle(
Graph<N> graph,
Map<Object, NodeVisitState> visitedNodes,
N node,
@NullableDecl N previousNode) {
NodeVisitState state = visitedNodes.get(node);
if (state == NodeVisitState.COMPLETE) {
return false;
}
if (state == NodeVisitState.PENDING) {
return true;
}
visitedNodes.put(node, NodeVisitState.PENDING);
for (N nextNode : graph.successors(node)) {
if (canTraverseWithoutReusingEdge(graph, nextNode, previousNode)
&& subgraphHasCycle(graph, visitedNodes, nextNode, node)) {
return true;
}
}
visitedNodes.put(node, NodeVisitState.COMPLETE);
return false;
}
代码示例来源:origin: wildfly/wildfly
/**
* Performs a traversal of the nodes reachable from {@code node}. If we ever reach a node we've
* already visited (following only outgoing edges and without reusing edges), we know there's a
* cycle in the graph.
*/
private static <N> boolean subgraphHasCycle(
Graph<N> graph,
Map<Object, NodeVisitState> visitedNodes,
N node,
@NullableDecl N previousNode) {
NodeVisitState state = visitedNodes.get(node);
if (state == NodeVisitState.COMPLETE) {
return false;
}
if (state == NodeVisitState.PENDING) {
return true;
}
visitedNodes.put(node, NodeVisitState.PENDING);
for (N nextNode : graph.successors(node)) {
if (canTraverseWithoutReusingEdge(graph, nextNode, previousNode)
&& subgraphHasCycle(graph, visitedNodes, nextNode, node)) {
return true;
}
}
visitedNodes.put(node, NodeVisitState.COMPLETE);
return false;
}
代码示例来源: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: google/guava
/**
* Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph
* that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges}
* from {@code graph} for which both nodes are contained by {@code nodes}.
*
* @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph
*/
public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) {
MutableGraph<N> subgraph =
(nodes instanceof Collection)
? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build()
: GraphBuilder.from(graph).build();
for (N node : nodes) {
subgraph.addNode(node);
}
for (N node : subgraph.nodes()) {
for (N successorNode : graph.successors(node)) {
if (subgraph.nodes().contains(successorNode)) {
subgraph.putEdge(node, successorNode);
}
}
}
return subgraph;
}
代码示例来源: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: google/j2objc
/**
* Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph
* that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges}
* from {@code graph} for which both nodes are contained by {@code nodes}.
*
* @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph
*/
public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) {
MutableGraph<N> subgraph =
(nodes instanceof Collection)
? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build()
: GraphBuilder.from(graph).build();
for (N node : nodes) {
subgraph.addNode(node);
}
for (N node : subgraph.nodes()) {
for (N successorNode : graph.successors(node)) {
if (subgraph.nodes().contains(successorNode)) {
subgraph.putEdge(node, successorNode);
}
}
}
return subgraph;
}
代码示例来源: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
/**
* Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph
* that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges}
* from {@code graph} for which both nodes are contained by {@code nodes}.
*
* @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph
*/
public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) {
MutableGraph<N> subgraph =
(nodes instanceof Collection)
? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build()
: GraphBuilder.from(graph).build();
for (N node : nodes) {
subgraph.addNode(node);
}
for (N node : subgraph.nodes()) {
for (N successorNode : graph.successors(node)) {
if (subgraph.nodes().contains(successorNode)) {
subgraph.putEdge(node, successorNode);
}
}
}
return subgraph;
}
代码示例来源: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(graph.degree(node)).isEqualTo(graph.inDegree(node) + graph.outDegree(node));
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));
assertThat(graph.outDegree(node)).isEqualTo(graph.degree(node));
|| graph.successors(node).contains(adjacentNode))
.isTrue();
assertThat(graph.successors(predecessor)).contains(node);
assertThat(graph.hasEdgeConnecting(predecessor, node)).isTrue();
assertThat(graph.incidentEdges(node)).contains(EndpointPair.of(graph, predecessor, node));
for (N successor : sanityCheckSet(graph.successors(node))) {
allEndpointPairs.add(EndpointPair.of(graph, node, successor));
assertThat(graph.predecessors(successor)).contains(node);
代码示例来源: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
@Test
public void transpose_directedGraph() {
MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build();
directedGraph.putEdge(N1, N3);
directedGraph.putEdge(N3, N1);
directedGraph.putEdge(N1, N2);
directedGraph.putEdge(N1, N1);
directedGraph.putEdge(N3, N4);
MutableGraph<Integer> expectedTranspose = GraphBuilder.directed().allowsSelfLoops(true).build();
expectedTranspose.putEdge(N3, N1);
expectedTranspose.putEdge(N1, N3);
expectedTranspose.putEdge(N2, N1);
expectedTranspose.putEdge(N1, N1);
expectedTranspose.putEdge(N4, N3);
Graph<Integer> transpose = transpose(directedGraph);
assertThat(transpose).isEqualTo(expectedTranspose);
assertThat(transpose(transpose)).isSameAs(directedGraph);
AbstractGraphTest.validateGraph(transpose);
for (Integer node : directedGraph.nodes()) {
assertThat(directedGraph.inDegree(node)).isSameAs(transpose.outDegree(node));
assertThat(directedGraph.outDegree(node)).isSameAs(transpose.inDegree(node));
}
assertThat(transpose.successors(N1)).doesNotContain(N2);
directedGraph.putEdge(N2, N1);
// View should be updated.
assertThat(transpose.successors(N1)).contains(N2);
AbstractGraphTest.validateGraph(transpose);
}
代码示例来源:origin: google/guava
assertThat(network.successors(node)).isEqualTo(asGraph.successors(node));
内容来源于网络,如有侵权,请联系作者删除!