This documents the development version of NetworkX. Documentation for the current release can be found here.
Returns a SubGraph view of
Gshowing 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.
- GNetworkX Graph
- nbunchnode, container of nodes or None (for all nodes)
- subgraphSubGraph View
A read-only view of the subgraph in
Ginduced by the nodes. Changes to the graph
Gwill be reflected in the view.
To create a mutable subgraph with its own copies of nodes edges and attributes use
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.subgraphdoes this when
Gis 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 = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc >>> H = G.subgraph([0, 1, 2]) >>> list(H.edges) [(0, 1), (1, 2)]