ó
 ,µ[c           @   sP   d  d l  m Z d j d d d g ƒ Z d g Z d
 d
 e d d d d	 „ Z d
 S(   iÿÿÿÿ(   t   combinationss   
s!   Ben Edwards (bedwards@cs.unm.edu)s    Huston Hedinger (hstn@hdngr.com)s    Dan Schult (dschult@colgate.edu)t
   dispersiong      ð?g        c   	         s3  ‡  ‡ ‡ ‡ f d †  } | d k rÍ | d k r‰ t d „  |  Dƒ ƒ } x |  D]5 } x, |  | D]  } | |  | | ƒ | | | <q^ WqM Wq/t j |  | i  ƒ } x |  | D] } | |  | | ƒ | | <qª Wnb | d k rt j |  | i  ƒ } x= |  | D] } | |  | | ƒ | | <qú Wn | |  | | ƒ } | S(   sŽ  Calculate dispersion between `u` and `v` in `G`.

    A link between two actors (`u` and `v`) has a high dispersion when their
    mutual ties (`s` and `t`) are not well connected with each other.

    Parameters
    ----------
    G : graph
        A NetworkX graph.
    u : node, optional
        The source for the dispersion score (e.g. ego node of the network).
    v : node, optional
        The target of the dispersion score if specified.
    normalized : bool
        If True (default) normalize by the embededness of the nodes (u and v).

    Returns
    -------
    nodes : dictionary
        If u (v) is specified, returns a dictionary of nodes with dispersion
        score for all "target" ("source") nodes. If neither u nor v is
        specified, returns a dictionary of dictionaries for all nodes 'u' in the
        graph with a dispersion score for each node 'v'.

    Notes
    -----
    This implementation follows Lars Backstrom and Jon Kleinberg [1]_. Typical
    usage would be to run dispersion on the ego network $G_u$ if $u$ were
    specified.  Running :func:`dispersion` with neither $u$ nor $v$ specified
    can take some time to complete.

    References
    ----------
    .. [1] Romantic Partnerships and the Dispersion of Social Ties:
        A Network Analysis of Relationship Status on Facebook.
        Lars Backstrom, Jon Kleinberg.
        https://arxiv.org/pdf/1310.6753v1.pdf

    c            s  t  |  | ƒ ‰  t  ‡  f d †  |  | Dƒ ƒ } t  | | g ƒ } t | d ƒ } d } xZ | D]R \ } } ˆ  j |  | ƒ | }	 | |	 k r^ |	 j |  | ƒ r° | d 7} q° q^ q^ Wt | ƒ }
 ˆ r|
 ˆ d k rï | ˆ ˆ |
 ˆ } n | ˆ ˆ } | } n | } | S(   s=   dispersion for all nodes 'v' in a ego network G_u of node 'u'c         3   s!   |  ] } | ˆ  k r | Vq d  S(   N(    (   t   .0t   n(   t   u_nbrs(    sH   lib/python2.7/site-packages/networkx/algorithms/centrality/dispersion.pys	   <genexpr>6   s    i   i    i   (   t   setR    t   intersectiont
   isdisjointt   len(   t   G_ut   ut   vt   STt   set_uvt   possibt   totalt   st   tt   nbrs_st   embedednesst	   norm_dispR   (   t   alphat   bt   ct
   normalized(   R   sH   lib/python2.7/site-packages/networkx/algorithms/centrality/dispersion.pyt   _dispersion3   s$     	c         s   s   |  ] } | i  f Vq d  S(   N(    (   R   R   (    (    sH   lib/python2.7/site-packages/networkx/algorithms/centrality/dispersion.pys	   <genexpr>V   s    N(   t   Nonet   dictt   fromkeys(	   t   GR
   R   R   R   R   R   R   t   results(    (   R   R   R   R   sH   lib/python2.7/site-packages/networkx/algorithms/centrality/dispersion.pyR   
   s     ) %N(   t	   itertoolsR    t   joint
   __author__t   __all__R   t   TrueR   (    (    (    sH   lib/python2.7/site-packages/networkx/algorithms/centrality/dispersion.pyt   <module>   s
   		