Warning
This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.
subgraph¶
-
MultiGraph.
subgraph
(nbunch)[source]¶ Return the subgraph induced on nodes in nbunch.
The induced subgraph of the graph contains the nodes in nbunch and the edges between those nodes.
Parameters: nbunch (list, iterable) – A container of nodes which will be iterated through once. Returns: G – A subgraph of the graph with the same edge attributes. Return type: Graph Notes
The graph, edge or node attributes just point to the original graph. So changes to the node or edge structure will not be reflected in the original graph while changes to the attributes will.
To create a subgraph with its own copy of the edge/node attributes use: nx.Graph(G.subgraph(nbunch))
If edge attributes are containers, a deep copy can be obtained using: G.subgraph(nbunch).copy()
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)])
Examples
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc >>> G.add_path([0,1,2,3]) >>> H = G.subgraph([0,1,2]) >>> H.edges() [(0, 1), (1, 2)]