<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> __author__ = """<STR_LIT:\n>""" . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def color ( G ) : <EOL> """<STR_LIT>""" <EOL> if G . is_directed ( ) : <EOL> import itertools <EOL> def neighbors ( v ) : <EOL> return itertools . chain . from_iterable ( [ G . predecessors ( v ) , <EOL> G . successors ( v ) ] ) <EOL> else : <EOL> neighbors = G . neighbors <EOL> color = { } <EOL> for n in G : <EOL> if n in color or len ( G [ n ] ) == <NUM_LIT:0> : <EOL> continue <EOL> queue = [ n ] <EOL> color [ n ] = <NUM_LIT:1> <EOL> while queue : <EOL> v = queue . pop ( ) <EOL> c = <NUM_LIT:1> - color [ v ] <EOL> for w in neighbors ( v ) : <EOL> if w in color : <EOL> if color [ w ] == color [ v ] : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> else : <EOL> color [ w ] = c <EOL> queue . append ( w ) <EOL> color . update ( dict . fromkeys ( nx . isolates ( G ) , <NUM_LIT:0> ) ) <EOL> return color <EOL> def is_bipartite ( G ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> color ( G ) <EOL> return True <EOL> except nx . NetworkXError : <EOL> return False <EOL> def is_bipartite_node_set ( G , nodes ) : <EOL> """<STR_LIT>""" <EOL> S = set ( nodes ) <EOL> for CC in nx . connected_component_subgraphs ( G ) : <EOL> X , Y = sets ( CC ) <EOL> if not ( ( X . issubset ( S ) and Y . isdisjoint ( S ) ) or <EOL> ( Y . issubset ( S ) and X . isdisjoint ( S ) ) ) : <EOL> return False <EOL> return True <EOL> def sets ( G ) : <EOL> """<STR_LIT>""" <EOL> c = color ( G ) <EOL> X = set ( n for n in c if c [ n ] ) <EOL> Y = set ( n for n in c if not c [ n ] ) <EOL> return ( X , Y ) <EOL> def density ( B , nodes ) : <EOL> """<STR_LIT>""" <EOL> n = len ( B ) <EOL> m = nx . number_of_edges ( B ) <EOL> nb = len ( nodes ) <EOL> nt = n - nb <EOL> if m == <NUM_LIT:0> : <EOL> d = <NUM_LIT:0.0> <EOL> else : <EOL> if B . is_directed ( ) : <EOL> d = m / ( <NUM_LIT> * float ( nb * nt ) ) <EOL> else : <EOL> d = m / float ( nb * nt ) <EOL> return d <EOL> def degrees ( B , nodes , weight = None ) : <EOL> """<STR_LIT>""" <EOL> bottom = set ( nodes ) <EOL> top = set ( B ) - bottom <EOL> return ( B . degree ( top , weight ) , B . degree ( bottom , weight ) ) </s>
