<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> from networkx . exception import NetworkXError <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class AntiGraph ( nx . Graph ) : <EOL> """<STR_LIT>""" <EOL> all_edge_dict = { '<STR_LIT>' : <NUM_LIT:1> } <EOL> def single_edge_dict ( self ) : <EOL> return self . all_edge_dict <EOL> edge_attr_dict_factory = single_edge_dict <EOL> def __getitem__ ( self , n ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( node , self . all_edge_dict ) for node in <EOL> set ( self . adj ) - set ( self . adj [ n ] ) - set ( [ n ] ) ) <EOL> def neighbors ( self , n ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return iter ( set ( self . adj ) - set ( self . adj [ n ] ) - set ( [ n ] ) ) <EOL> except KeyError : <EOL> raise NetworkXError ( "<STR_LIT>" % ( n , ) ) <EOL> def degree ( self , nbunch = None , weight = None ) : <EOL> """<STR_LIT>""" <EOL> if nbunch is None : <EOL> nodes_nbrs = ( ( n , { v : self . all_edge_dict for v in <EOL> set ( self . adj ) - set ( self . adj [ n ] ) - set ( [ n ] ) } ) <EOL> for n in self . nodes ( ) ) <EOL> else : <EOL> nodes_nbrs = ( ( n , { v : self . all_edge_dict for v in <EOL> set ( self . nodes ( ) ) - set ( self . adj [ n ] ) - set ( [ n ] ) } ) <EOL> for n in self . nbunch_iter ( nbunch ) ) <EOL> if weight is None : <EOL> for n , nbrs in nodes_nbrs : <EOL> yield ( n , len ( nbrs ) + ( n in nbrs ) ) <EOL> else : <EOL> for n , nbrs in nodes_nbrs : <EOL> yield ( n , sum ( ( nbrs [ nbr ] . get ( weight , <NUM_LIT:1> ) for nbr in nbrs ) ) + <EOL> ( n in nbrs and nbrs [ n ] . get ( weight , <NUM_LIT:1> ) ) ) <EOL> def adjacency_iter ( self ) : <EOL> """<STR_LIT>""" <EOL> for n in self . adj : <EOL> yield ( n , set ( self . adj ) - set ( self . adj [ n ] ) - set ( [ n ] ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> Gnp = nx . gnp_random_graph ( <NUM_LIT:20> , <NUM_LIT> ) <EOL> Anp = AntiGraph ( nx . complement ( Gnp ) ) <EOL> Gd = nx . davis_southern_women_graph ( ) <EOL> Ad = AntiGraph ( nx . complement ( Gd ) ) <EOL> Gk = nx . karate_club_graph ( ) <EOL> Ak = AntiGraph ( nx . complement ( Gk ) ) <EOL> pairs = [ ( Gnp , Anp ) , ( Gd , Ad ) , ( Gk , Ak ) ] <EOL> for G , A in pairs : <EOL> gc = [ set ( c ) for c in nx . connected_components ( G ) ] <EOL> ac = [ set ( c ) for c in nx . connected_components ( A ) ] <EOL> for comp in ac : <EOL> assert comp in gc <EOL> for G , A in pairs : <EOL> gc = [ set ( c ) for c in nx . biconnected_components ( G ) ] <EOL> ac = [ set ( c ) for c in nx . biconnected_components ( A ) ] <EOL> for comp in ac : <EOL> assert comp in gc <EOL> for G , A in pairs : <EOL> node = list ( G . nodes ( ) ) [ <NUM_LIT:0> ] <EOL> nodes = list ( G . nodes ( ) ) [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> assert G . degree ( node ) == A . degree ( node ) <EOL> assert sum ( d for n , d in G . degree ( ) ) == sum ( d for n , d in A . degree ( ) ) <EOL> assert sum ( d for n , d in A . degree ( ) ) == sum ( d for n , d in A . degree ( weight = '<STR_LIT>' ) ) <EOL> assert sum ( d for n , d in G . degree ( nodes ) ) == sum ( d for n , d in A . degree ( nodes ) ) </s>
