#NEWLINE# def gnr_graph( n , p , create_using = None , seed = None ) : #NEWLINE# #INDENT# 'Return the GNR digraph with n nodes and redirection probability p.\n\n    The GNR (growing network with redirection) graph is built by adding nodes \n    one at a time with a link to one previously added node.  The previous \n    target node is chosen uniformly at random.  With probabiliy p the link is \n    instead "redirected" to the successor node of the target.  The graph is \n    always a (directed) tree.\n\n    Parameters\n    ----------\n    n : int\n        The number of nodes for the generated graph.\n    p : float\n        The redirection probability.\n    create_using : graph, optional (default DiGraph)\n        Return graph of this type. The instance will be cleared.\n    seed : hashable object, optional\n        The seed for the random number generator.\n\n    Examples\n    --------\n    >>> D=nx.gnr_graph(10,0.5)  # the GNR graph\n    >>> G=D.to_undirected()  # the undirected version\n\n    References\n    ----------\n    .. [1] P. L. Krapivsky and S. Redner,\n           Organization of Growing Random Networks,\n           Phys. Rev. E, 63, 066123, 2001.\n    ' #NEWLINE# if ( create_using is None ) : #NEWLINE# #INDENT# create_using = nx . DiGraph ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not create_using . is_directed ( ) ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( 'Directed Graph required in create_using' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not ( seed is None ) ) : #NEWLINE# #INDENT# random . seed ( seed ) #NEWLINE# #UNINDENT# G = empty_graph ( 1 , create_using ) #NEWLINE# G . name = ( 'gnr_graph(%s,%s)' % ( n , p ) ) #NEWLINE# if ( n == 1 ) : #NEWLINE# #INDENT# return G #NEWLINE# #UNINDENT# for source in range ( 1 , n ) : #NEWLINE# #INDENT# target = random . randrange ( 0 , source ) #NEWLINE# if ( ( random . random ( ) < p ) and ( target != 0 ) ) : #NEWLINE# #INDENT# target = G . successors ( target ) [ 0 ] #NEWLINE# #UNINDENT# G . add_edge ( source , target ) #NEWLINE# #UNINDENT# return G