ó
|£*^c           @   sÃ   d  Z  d d l m Z d d l Z d d l m Z d j d d d d	 g ƒ Z d
 d d d d g Z d d „ Z
 d d „ Z d d d „ Z d d e d „ Z d d d „ Z d „  Z d d „ Z d S(   s>   Algorithms to characterize the number of triangles in a graph.iÿÿÿÿ(   t   combinationsN(   t   NetworkXErrors   
s%   Aric Hagberg <aric.hagberg@gmail.com>s    Dan Schult (dschult@colgate.edu)s   Pieter Swart (swart@lanl.gov)s%   Jordi Torrents <jtorrents@milnou.net>t	   trianglest   average_clusteringt
   clusteringt   transitivityt   square_clusteringc         C   s_   |  j  ƒ  r t d ƒ ‚ n  | |  k rB t t |  | ƒ ƒ d d St d „  t |  | ƒ Dƒ ƒ S(   s  Compute the number of triangles.

    Finds the number of triangles that include a node as one vertex.

    Parameters
    ----------
    G : graph
       A networkx graph
    nodes : container of nodes, optional (default= all nodes in G)
       Compute triangles for nodes in this container. 

    Returns
    -------
    out : dictionary
       Number of triangles keyed by node label.
    
    Examples
    --------
    >>> G=nx.complete_graph(5)
    >>> print(nx.triangles(G,0))
    6
    >>> print(nx.triangles(G))
    {0: 6, 1: 6, 2: 6, 3: 6, 4: 6}
    >>> print(list(nx.triangles(G,(0,1)).values()))
    [6, 6]

    Notes
    -----
    When computing triangles for the entire graph each triangle is counted 
    three times, once at each node.  Self loops are ignored.

    s/   triangles() is not defined for directed graphs.i   c         s   s(   |  ] \ } } } | | d  f Vq d S(   i   N(    (   t   .0t   vt   dt   t(    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pys	   <genexpr>9   s    (   t   is_directedR   t   nextt   _triangles_and_degree_itert   dict(   t   Gt   nodes(    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR      s
    !c   	      #   sí   ˆ  j  ƒ  r t d ƒ ‚ n  | d k r9 ˆ  j j ƒ  } n ‡  f d †  ˆ  j | ƒ Dƒ } xŽ | D]† \ } } t | ƒ t | g ƒ } d } xD | D]< } t ˆ  | ƒ t | g ƒ } | t | j | ƒ ƒ 7} q‘ W| t | ƒ | f Vq_ Wd S(   s¹    Return an iterator of (node, degree, triangles).  

    This double counts triangles so you may want to divide by 2.
    See degree() and triangles() for definitions and details.

    s   Not defined for multigraphs.c         3   s   |  ] } | ˆ  | f Vq d  S(   N(    (   R   t   n(   R   (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pys	   <genexpr>H   s    i    N(	   t   is_multigraphR   t   Nonet   adjt   itemst   nbunch_itert   sett   lent   intersection(	   R   R   t
   nodes_nbrsR   t   v_nbrst   vst
   ntrianglest   wt   ws(    (   R   so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR   ;   s    t   weightc         #   sÃ  ˆ  j  ƒ  r t d ƒ ‚ n  ˆ d	 k s9 ˆ  j ƒ  g  k rB d } n. t t ‡ f d †  ˆ  j d t ƒ Dƒ ƒ ƒ } | d	 k rŽ ˆ  j j ƒ  } n ‡  f d †  ˆ  j	 | ƒ Dƒ } x| D]\ } } t
 | ƒ t
 | g ƒ } d } t
 ƒ  }	 x¸ | D]° }
 ˆ  | |
 j ˆ d ƒ | } |	 j |
 ƒ t
 ˆ  |
 ƒ |	 } xh | | @D]\ } ˆ  |
 | j ˆ d ƒ | } ˆ  | | j ˆ d ƒ | } | | | | d d 7} q?Wqï W| t | ƒ | d f Vq´ Wd	 S(
   si    Return an iterator of (node, degree, weighted_triangles).  
    
    Used for weighted clustering.

    s   Not defined for multigraphs.g      ð?c         3   s*   |  ]  \ } } } | j  ˆ  d  ƒ Vq d S(   g      ð?N(   t   get(   R   t   uR   R	   (   R    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pys	   <genexpr>_   s   t   datac         3   s   |  ] } | ˆ  | f Vq d  S(   N(    (   R   R   (   R   (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pys	   <genexpr>d   s    g        g      @i   N(   R   R   R   t   edgest   floatt   maxt   TrueR   R   R   R   R!   t   addR   (   R   R   R    t
   max_weightR   t   it   nbrst   inbrst   weighted_trianglest   seent   jt   wijt   jnbrst   kt   wjkt   wki(    (   R   R    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyt#   _weighted_triangles_and_degree_iterS   s,    		"c         C   sc   t  |  | d | ƒj ƒ  } | sI g  | D] } | d k r( | ^ q( } n  t | ƒ t t | ƒ ƒ S(   sø  Compute the average clustering coefficient for the graph G.

    The clustering coefficient for the graph is the average, 

    .. math::

       C = \frac{1}{n}\sum_{v \in G} c_v,
       
    where `n` is the number of nodes in `G`.

    Parameters
    ----------
    G : graph

    nodes : container of nodes, optional (default=all nodes in G)
       Compute average clustering for nodes in this container. 

    weight : string or None, optional (default=None)
       The edge attribute that holds the numerical value used as a weight.
       If None, then each edge has weight 1.

    count_zeros : bool
       If False include only the nodes with nonzero clustering in the average.

    Returns
    -------
    avg : float
       Average clustering
    
    Examples
    --------
    >>> G=nx.complete_graph(5)
    >>> print(nx.average_clustering(G))
    1.0

    Notes
    -----
    This is a space saving routine; it might be faster
    to use the clustering function to get a list and then take the average.

    Self loops are ignored.

    References
    ----------
    .. [1] Generalizations of the clustering coefficient to weighted 
       complex networks by J. SaramÃ¤ki, M. KivelÃ¤, J.-P. Onnela, 
       K. Kaski, and J. KertÃ©sz, Physical Review E, 75 027105 (2007).  
       http://jponnela.com/web_documents/a9.pdf
    .. [2] Marcus Kaiser,  Mean clustering coefficients: the role of isolated 
       nodes and leafs on clustering measures for small-world networks.
       http://arxiv.org/abs/0802.2512
    R    i    (   R   t   valuest   sumR%   R   (   R   R   R    t   count_zerost   cR   (    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR   u   s    5(c         C   sÇ   |  j  ƒ  r t d d ƒ ‚ n  | d k	 r? t |  | | ƒ } n t |  | ƒ } i  } xL | D]D \ } } } | d k rƒ d | | <q[ | t | | d ƒ | | <q[ W| |  k rÃ t | j ƒ  ƒ d S| S(   sÉ  Compute the clustering coefficient for nodes.

    For unweighted graphs, the clustering of a node `u`
    is the fraction of possible triangles through that node that exist,

    .. math::

      c_u = \frac{2 T(u)}{deg(u)(deg(u)-1)},

    where `T(u)` is the number of triangles through node `u` and
    `deg(u)` is the degree of `u`.

    For weighted graphs, the clustering is defined
    as the geometric average of the subgraph edge weights [1]_,

    .. math::

       c_u = \frac{1}{deg(u)(deg(u)-1))}
            \sum_{uv} (\hat{w}_{uv} \hat{w}_{uw} \hat{w}_{vw})^{1/3}.
      
    The edge weights `\hat{w}_{uv}` are normalized by the maximum weight in the
    network `\hat{w}_{uv} = w_{uv}/\max(w)`.

    The value of `c_u` is assigned to 0 if `deg(u) < 2`.

    Parameters
    ----------
    G : graph

    nodes : container of nodes, optional (default=all nodes in G)
       Compute clustering for nodes in this container. 

    weight : string or None, optional (default=None)
       The edge attribute that holds the numerical value used as a weight.
       If None, then each edge has weight 1.

    Returns
    -------
    out : float, or dictionary
       Clustering coefficient at specified nodes

    Examples
    --------
    >>> G=nx.complete_graph(5)
    >>> print(nx.clustering(G,0))
    1.0
    >>> print(nx.clustering(G))
    {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}

    Notes
    -----
    Self loops are ignored.

    References
    ----------
    .. [1] Generalizations of the clustering coefficient to weighted 
       complex networks by J. SaramÃ¤ki, M. KivelÃ¤, J.-P. Onnela, 
       K. Kaski, and J. KertÃ©sz, Physical Review E, 75 027105 (2007).  
       http://jponnela.com/web_documents/a9.pdf
    s&   Clustering algorithms are not defined s   for directed graphs.i    g        i   N(   R   R   R   R5   R   R%   t   listR6   (   R   R   R    t   td_itert   clustercR   R	   R
   (    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR   ¯   s    = c         C   sj   d } d } x9 t  |  ƒ D]+ \ } } } | | | d 7} | | 7} q W| d k rX d S| t | ƒ Sd S(   sæ  Compute graph transitivity, the fraction of all possible triangles 
    present in G.

    Possible triangles are identified by the number of "triads" 
    (two edges with a shared vertex).

    The transitivity is

    .. math::

        T = 3\frac{\#triangles}{\#triads}.

    Parameters
    ----------
    G : graph

    Returns
    -------
    out : float
       Transitivity

    Examples
    --------
    >>> G = nx.complete_graph(5)
    >>> print(nx.transitivity(G))
    1.0
    i    i   g        N(   R   R%   (   R   R   t   contriR   R	   R
   (    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR      s    c   
      C   sH  | d k r |  } n |  j | ƒ } i  } x÷ | D]ï } d | | <d } x· t |  | d ƒ D]¢ \ } } t t |  | ƒ t |  | ƒ @t | g ƒ ƒ } | | c | 7<| d }	 | |  | k rÏ |	 d 7}	 n  | t |  | ƒ |	 t |  | ƒ |	 | 7} q[ W| d k r1 | | c | :<q1 q1 W| |  k rDt | j ƒ  ƒ d S| S(   sF   Compute the squares clustering coefficient for nodes.

    For each node return the fraction of possible squares that exist at
    the node [1]_

    .. math::
       C_4(v) = \frac{ \sum_{u=1}^{k_v} 
       \sum_{w=u+1}^{k_v} q_v(u,w) }{ \sum_{u=1}^{k_v} 
       \sum_{w=u+1}^{k_v} [a_v(u,w) + q_v(u,w)]},
    
    where `q_v(u,w)` are the number of common neighbors of `u` and `w` 
    other than `v` (ie squares), and 
    `a_v(u,w) = (k_u - (1+q_v(u,w)+\theta_{uv}))(k_w - (1+q_v(u,w)+\theta_{uw}))`,
    where `\theta_{uw} = 1` if `u` and `w` are connected and 0 otherwise.

    Parameters
    ----------
    G : graph

    nodes : container of nodes, optional (default=all nodes in G)
       Compute clustering for nodes in this container. 
        
    Returns
    -------
    c4 : dictionary
       A dictionary keyed by node with the square clustering coefficient value. 

    Examples
    --------
    >>> G=nx.complete_graph(5)
    >>> print(nx.square_clustering(G,0))
    1.0
    >>> print(nx.square_clustering(G))
    {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}

    Notes
    -----
    While `C_3(v)` (triangle clustering) gives the probability that
    two neighbors of node v are connected with each other, `C_4(v)` is
    the probability that two neighbors of node v share a common
    neighbor different from v. This algorithm can be applied to both
    bipartite and unipartite networks.
 
    References
    ----------
    .. [1] Pedro G. Lind, Marta C. GonzÃ¡lez, and Hans J. Herrmann. 2005
        Cycles and clustering in bipartite networks.
        Physical Review E (72) 056127.
    g        i    i   g      ð?i   N(   R   R   R    R   R   R:   R6   (
   R   R   t	   node_iterR   R   t	   potentialR"   R   t   squarest   degm(    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyR   &  s&    2	
 1
2(   t   __doc__t	   itertoolsR    t   networkxt   nxR   t   joint
   __author__t   __all__R   R   R   R5   R'   R   R   R   R   (    (    (    so   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/cluster.pyt   <module>   s    		(":Q	&