ó
ú£Õ\c           @   s…   d  d l  Z d j d d g ƒ Z d d g Z d „  Z d „  Z d	 „  Z i e d
 6e d 6e d 6Z d d
 d „ Z
 d d
 d „ Z d S(   iÿÿÿÿNs   
s%   Jordi Torrents <jtorrents@milnou.net>s   Aric Hagberg (hagberg@lanl.gov)t
   clusteringt   average_clusteringc         C   s"   t  t |  | @ƒ ƒ t |  | Bƒ S(   N(   t   floatt   len(   t   nut   nv(    (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyt   cc_dot   s    c         C   s-   t  t |  | @ƒ ƒ t t |  ƒ t | ƒ ƒ S(   N(   R   R   t   max(   R   R   (    (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyt   cc_max   s    c         C   s-   t  t |  | @ƒ ƒ t t |  ƒ t | ƒ ƒ S(   N(   R   R   t   min(   R   R   (    (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyt   cc_min   s    t   dotR	   R   c   
      C   s0  t  j j j |  ƒ s' t  j d ƒ ‚ n  y t | } Wn  t k
 rW t  j d ƒ ‚ n X| d k rm |  } n  i  } x¶ | D]® } d } t g  |  | D] } |  | D] } | ^ q¢ q” ƒ t | g ƒ }	 x5 |	 D]- } | | t |  | ƒ t |  | ƒ ƒ 7} qÎ W| d k r| t	 |	 ƒ :} n  | | | <qz W| S(   sµ  Compute a bipartite clustering coefficient for nodes.

    The bipartie clustering coefficient is a measure of local density
    of connections defined as [1]_
    
    .. math::

       c_u = \frac{\sum_{v \in N(N(v))} c_{uv} }{|N(N(u))|}

    where `N(N(u))` are the second order neighbors of `u` in `G` excluding `u`, 
    and `c_{uv}` is the pairwise clustering coefficient between nodes 
    `u` and `v`.

    The mode selects the function for `c_{uv}`
    'dot': 

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{|N(u) \cup N(v)|}

    'min': 

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{min(|N(u)|,|N(v)|)}

    'max': 

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{max(|N(u)|,|N(v)|)}


    Parameters
    ----------
    G : graph
        A bipartite graph

    nodes : list or iterable (optional)
        Compute bipartite clustering for these nodes. The default 
        is all nodes in G.

    mode : string
        The pariwise bipartite clustering method to be used in the computation.
        It must be "dot", "max", or "min". 
    
    Returns
    -------
    clustering : dictionary
        A dictionary keyed by node with the clustering coefficient value.


    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G=nx.path_graph(4) # path is bipartite
    >>> c=bipartite.clustering(G) 
    >>> c[0]
    0.5
    >>> c=bipartite.clustering(G,mode='min') 
    >>> c[0]
    1.0

    See Also
    --------
    average_clustering
    
    References
    ----------
    .. [1] Latapy, Matthieu, ClÃ©mence Magnien, and Nathalie Del Vecchio (2008).
       Basic notions for the analysis of large two-mode networks. 
       Social Networks 30(1), 31--48.
    s   Graph is not bipartites6   Mode for bipartite clustering must be: dot, min or maxg        N(
   t   nxt
   algorithmst	   bipartitet   is_bipartitet   NetworkXErrort   modest   KeyErrort   Nonet   setR   (
   t   Gt   nodest   modet   cc_funct   ccst   vt   cct   nbrt   ut   nbrs2(    (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyR       s&    J	A+c            sW   | d k r |  } n  t |  d | d | ƒ‰  t t ‡  f d †  | Dƒ ƒ ƒ t | ƒ S(   s  Compute the average bipartite clustering coefficient.

    A clustering coefficient for the whole graph is the average, 

    .. math::

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

    Similar measures for the two bipartite sets can be defined [1]_
    
    .. math::

       C_X = \frac{1}{|X|}\sum_{v \in X} c_v,
       
    where `X` is a bipartite set of `G`.

    Parameters
    ----------
    G : graph
        A bipartite graph

    nodes : list or iterable, optional
        A container of nodes to use in computing the average.  
        The nodes should be either the entire graph (the default) or one of the 
        bipartite sets.

    mode : string
        The pariwise bipartite clustering method. 
        It must be "dot", "max", or "min" 
    
    Returns
    -------
    clustering : float
       The average bipartite clustering for the given set of nodes or the 
       entire graph if no nodes are specified.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G=nx.star_graph(3) # path is bipartite
    >>> bipartite.average_clustering(G) 
    0.75
    >>> X,Y=bipartite.sets(G)
    >>> bipartite.average_clustering(G,X) 
    0.0
    >>> bipartite.average_clustering(G,Y) 
    1.0

    See Also
    --------
    clustering
   
    Notes    
    -----
    The container of nodes passed to this function must contain all of the nodes
    in one of the bipartite sets ("top" or "bottom") in order to compute 
    the correct average bipartite clustering coefficients.

    References
    ----------
    .. [1] Latapy, Matthieu, ClÃ©mence Magnien, and Nathalie Del Vecchio (2008).
        Basic notions for the analysis of large two-mode networks. 
        Social Networks 30(1), 31--48.
    R   R   c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   t   .0R   (   R   (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pys	   <genexpr>À   s    N(   R   R    R   t   sumR   (   R   R   R   (    (   R   s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyR   z   s    C	(   t   networkxR   t   joint
   __author__t   __all__R   R   R
   R   R   R    R   (    (    (    s]   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/cluster.pyt   <module>   s   				

`