networkx.classes.function.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:  G (NetworkX Graph)
 nbunch (node, container of nodes or None (for all nodes))
Returns: subgraph – A readonly view of the subgraph in
G
induced by the nodes. Changes to the graphG
will be reflected in the view.Return type: SubGraph View
Notes
To create a mutable subgraph with its own copies of nodes edges and attributes use
subgraph.copy()
orGraph(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 shortcut the chain by making them all subgraphs of the original graph. The graph class method
G.subgraph
does this whenG
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 onG
.Examples
>>> import networkx as nx >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc >>> H = G.subgraph([0, 1, 2]) >>> list(H.edges) [(0, 1), (1, 2)]