edge_subgraph

edge_subgraph(G, edges)[source]

Returns a view of the subgraph induced by the specified edges.

The induced subgraph contains each edge in edges and each node incident to any of those edges.

Parameters
GNetworkX Graph
edgesiterable

An iterable of edges. Edges not present in G are ignored.

Returns
subgraphSubGraph View

A read-only edge-induced subgraph of G. Changes to G are 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)

If you create a subgraph of a subgraph recursively you can end up with a chain of subgraphs that becomes very slow with about 15 nested subgraph views. Luckily the edge_subgraph filter nests nicely so you can use the original graph as G in this function to avoid chains. We do not rule out chains programmatically so that odd cases like an edge_subgraph of a restricted_view can be created.

Examples

>>> G = nx.path_graph(5)
>>> H = G.edge_subgraph([(0, 1), (3, 4)])
>>> list(H.nodes)
[0, 1, 3, 4]
>>> list(H.edges)
[(0, 1), (3, 4)]