<s> """<STR_LIT>""" <EOL> import random <EOL> import sys <EOL> import networkx as nx <EOL> __author__ = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> import warnings as _warnings <EOL> def create_degree_sequence ( n , sfunction = None , max_tries = <NUM_LIT:50> , ** kwds ) : <EOL> _warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> """<STR_LIT>""" <EOL> tries = <NUM_LIT:0> <EOL> max_deg = n <EOL> while tries < max_tries : <EOL> trialseq = sfunction ( n , ** kwds ) <EOL> seq = [ min ( max_deg , max ( int ( round ( s ) ) , <NUM_LIT:0> ) ) for s in trialseq ] <EOL> if nx . is_valid_degree_sequence ( seq ) : <EOL> return seq <EOL> tries += <NUM_LIT:1> <EOL> raise nx . NetworkXError ( "<STR_LIT>" % max_tries ) <EOL> def pareto_sequence ( n , exponent = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> return [ random . paretovariate ( exponent ) for i in range ( n ) ] <EOL> def powerlaw_sequence ( n , exponent = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return [ random . paretovariate ( exponent - <NUM_LIT:1> ) for i in range ( n ) ] <EOL> def zipf_rv ( alpha , xmin = <NUM_LIT:1> , seed = None ) : <EOL> r"""<STR_LIT>""" <EOL> if xmin < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if alpha <= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not seed is None : <EOL> random . seed ( seed ) <EOL> a1 = alpha - <NUM_LIT:1.0> <EOL> b = <NUM_LIT:2> ** a1 <EOL> while True : <EOL> u = <NUM_LIT:1.0> - random . random ( ) <EOL> v = random . random ( ) <EOL> x = int ( xmin * u ** - ( <NUM_LIT:1.0> / a1 ) ) <EOL> t = ( <NUM_LIT:1.0> + ( <NUM_LIT:1.0> / x ) ) ** a1 <EOL> if v * x * ( t - <NUM_LIT:1.0> ) / ( b - <NUM_LIT:1.0> ) <= t / b : <EOL> break <EOL> return x <EOL> def zipf_sequence ( n , alpha = <NUM_LIT> , xmin = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return [ zipf_rv ( alpha , xmin ) for _ in range ( n ) ] <EOL> def uniform_sequence ( n ) : <EOL> """<STR_LIT>""" <EOL> return [ random . uniform ( <NUM_LIT:0> , n ) for i in range ( n ) ] <EOL> def cumulative_distribution ( distribution ) : <EOL> """<STR_LIT>""" <EOL> cdf = [ <NUM_LIT:0.0> ] <EOL> psum = float ( sum ( distribution ) ) <EOL> for i in range ( <NUM_LIT:0> , len ( distribution ) ) : <EOL> cdf . append ( cdf [ i ] + distribution [ i ] / psum ) <EOL> return cdf <EOL> def discrete_sequence ( n , distribution = None , cdistribution = None ) : <EOL> """<STR_LIT>""" <EOL> import bisect <EOL> if cdistribution is not None : <EOL> cdf = cdistribution <EOL> elif distribution is not None : <EOL> cdf = cumulative_distribution ( distribution ) <EOL> else : <EOL> raise nx . NetworkXError ( <EOL> "<STR_LIT>" ) <EOL> inputseq = [ random . random ( ) for i in range ( n ) ] <EOL> seq = [ bisect . bisect_left ( cdf , s ) - <NUM_LIT:1> for s in inputseq ] <EOL> return seq <EOL> def random_weighted_sample ( mapping , k ) : <EOL> """<STR_LIT>""" <EOL> if k > len ( mapping ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sample = set ( ) <EOL> while len ( sample ) < k : <EOL> sample . add ( weighted_choice ( mapping ) ) <EOL> return list ( sample ) <EOL> def weighted_choice ( mapping ) : <EOL> """<STR_LIT>""" <EOL> rnd = random . random ( ) * sum ( mapping . values ( ) ) <EOL> for k , w in mapping . items ( ) : <EOL> rnd -= w <EOL> if rnd < <NUM_LIT:0> : <EOL> return k </s>
