MCPcopy
hub / github.com/google/guava / inducedSubgraph

Method inducedSubgraph

guava/src/com/google/common/graph/Graphs.java:562–578  ·  view source on GitHub ↗

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 el

(Graph<N> graph, Iterable<? extends N> nodes)

Source from the content-addressed store, hash-verified

560 * @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph
561 */
562 public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) {
563 MutableGraph<N> subgraph =
564 (nodes instanceof Collection)
565 ? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build()
566 : GraphBuilder.from(graph).build();
567 for (N node : nodes) {
568 subgraph.addNode(node);
569 }
570 for (N node : subgraph.nodes()) {
571 for (N successorNode : graph.successors(node)) {
572 if (subgraph.nodes().contains(successorNode)) {
573 subgraph.putEdge(node, successorNode);
574 }
575 }
576 }
577 return subgraph;
578 }
579
580 /**
581 * Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph

Calls 15

fromMethod · 0.95
fromMethod · 0.95
fromMethod · 0.95
sizeMethod · 0.65
addNodeMethod · 0.65
nodesMethod · 0.65
successorsMethod · 0.65
containsMethod · 0.65
putEdgeMethod · 0.65
putEdgeValueMethod · 0.65
edgeValueOrDefaultMethod · 0.65
outEdgesMethod · 0.65

Tested by 4

inducedSubgraph_graphMethod · 0.36