'Returns the subgraph induced by the specified edges.
The induced subgraph contains each edge in `edges` and each
node incident to any one of those edges.
Parameters
edges : iterable
An iterable of edges in this graph.
Returns
G : Graph
An edge-induced subgraph of this graph with the same edge
attributes.
Notes
The graph, edge, and node attributes in the returned subgraph
are references to the corresponding attributes in the original
graph. Thus changes to the node or edge structure of the
returned graph will not be reflected in the original graph, but
changes to the attributes will.
To create a subgraph with its own copy of the edge or node
attributes, use::
>>> nx.MultiDiGraph(G.edge_subgraph(edges))  # doctest: +SKIP
If edge attributes are containers, a deep copy of the attributes
can be obtained using::
>>> G.edge_subgraph(edges).copy()  # doctest: +SKIP
Examples
Get a subgraph induced by only those edges that have a certain
attribute::
>>> # Create a graph in which some edges are "good" and some "bad".
>>> G = nx.MultiDiGraph()
>>> key = G.add_edge(0, 1, key=0, good=True)
>>> key = G.add_edge(0, 1, key=1, good=False)
>>> key = G.add_edge(1, 2, key=0, good=False)
>>> key = G.add_edge(1, 2, key=1, good=True)
>>> # Keep only those edges that are marked as "good".
>>> edges = G.edges(keys=True, data=\'good\')
>>> edges = ((u, v, k) for (u, v, k, good) in edges if good)
>>> H = G.edge_subgraph(edges)
>>> list(H.edges(keys=True, data=True))
[(0, 1, 0, {\'good\': True}), (1, 2, 1, {\'good\': True})]'

def edge_subgraph(self, edges):

    H = self.__class__()
    succ = self._succ
    def is_in_graph(u, v, k):
        return ((u in succ) and (v in succ[u]) and (k in succ[u][v]))
    edges = (e for e in edges if is_in_graph(*e))
    for (u, v, k) in edges:
        if (u not in H.node):
            H._node[u] = self._node[u]
        if (v not in H.node):
            H._node[v] = self._node[v]
        if (u not in H._succ):
            H._succ[u] = H.adjlist_inner_dict_factory()
        if (v not in H._pred):
            H._pred[v] = H.adjlist_inner_dict_factory()
        if (v not in H._succ[u]):
            H._succ[u][v] = H.edge_key_dict_factory()
        if (u not in H._pred[v]):
            H._pred[v][u] = H.edge_key_dict_factory()
        H._succ[u][v][k] = self._succ[u][v][k]
        H._pred[v][u][k] = self._pred[v][u][k]
    H.graph = self.graph
    return H
