NetworkX

Previous topic

networkx.DiGraph.to_undirected

Next topic

networkx.DiGraph.reverse

networkx.DiGraph.subgraph

DiGraph.subgraph(nbunch, copy=True)

Return the subgraph induced on nodes in nbunch.

The induced subgraph of the graph has the nodes in nbunch as its node set and the edges adjacent to those nodes as its edge set.

Parameters:

nbunch : list, iterable

A container of nodes. The container will be iterated through once.

copy : bool, optional (default=True)

If True return a new graph holding the subgraph including copies of all edge and node properties. If False the subgraph is created using the original graph by deleting all nodes not in nbunch (this changes the original graph).

Returns:

G : Graph

A subgraph of the graph. If copy=True a new graph is returned with copies of graph, node, and edge data. If copy=False the subgraph is created in place by modifying the original graph.

Notes

If copy=True, nodes and edges are copied using copy.deepcopy.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> H = G.subgraph([0,1,2])
>>> print H.edges()
[(0, 1), (1, 2)]