ó
ú£Õ\c           @   sk   d  d l  m Z d  d l Z d j d d g ƒ Z d d g Z d d d „ Z d	 d	 d d d
 „ Z	 e	 Z
 d S(   iÿÿÿÿ(   t   defaultdictNs   
s%   Jordi Torrents <jtorrents@milnou.net>s   Aric Hagberg (hagberg@lanl.gov)t   average_degree_connectivityt   k_nearest_neighborsc            s4  t  t ƒ } t  t ƒ } x³ | | ƒ j ƒ  D]Ÿ \ ‰ } | | ˆ ƒ ƒ }	 ˆ d  k rp t t |	 j ƒ  ƒ ƒ }
 n. t t ‡  ‡ ‡ f d †  |	 j ƒ  Dƒ ƒ ƒ }
 | | c | ˆ d ˆ ƒ7<| | c |
 7<q+ Wi  } xY | j ƒ  D]K \ } } | | | <| | } | d k rá | d k rá | | c | :<qá qá W| S(   Nc         3   s3   |  ]) \ } } ˆ  ˆ | j  ˆ d  ƒ | Vq d S(   i   N(   t   get(   t   .0t   nbrt   d(   t   Gt   nt   weight(    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/connectivity.pys	   <genexpr>   s   R	   i    (   R    t   floatt   itemst   Nonet   sumt   values(   R   t	   neighborst   source_degreet   target_degreet   nodesR	   t   dsumt   dnormt   kt   nbrdegt   st   dct   avgt   norm(    (   R   R   R	   sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/connectivity.pyt   _avg_deg_conn   s"    

s   in+outc   	   	   C   s±   |  j  } |  j  } |  j } |  j ƒ  r’ i |  j d 6|  j d 6|  j  d 6} | | } | | } | d k rw |  j } q’ | d k r’ |  j } q’ n  t |  | | | d | d | ƒS(   s.  Compute the average degree connectivity of graph.

    The average degree connectivity is the average nearest neighbor degree of
    nodes with degree k. For weighted graphs, an analogous measure can 
    be computed using the weighted average neighbors degree defined in 
    [1]_, for a node `i`, as:

    .. math::

        k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j

    where `s_i` is the weighted degree of node `i`, 
    `w_{ij}` is the weight of the edge that links `i` and `j`,
    and `N(i)` are the neighbors of node `i`.

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

    source :  "in"|"out"|"in+out" (default:"in+out")
       Directed graphs only. Use "in"- or "out"-degree for source node.

    target : "in"|"out"|"in+out" (default:"in+out"
       Directed graphs only. Use "in"- or "out"-degree for target node.

    nodes: list or iterable (optional)
        Compute neighbor connectivity for these nodes. The default is all nodes.

    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
    -------
    d: dict
       A dictionary keyed by degree k with the value of average connectivity.
    
    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> G.edge[1][2]['weight'] = 3
    >>> nx.k_nearest_neighbors(G)
    {1: 2.0, 2: 1.5}
    >>> nx.k_nearest_neighbors(G, weight='weight')
    {1: 2.0, 2: 1.75}

    See also
    --------
    neighbors_average_degree

    Notes
    -----
    This algorithm is sometimes called "k nearest neighbors'.

    References
    ----------    
    .. [1] A. Barrat, M. BarthÃ©lemy, R. Pastor-Satorras, and A. Vespignani, 
       "The architecture of complex weighted networks". 
       PNAS 101 (11): 3747â€“3752 (2004).
    t   outt   ins   in+outR   R	   (   t   degreeR   t   is_directedt
   out_degreet	   in_degreet   predecessorst
   successorsR   (	   R   t   sourcet   targetR   R	   R   R   R   t	   direction(    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/connectivity.pyR   &   s    >			


(   t   collectionsR    t   networkxt   nxt   joint
   __author__t   __all__R   R   R   R   (    (    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/connectivity.pyt   <module>   s   		M