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