networkx:creating a subgraph induced from edges

graph, networkx, python

Solution

If you have a list of edges, then you already have the subgraph. Just call `nx.Graph` on the list, and optionally add the (unconnected) nodes from the original graph. From the docs

Graph.__init__(data=None, **attr)

Initialize a graph with edges, name, graph attributes. Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object.

Problem

networkx only has a function Graph.subgraph() to create a subgraph induced from nodes. but how to construct a subgraph from edge list ? thanks !

Original source