Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.classes.function.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
  • G (NetworkX Graph)

  • edges (iterable) – An iterable of edges. Edges not present in G are ignored.

Returns

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

>>> import networkx as nx
>>> 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)]