<s> """<STR_LIT>""" <EOL> from itertools import combinations <EOL> import random <EOL> import networkx as nx <EOL> from networkx . algorithms . simple_paths import is_simple_path as is_path <EOL> from networkx . utils import arbitrary_element <EOL> from networkx . utils import not_implemented_for <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def index_satisfying ( iterable , condition ) : <EOL> """<STR_LIT>""" <EOL> for i , x in enumerate ( iterable ) : <EOL> if condition ( x ) : <EOL> return i <EOL> try : <EOL> return i + <NUM_LIT:1> <EOL> except NameError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def is_tournament ( G ) : <EOL> """<STR_LIT>""" <EOL> return ( all ( ( v in G [ u ] ) ^ ( u in G [ v ] ) for u , v in combinations ( G , <NUM_LIT:2> ) ) and <EOL> G . number_of_selfloops ( ) == <NUM_LIT:0> ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def hamiltonian_path ( G ) : <EOL> """<STR_LIT>""" <EOL> if len ( G ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> if len ( G ) == <NUM_LIT:1> : <EOL> return [ arbitrary_element ( G ) ] <EOL> v = arbitrary_element ( G ) <EOL> hampath = hamiltonian_path ( G . subgraph ( set ( G ) - { v } ) ) <EOL> index = index_satisfying ( hampath , lambda u : v not in G [ u ] ) <EOL> hampath . insert ( index , v ) <EOL> return hampath <EOL> def random_tournament ( n ) : <EOL> r"""<STR_LIT>""" <EOL> coins = ( random . random ( ) for i in range ( ( n * ( n - <NUM_LIT:1> ) ) // <NUM_LIT:2> ) ) <EOL> pairs = combinations ( range ( n ) , <NUM_LIT:2> ) <EOL> edges = ( ( u , v ) if r < <NUM_LIT:0.5> else ( v , u ) for ( u , v ) , r in zip ( pairs , coins ) ) <EOL> return nx . DiGraph ( edges ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def score_sequence ( G ) : <EOL> """<STR_LIT>""" <EOL> return sorted ( d for v , d in G . out_degree ( ) ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def tournament_matrix ( G ) : <EOL> r"""<STR_LIT>""" <EOL> A = nx . adjacency_matrix ( G ) <EOL> return A - A . T <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def is_reachable ( G , s , t ) : <EOL> """<STR_LIT>""" <EOL> def two_neighborhood ( G , v ) : <EOL> """<STR_LIT>""" <EOL> return { x for x in G <EOL> if x == v or x in G [ v ] or <EOL> any ( is_path ( G , [ v , z , x ] ) for z in G ) } <EOL> def is_closed ( G , nodes ) : <EOL> """<STR_LIT>""" <EOL> return all ( v in G [ u ] for u in set ( G ) - nodes for v in nodes ) <EOL> neighborhoods = [ two_neighborhood ( G , v ) for v in G ] <EOL> return all ( not ( is_closed ( G , S ) and s in S and t not in S ) <EOL> for S in neighborhoods ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def is_strongly_connected ( G ) : <EOL> """<STR_LIT>""" <EOL> return all ( is_reachable ( G , u , v ) for u in G for v in G ) </s>
