ó
|£*^c           @   s¨   d  Z  d Z d d d d g Z d d l Z d d l Z d d l m Z d d	 l m	 Z	 d d d d
 „ Z d d d „ Z d d d „ Z d d d d d d d d „ Z d S(   sd   
Generators for some directed graphs, including growing network (GN) graphs and
scale-free graphs.

sK   Aric Hagberg (hagberg@lanl.gov)
Willem Ligtenberg (W.P.A.Ligtenberg@tue.nl)t   gn_grapht	   gnc_grapht	   gnr_grapht   scale_free_graphiÿÿÿÿN(   t   empty_graph(   t   discrete_sequencec   
      C   s5  | d k r t j ƒ  } n | j ƒ  s9 t j d ƒ ‚ n  | d k rQ d „  } n  | d k	 rm t j | ƒ n  t d | ƒ } d |  | _ |  d k r™ | S| j	 d d ƒ d d g } xy t
 d |  ƒ D]h } g  | D] } | | ƒ ^ qÒ } t d d | ƒd }	 | j	 | |	 ƒ | j d ƒ | |	 c d 7<qÅ W| S(	   sæ  Return the growing network (GN) digraph with ``n`` nodes.

    The GN graph is built by adding nodes one at a time with a link to one
    previously added node.  The target node for the link is chosen with
    probability based on degree.  The default attachment kernel is a linear
    function of the degree of a node.

    The graph is always a (directed) tree.

    Parameters
    ----------
    n : int
        The number of nodes for the generated graph.
    kernel : function
        The attachment kernel.
    create_using : graph, optional (default DiGraph)
        Return graph of this type. The instance will be cleared.
    seed : hashable object, optional
        The seed for the random number generator.

    Examples
    --------
    To create the undirected GN graph, use the :meth:`~DiGraph.to_directed`
    method::

    >>> D = nx.gn_graph(10)  # the GN graph
    >>> G = D.to_undirected()  # the undirected version

    To specify an attachment kernel, use the ``kernel`` keyword argument::

    >>> D = nx.gn_graph(10, kernel=lambda x: x ** 1.5)  # A_k = k^1.5

    References
    ----------
    .. [1] P. L. Krapivsky and S. Redner,
           Organization of Growing Random Networks,
           Phys. Rev. E, 63, 066123, 2001.
    s'   Directed Graph required in create_usingc         S   s   |  S(   N(    (   t   x(    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyt   <lambda>E   s    i   s   gn_graph(%s)i    i   t   distributionN(   t   Nonet   nxt   DiGrapht   is_directedt   NetworkXErrort   randomt   seedR   t   namet   add_edget   rangeR   t   append(
   t   nt   kernelt   create_usingR   t   Gt   dst   sourcet   dt   distt   target(    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyR       s*    'c         C   sû   | d k r t j ƒ  } n | j ƒ  s9 t j d ƒ ‚ n  | d k	 rU t j | ƒ n  t d | ƒ } d |  | f | _ |  d k r‡ | Sxm t	 d |  ƒ D]\ } t j
 d | ƒ } t j ƒ  | k  rã | d k rã | j | ƒ d } n  | j | | ƒ q— W| S(   s‘  Return the growing network with redirection (GNR) digraph with ``n``
    nodes and redirection probability ``p``.

    The GNR graph is built by adding nodes one at a time with a link to one
    previously added node.  The previous target node is chosen uniformly at
    random.  With probabiliy ``p`` the link is instead "redirected" to the
    successor node of the target.

    The graph is always a (directed) tree.

    Parameters
    ----------
    n : int
        The number of nodes for the generated graph.
    p : float
        The redirection probability.
    create_using : graph, optional (default DiGraph)
        Return graph of this type. The instance will be cleared.
    seed : hashable object, optional
        The seed for the random number generator.

    Examples
    --------
    To create the undirected GNR graph, use the :meth:`~DiGraph.to_directed`
    method::

    >>> D = nx.gnr_graph(10, 0.5)  # the GNR graph
    >>> G = D.to_undirected()  # the undirected version

    References
    ----------
    .. [1] P. L. Krapivsky and S. Redner,
           Organization of Growing Random Networks,
           Phys. Rev. E, 63, 066123, 2001.
    s'   Directed Graph required in create_usingi   s   gnr_graph(%s,%s)i    N(   R	   R
   R   R   R   R   R   R   R   R   t	   randranget
   successorsR   (   R   t   pR   R   R   R   R   (    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyR   ^   s     $c         C   së   | d k r t j ƒ  } n | j ƒ  s9 t j d ƒ ‚ n  | d k	 rU t j | ƒ n  t d | ƒ } d |  | _ |  d k r� | Sxc t	 d |  ƒ D]R } t j
 d | ƒ } x' | j | ƒ D] } | j | | ƒ q¹ W| j | | ƒ q‘ W| S(   sÄ  Return the growing network with copying (GNC) digraph with ``n`` nodes.

    The GNC graph is built by adding nodes one at a time with a link to one
    previously added node (chosen uniformly at random) and to all of that
    node's successors.

    Parameters
    ----------
    n : int
        The number of nodes for the generated graph.
    create_using : graph, optional (default DiGraph)
        Return graph of this type. The instance will be cleared.
    seed : hashable object, optional
        The seed for the random number generator.

    References
    ----------
    .. [1] P. L. Krapivsky and S. Redner,
           Network Growth by Copying,
           Phys. Rev. E, 71, 036118, 2005k.},
    s'   Directed Graph required in create_usingi   s   gnc_graph(%s)i    N(   R	   R
   R   R   R   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   t   succ(    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyR   ™   s     g=
×£p=Ú?gHáz®Gá?gš™™™™™©?gš™™™™™É?i    c         C   sÝ  d „  } | d k r: t j ƒ  }	 |	 j d d d g ƒ n0 | }	 |	 j ƒ  oU |	 j ƒ  sj t j d ƒ ‚ n  | d k r… t d ƒ ‚ n  | d k r  t d ƒ ‚ n  | d k r» t d ƒ ‚ n  | | | d k rÞ t d	 ƒ ‚ n  d
 |  | | | | | f |	 _ t	 j
 | ƒ xÌ t |	 ƒ |  k  rØt	 j	 ƒ  }
 |
 | k  r^t |	 ƒ } | |	 |	 j ƒ  | ƒ } ng |
 | | k  r¡| |	 |	 j ƒ  | ƒ } | |	 |	 j ƒ  | ƒ } n$ | |	 |	 j ƒ  | ƒ } t |	 ƒ } |	 j | | ƒ qW|	 S(   sê  Returns a scale-free directed graph.

    Parameters
    ----------
    n : integer
        Number of nodes in graph
    alpha : float 
        Probability for adding a new node connected to an existing node
        chosen randomly according to the in-degree distribution.
    beta : float
        Probability for adding an edge between two existing nodes.
        One existing node is chosen randomly according the in-degree 
        distribution and the other chosen randomly according to the out-degree 
        distribution.     
    gamma : float
        Probability for adding a new node conecgted to an existing node
        chosen randomly according to the out-degree distribution.
    delta_in : float
        Bias for choosing ndoes from in-degree distribution.
    delta_out : float
        Bias for choosing ndoes from out-degree distribution.
    create_using : graph, optional (default MultiDiGraph)
        Use this graph instance to start the process (default=3-cycle).
    seed : integer, optional
        Seed for random number generator

    Examples
    --------
    Create a scale-free graph on one hundred nodes::

    >>> G = nx.scale_free_graph(100)
  
    Notes
    -----
    The sum of ``alpha``, ``beta``, and ``gamma`` must be 1.

    References
    ----------  
    .. [1] B. BollobÃ¡s, C. Borgs, J. Chayes, and O. Riordan,
           Directed scale-free graphs,
           Proceedings of the fourteenth annual ACM-SIAM Symposium on
           Discrete Algorithms, 132--139, 2003.
    c         S   sˆ   d } t  t | j ƒ  ƒ ƒ t  | ƒ t | ƒ } t j ƒ  } xC t d t | ƒ ƒ D], } | | | | | 7} | | k  rT PqT qT W| S(   Ng        i    (   t   floatt   sumt   valuest   lenR   R   (   R   R   t   deltat   cumsumt   psumt   rt   i(    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyt   _choose_nodeô   s    ,i    i   i   s%   MultiDiGraph required in create_usings   alpha must be >= 0.s   beta must be >= 0.g      ð?s   alpha+beta+gamma must equal 1.sP   directed_scale_free_graph(%s,alpha=%s,beta=%s,gamma=%s,delta_in=%s,delta_out=%s)N(   i    i   (   i   i   (   i   i    (   R	   R
   t   MultiDiGrapht   add_edges_fromR   t   is_multigraphR   t
   ValueErrorR   R   R   R$   t	   in_degreet
   out_degreeR   (   R   t   alphat   betat   gammat   delta_int	   delta_outR   R   R*   R   R(   t   vt   w(    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyR   Æ   s<    .	(   t   __doc__t
   __author__t   __all__R   t   networkxR
   t   networkx.generators.classicR   t   networkx.utilsR   R	   R    R   R   R   (    (    (    sp   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/directed.pyt   <module>   s   F;-