induced_subgraph#

induced_subgraph(G, nbunch)[source]#

Returns a SubGraph view of G showing only nodes in nbunch.

The induced subgraph of a graph on a set of nodes N is the graph with nodes N and edges from G which have both ends in N.

Parameters:
GNetworkX Graph
nbunchnode, container of nodes or None (for all nodes)
Returns:
subgraphSubGraph View

A read-only view of the subgraph in G induced by the nodes. Changes to the graph G will be reflected in the view.

Notes

To create a mutable subgraph with its own copies of nodes edges and attributes use subgraph.copy() or Graph(subgraph)

For an inplace reduction of a graph to a subgraph you can remove nodes: G.remove_nodes_from(n in G if n not in set(nbunch))

If you are going to compute subgraphs of your subgraphs you could end up with a chain of views that can be very slow once the chain has about 15 views in it. If they are all induced subgraphs, you can short-cut the chain by making them all subgraphs of the original graph. The graph class method G.subgraph does this when G is a subgraph. In contrast, this function allows you to choose to build chains or not, as you wish. The returned subgraph is a view on G.

Examples

>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> H = nx.induced_subgraph(G, [0, 1, 3])
>>> list(H.edges)
[(0, 1)]
>>> list(H.nodes)
[0, 1, 3]