o
    ßË·eö  ã                   @   s    d dl mZ dgZddd„ZdS )	é    )ÚcombinationsÚ
dispersionNTç      ð?ç        c           	         sÜ   ‡ ‡‡‡fdd„}|du rI|du r0dd„ | D ƒ}| D ]}| | D ]}|| ||ƒ|| |< q q|S t  | | i ¡}| | D ]
}|| ||ƒ||< q<|S |du rft  | | i ¡}| | D ]
}|| ||ƒ||< qY|S || ||ƒ}|S )aŽ  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 | | ƒ‰ ‡ fdd„| | D ƒ}||h}t|dƒ}d}|D ]\}}ˆ  | | ¡| }	||	vr:|	 | | ¡r:|d7 }qt|ƒ}
|}ˆrU|ˆ ˆ }|
ˆ dkrU||
ˆ  }|S )z=dispersion for all nodes 'v' in a ego network G_u of node 'u'c                    s   h | ]}|ˆ v r|’qS © r   ©Ú.0Ún©Úu_nbrsr   ú`/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/centrality/dispersion.pyÚ	<setcomp>2   s    z2dispersion.<locals>._dispersion.<locals>.<setcomp>é   r   é   )Úsetr   ÚintersectionÚ
isdisjointÚlen)ÚG_uÚuÚvÚSTÚset_uvÚpossibÚtotalÚsÚtÚnbrs_sÚembedednessÚdispersion_val©ÚalphaÚbÚcÚ
normalizedr
   r   Ú_dispersion/   s$   
€zdispersion.<locals>._dispersionNc                 S   s   i | ]}|i “qS r   r   r   r   r   r   Ú
<dictcomp>N   s    zdispersion.<locals>.<dictcomp>)ÚdictÚfromkeys)	ÚGr   r   r$   r!   r"   r#   r%   Úresultsr   r    r   r      s(   )ÿóøþ)NNTr   r   r   )Ú	itertoolsr   Ú__all__r   r   r   r   r   Ú<module>   s    