#NEWLINE# def intersection( G , H ) : #NEWLINE# #INDENT# 'Return a new graph that contains only the edges that exist in\n    both G and H.\n\n    The node sets of H and G must be the same.\n\n    Parameters\n    ----------\n    G,H : graph\n       A NetworkX graph.  G and H must have the same node sets.\n\n    Returns\n    -------\n    GH : A new graph with the same type as G.\n\n    Notes\n    -----\n    Attributes from the graph, nodes, and edges are not copied to the new\n    graph.  If you want a new graph of the intersection of G and H\n    with the attributes (including edge data) from G use remove_nodes_from()\n    as follows\n\n    >>> G=nx.path_graph(3)\n    >>> H=nx.path_graph(5)\n    >>> R=G.copy()\n    >>> R.remove_nodes_from(n for n in G if n not in H)\n    ' #NEWLINE# R = nx . create_empty_copy ( G ) #NEWLINE# R . name = ( 'Intersection of (%s and %s)' % ( G . name , H . name ) ) #NEWLINE# if ( set ( G ) != set ( H ) ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( 'Node sets of graphs are not equal' ) #NEWLINE# #UNINDENT# if ( G . number_of_edges ( ) <= H . number_of_edges ( ) ) : #NEWLINE# #INDENT# if G . is_multigraph ( ) : #NEWLINE# #INDENT# edges = G . edges_iter ( keys = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# edges = G . edges_iter ( ) #NEWLINE# #UNINDENT# for e in edges : #NEWLINE# #INDENT# if H . has_edge ( * e ) : #NEWLINE# #INDENT# R . add_edge ( * e ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if H . is_multigraph ( ) : #NEWLINE# #INDENT# edges = H . edges_iter ( keys = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# edges = H . edges_iter ( ) #NEWLINE# #UNINDENT# for e in edges : #NEWLINE# #INDENT# if G . has_edge ( * e ) : #NEWLINE# #INDENT# R . add_edge ( * e ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return R