U
    ª9%e  ã                   @   s    d dl mZ dgZddd„ZdS )	é    )ÚcombinationsÚ
dispersionNTç      ð?ç        c           	         sÖ   ‡ ‡‡‡fdd„}|dkrŽ|dkr^dd„ | D ƒ}| D ]&}| | D ]}|| ||ƒ|| |< q@q4qÒt  | | i ¡}| | D ]}|| ||ƒ||< qvnD|dkrÆt  | | i ¡}| | D ]}|| ||ƒ||< q®n|| ||ƒ}|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).
    alpha, b, c : float
        Parameters for the normalization procedure. When `normalized` is True,
        the dispersion value is normalized by::

            result = ((dispersion + b) ** alpha) / (embeddedness + c)

        as long as the denominator is nonzero.

    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 ]8\}}ˆ  | | ¡| }	||	kr<|	 | | ¡r<|d7 }q<t|ƒ}
|}ˆrª|ˆ ˆ }|
ˆ dkrª||
ˆ  }|S )z=dispersion for all nodes 'v' in a ego network G_u of node 'u'c                    s   h | ]}|ˆ kr|’qS © r   ©Ú.0Ún©Zu_nbrsr   úh/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/networkx/algorithms/centrality/dispersion.pyÚ	<setcomp>9   s      z2dispersion.<locals>._dispersion.<locals>.<setcomp>é   r   é   )Úsetr   ÚintersectionÚ
isdisjointÚlen)ZG_uÚuÚvZSTZset_uvZpossibÚtotalÚsÚtZnbrs_sZembedednessZdispersion_val©ÚalphaÚbÚcÚ
normalizedr
   r   Ú_dispersion6   s"    

zdispersion.<locals>._dispersionNc                 S   s   i | ]
}|i “qS r   r   r   r   r   r   Ú
<dictcomp>U   s      zdispersion.<locals>.<dictcomp>)ÚdictÚfromkeys)	ÚGr   r   r   r   r   r   r   Úresultsr   r   r   r      s     0)NNTr   r   r   )Ú	itertoolsr   Ú__all__r   r   r   r   r   Ú<module>   s   