<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> from networkx . utils import is_string_like <EOL> __author__ = """<STR_LIT:\n>""" . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def union ( G , H , rename = ( None , None ) , name = None ) : <EOL> """<STR_LIT>""" <EOL> if not G . is_multigraph ( ) == H . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' ) <EOL> R = G . __class__ ( ) <EOL> if name is None : <EOL> name = "<STR_LIT>" % ( G . name , H . name ) <EOL> R . name = name <EOL> def add_prefix ( graph , prefix ) : <EOL> if prefix is None : <EOL> return graph <EOL> def label ( x ) : <EOL> if is_string_like ( x ) : <EOL> name = prefix + x <EOL> else : <EOL> name = prefix + repr ( x ) <EOL> return name <EOL> return nx . relabel_nodes ( graph , label ) <EOL> G = add_prefix ( G , rename [ <NUM_LIT:0> ] ) <EOL> H = add_prefix ( H , rename [ <NUM_LIT:1> ] ) <EOL> if set ( G ) & set ( H ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if G . is_multigraph ( ) : <EOL> G_edges = G . edges ( keys = True , data = True ) <EOL> else : <EOL> G_edges = G . edges ( data = True ) <EOL> if H . is_multigraph ( ) : <EOL> H_edges = H . edges ( keys = True , data = True ) <EOL> else : <EOL> H_edges = H . edges ( data = True ) <EOL> R . add_nodes_from ( G ) <EOL> R . add_edges_from ( G_edges ) <EOL> R . add_nodes_from ( H ) <EOL> R . add_edges_from ( H_edges ) <EOL> R . node . update ( G . node ) <EOL> R . node . update ( H . node ) <EOL> R . graph . update ( G . graph ) <EOL> R . graph . update ( H . graph ) <EOL> return R <EOL> def disjoint_union ( G , H ) : <EOL> """<STR_LIT>""" <EOL> R1 = nx . convert_node_labels_to_integers ( G ) <EOL> R2 = nx . convert_node_labels_to_integers ( H , first_label = len ( R1 ) ) <EOL> R = union ( R1 , R2 ) <EOL> R . name = "<STR_LIT>" % ( G . name , H . name ) <EOL> R . graph . update ( G . graph ) <EOL> R . graph . update ( H . graph ) <EOL> return R <EOL> def intersection ( G , H ) : <EOL> """<STR_LIT>""" <EOL> R = nx . create_empty_copy ( G ) <EOL> R . name = "<STR_LIT>" % ( G . name , H . name ) <EOL> if not G . is_multigraph ( ) == H . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' ) <EOL> if set ( G ) != set ( H ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if G . number_of_edges ( ) <= H . number_of_edges ( ) : <EOL> if G . is_multigraph ( ) : <EOL> edges = G . edges ( keys = True ) <EOL> else : <EOL> edges = G . edges ( ) <EOL> for e in edges : <EOL> if H . has_edge ( * e ) : <EOL> R . add_edge ( * e ) <EOL> else : <EOL> if H . is_multigraph ( ) : <EOL> edges = H . edges ( keys = True ) <EOL> else : <EOL> edges = H . edges ( ) <EOL> for e in edges : <EOL> if G . has_edge ( * e ) : <EOL> R . add_edge ( * e ) <EOL> return R <EOL> def difference ( G , H ) : <EOL> """<STR_LIT>""" <EOL> if not G . is_multigraph ( ) == H . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' ) <EOL> R = nx . create_empty_copy ( G ) <EOL> R . name = "<STR_LIT>" % ( G . name , H . name ) <EOL> if set ( G ) != set ( H ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if G . is_multigraph ( ) : <EOL> edges = G . edges ( keys = True ) <EOL> else : <EOL> edges = G . edges ( ) <EOL> for e in edges : <EOL> if not H . has_edge ( * e ) : <EOL> R . add_edge ( * e ) <EOL> return R <EOL> def symmetric_difference ( G , H ) : <EOL> """<STR_LIT>""" <EOL> if not G . is_multigraph ( ) == H . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' ) <EOL> R = nx . create_empty_copy ( G ) <EOL> R . name = "<STR_LIT>" % ( G . name , H . name ) <EOL> if set ( G ) != set ( H ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> gnodes = set ( G ) <EOL> hnodes = set ( H ) <EOL> nodes = gnodes . symmetric_difference ( hnodes ) <EOL> R . add_nodes_from ( nodes ) <EOL> if G . is_multigraph ( ) : <EOL> edges = G . edges ( keys = True ) <EOL> else : <EOL> edges = G . edges ( ) <EOL> for e in edges : <EOL> if not H . has_edge ( * e ) : <EOL> R . add_edge ( * e ) <EOL> if H . is_multigraph ( ) : <EOL> edges = H . edges ( keys = True ) <EOL> else : <EOL> edges = H . edges ( ) <EOL> for e in edges : <EOL> if not G . has_edge ( * e ) : <EOL> R . add_edge ( * e ) <EOL> return R <EOL> def compose ( G , H , name = None ) : <EOL> """<STR_LIT>""" <EOL> if not G . is_multigraph ( ) == H . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' ) <EOL> if name is None : <EOL> name = "<STR_LIT>" % ( G . name , H . name ) <EOL> R = G . __class__ ( ) <EOL> R . name = name <EOL> R . add_nodes_from ( G . nodes ( data = True ) ) <EOL> R . add_nodes_from ( H . nodes ( data = True ) ) <EOL> if G . is_multigraph ( ) : <EOL> R . add_edges_from ( G . edges ( keys = True , data = True ) ) <EOL> else : <EOL> R . add_edges_from ( G . edges ( data = True ) ) <EOL> if H . is_multigraph ( ) : <EOL> R . add_edges_from ( H . edges ( keys = True , data = True ) ) <EOL> else : <EOL> R . add_edges_from ( H . edges ( data = True ) ) <EOL> R . graph . update ( G . graph ) <EOL> R . graph . update ( H . graph ) <EOL> return R </s>
