ó
|£*^c           @   s²   d  Z  d d l Z d d l Z d d l Z d d l Z d j d d g ƒ Z d d d d	 d
 d g Z d „  Z	 d „  Z
 d d „ Z d e d „ Z d e d „ Z e d d „ Z d S(   sB   Generators for classes of graphs used in studying social networks.iÿÿÿÿNs   
s!   Ben Edwards (bedwards@cs.unm.edu)s   Aric Hagberg (hagberg@lanl.gov)t   caveman_grapht   connected_caveman_grapht   relaxed_caveman_grapht   random_partition_grapht   planted_partition_grapht   gaussian_random_partition_graphc         C   sŠ   t  j |  | ƒ } d |  | | f | _ | d k r† xM t d |  | | ƒ D]2 } t j t | | | ƒ d ƒ } | j | ƒ qM Wn  | S(   s<  Returns a caveman graph of ``l`` cliques of size ``k``.

    Parameters
    ----------
    l : int
      Number of cliques
    k : int
      Size of cliques

    Returns
    -------
    G : NetworkX Graph
      caveman graph

    Notes
    -----
    This returns an undirected graph, it can be converted to a directed
    graph using :func:`nx.to_directed`, or a multigraph using
    ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
    described in [1]_ and it is unclear which of the directed
    generalizations is most useful.

    Examples
    --------
    >>> G = nx.caveman_graph(3, 3)

    See also
    --------

    connected_caveman_graph

    References
    ----------
    .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
       Amer. J. Soc. 105, 493-527, 1999.
    s   caveman_graph(%s,%s)i   i    i   (   t   nxt   empty_grapht   namet   ranget	   itertoolst   combinationst   add_edges_from(   t   lt   kt   Gt   startt   edges(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR       s    &c         C   sz   t  j |  | ƒ } d |  | f | _ xN t d |  | | ƒ D]6 } | j | | d ƒ | j | | d |  | ƒ q< W| S(   sÌ  Returns a connected caveman graph of ``l`` cliques of size ``k``.

    The connected caveman graph is formed by creating ``n`` cliques of size
    ``k``, then a single edge in each clique is rewired to a node in an
    adjacent clique.

    Parameters
    ----------
    l : int
      number of cliques
    k : int
      size of cliques

    Returns
    -------
    G : NetworkX Graph
      connected caveman graph

    Notes
    -----
    This returns an undirected graph, it can be converted to a directed
    graph using :func:`nx.to_directed`, or a multigraph using
    ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
    described in [1]_ and it is unclear which of the directed
    generalizations is most useful.

    Examples
    --------
    >>> G = nx.connected_caveman_graph(3, 3)

    References
    ----------
    .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
       Amer. J. Soc. 105, 493-527, 1999.
    s   connected_caveman_graph(%s,%s)i    i   (   R   R    R   R	   t   remove_edget   add_edge(   R   R   R   R   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR   A   s    $ c   	      C   sÍ   | d k	 r t j | ƒ n  t j |  | ƒ } | j ƒ  } d |  | | f | _ xv | j ƒ  D]h \ } } t j ƒ  | k  r] t j | ƒ } | j	 | | ƒ r¢ q] n  | j
 | | ƒ | j | | ƒ q] q] W| S(   sX  Return a relaxed caveman graph.

    A relaxed caveman graph starts with ``l`` cliques of size ``k``.  Edges are
    then randomly rewired with probability ``p`` to link different cliques.

    Parameters
    ----------
    l : int
      Number of groups
    k : int
      Size of cliques
    p : float
      Probabilty of rewiring each edge.
    seed : int,optional
      Seed for random number generator(default=None)

    Returns
    -------
    G : NetworkX Graph
      Relaxed Caveman Graph

    Raises
    ------
    NetworkXError:
     If p is not in [0,1]

    Examples
    --------
    >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42)

    References
    ----------
    .. [1] Santo Fortunato, Community Detection in Graphs,
       Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174.
       http://arxiv.org/abs/0906.0612
    s    relaxed_caveman_graph (%s,%s,%s)N(   t   Nonet   randomt   seedR   R    t   nodesR   R   t   choicet   has_edgeR   R   (	   R   R   t   pR   R   R   t   ut   vt   x(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR   m   s    %c            sŠ  | d
 k	 r t j | ƒ n  d | k o3 d k n sJ t j d ƒ ‚ n  d | k oa d k n sx t j d ƒ ‚ n  | r� t j ƒ  } n t j ƒ  } g  | j d <t |  ƒ } | j	 t
 | ƒ ƒ i  } d ‰  d } x® |  D]¦ } ‡  f d †  t j | | d | ƒj ƒ  Dƒ }	 | j |	 ƒ | j t j t
 ˆ  ˆ  | ƒ ˆ  | ƒ ƒ | j d j t t
 ˆ  ˆ  | ƒ ƒ ƒ | d	 7} ˆ  | 7‰  qÞ W| d k r˜| S| d	 k r!xv | D]n } t
 | | t | ƒ ƒ }
 | j t | g t |
 ƒ |
 ƒ ƒ | r«| j t |
 | g t |
 ƒ ƒ ƒ q«q«W| St j d | ƒ } t | ƒ } | røx=t
 | ƒ D]ž } d } x� | | k  rðt j d t j ƒ  ƒ } | t | | ƒ 7} | j | | ƒ | | k rÄ| | } n  | | k  rb| j | | ƒ | d	 7} qbqbWqSWnŽ x‹ t
 | d	 ƒ D]y } | | } xf | | k  r�t j d t j ƒ  ƒ } | t | | ƒ 7} | | k  r| j | | ƒ | d	 7} qqWq	W| S(   s©  Return the random partition graph with a partition of sizes.

    A partition graph is a graph of communities with sizes defined by
    s in sizes. Nodes in the same group are connected with probability
    p_in and nodes of different groups are connected with probability
    p_out.

    Parameters
    ----------
    sizes : list of ints
      Sizes of groups
    p_in : float
      probability of edges with in groups
    p_out : float
      probability of edges between groups
    directed : boolean optional, default=False
      Whether to create a directed graph
    seed : int optional, default None
      A seed for the random number generator

    Returns
    -------
    G : NetworkX Graph or DiGraph
      random partition graph of size sum(gs)

    Raises
    ------
    NetworkXError
      If p_in or p_out is not in [0,1]

    Examples
    --------
    >>> G = nx.random_partition_graph([10,10,10],.25,.01)
    >>> len(G)
    30
    >>> partition = G.graph['partition']
    >>> len(partition)
    3

    Notes
    -----
    This is a generalization of the planted-l-partition described in
    [1]_.  It allows for the creation of groups of any size.

    The partition is store as a graph attribute 'partition'.

    References
    ----------
    .. [1] Santo Fortunato 'Community Detection in Graphs' Physical Reports
       Volume 486, Issue 3-5 p. 75-174. http://arxiv.org/abs/0906.0612
       http://arxiv.org/abs/0906.0612
       g        g      ð?s   p_in must be in [0,1]s   p_out must be in [0,1]t	   partitioni    c         3   s)   |  ] \ } } | ˆ  | ˆ  f Vq d  S(   N(    (   t   .0R   R   (   R   (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pys	   <genexpr>í   s   t   directedi   N(   R   R   R   R   t   NetworkXErrort   DiGrapht   Grapht   grapht   sumt   add_nodes_fromR	   t   fast_gnp_random_graphR   R   t   updatet   dictt   fromkeyst   appendt   sett   lent   zipt   matht   logt   intt   getR   (   t   sizest   p_int   p_outR   R    R   t   nt
   next_groupt   groupR   t   targetst   lpR   R   t   lr(    (   R   sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR   ¡   sl    7"*'
#*
c         C   s   t  | g |  | | | | ƒ S(   sE  Return the planted l-partition graph.

    This model partitions a graph with n=l*k vertices in
    l groups with k vertices each. Vertices of the same
    group are linked with a probability p_in, and vertices
    of different groups are linked with probability p_out.

    Parameters
    ----------
    l : int
      Number of groups
    k : int
      Number of vertices in each group
    p_in : float
      probability of connecting vertices within a group
    p_out : float
      probability of connected vertices between groups
    seed : int,optional
      Seed for random number generator(default=None)
    directed : bool,optional (default=False)
      If True return a directed graph

    Returns
    -------
    G : NetworkX Graph or DiGraph
      planted l-partition graph

    Raises
    ------
    NetworkXError:
      If p_in,p_out are not in [0,1] or

    Examples
    --------
    >>> G = nx.planted_partition_graph(4, 3, 0.5, 0.1,seed=42)

    See Also
    --------
    random_partition_model

    References
    ----------
    .. [1] A. Condon, R.M. Karp, Algorithms for graph partitioning
        on the planted partition model,
        Random Struct. Algor. 18 (2001) 116-140.

    .. [2] Santo Fortunato 'Community Detection in Graphs' Physical Reports
       Volume 486, Issue 3-5 p. 75-174. http://arxiv.org/abs/0906.0612
    (   R   (   R   R   R4   R5   R   R    (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR     s    2c   
      C   sÁ   | |  k r t  j d ƒ ‚ n  d } g  } x~ t rª t t j | t | ƒ | d ƒ ƒ }	 |	 d k  rk q- n  | |	 |  k r� | j |  | ƒ Pn  | |	 7} | j |	 ƒ q- Wt | | | | | ƒ S(   s'  Generate a Gaussian random partition graph.

    A Gaussian random partition graph is created by creating k partitions
    each with a size drawn from a normal distribution with mean s and variance
    s/v. Nodes are connected within clusters with probability p_in and
    between clusters with probability p_out[1]

    Parameters
    ----------
    n : int
      Number of nodes in the graph
    s : float
      Mean cluster size
    v : float
      Shape parameter. The variance of cluster size distribution is s/v.
    p_in : float
      Probabilty of intra cluster connection.
    p_out : float
      Probability of inter cluster connection.
    directed : boolean, optional default=False
      Whether to create a directed graph or not
    seed : int
      Seed value for random number generator

    Returns
    -------
    G : NetworkX Graph or DiGraph
      gaussian random partition graph

    Raises
    ------
    NetworkXError
      If s is > n
      If p_in or p_out is not in [0,1]

    Notes
    -----
    Note the number of partitions is dependent on s,v and n, and that the
    last partition may be considerably smaller, as it is sized to simply
    fill out the nodes [1]

    See Also
    --------
    random_partition_graph

    Examples
    --------
    >>> G = nx.gaussian_random_partition_graph(100,10,10,.25,.1)
    >>> len(G)
    100

    References
    ----------
    .. [1] Ulrik Brandes, Marco Gaertler, Dorothea Wagner,
       Experiments on Graph Clustering Algorithms,
       In the proceedings of the 11th Europ. Symp. Algorithms, 2003.
    s   s must be <= ni    g      à?i   (	   R   R!   t   TrueR1   R   t   normalvariatet   floatR+   R   (
   R6   t   sR   R4   R5   R    R   t   assignedR3   t   size(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyR   P  s    ;	&
(   t   __doc__R
   R/   R   t   networkxR   t   joint
   __author__t   __all__R    R   R   R   t   FalseR   R   R   (    (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/community.pyt   <module>   s    		/	,4z5