§
    bŠtj  ã                   óX   — d Z ddlmZ ddlZddlmZ dgZej        dd„¦   «         Zd„ Z	dS )	z%Node redundancy for bipartite graphs.é    )ÚcombinationsN)ÚNetworkXErrorÚnode_redundancyc                 ó|   ‡ — |€‰ }t          ˆ fd„|D ¦   «         ¦  «        rt          d¦  «        ‚ˆ fd„|D ¦   «         S )uk  Computes the node redundancy coefficients for the nodes in the bipartite
    graph `G`.

    The redundancy coefficient of a node `v` is the fraction of pairs of
    neighbors of `v` that are both linked to other nodes. In a one-mode
    projection these nodes would be linked together even if `v` were
    not there.

    More formally, for any vertex `v`, the *redundancy coefficient of `v`* is
    defined by

    .. math::

        rc(v) = \frac{|\{\{u, w\} \subseteq N(v),
        \: \exists v' \neq  v,\: (v',u) \in E\:
        \mathrm{and}\: (v',w) \in E\}|}{ \frac{|N(v)|(|N(v)|-1)}{2}},

    where `N(v)` is the set of neighbors of `v` in `G` [1]_.

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

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

    Returns
    -------
    redundancy : dictionary
        A dictionary keyed by node with the node redundancy value.

    Examples
    --------
    Compute the redundancy coefficient of each node in a graph:

    >>> from networkx.algorithms import bipartite
    >>> G = nx.cycle_graph(4)
    >>> rc = bipartite.node_redundancy(G)
    >>> rc[0]
    1.0

    Compute the average redundancy for the graph:

    >>> from networkx.algorithms import bipartite
    >>> G = nx.cycle_graph(4)
    >>> rc = bipartite.node_redundancy(G)
    >>> sum(rc.values()) / len(G)
    1.0

    Compute the average redundancy for a set of nodes:

    >>> from networkx.algorithms import bipartite
    >>> G = nx.cycle_graph(4)
    >>> rc = bipartite.node_redundancy(G)
    >>> nodes = [0, 2]
    >>> sum(rc[n] for n in nodes) / len(nodes)
    1.0

    Raises
    ------
    NetworkXError
        If any of the nodes in the graph (or in `nodes`, if specified) has
        (out-)degree less than two (which would result in division by zero,
        according to the definition of the redundancy coefficient).

    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.

    Nc              3   óJ   •K  — | ]}t          ‰|         ¦  «        d k     V — ŒdS )é   N)Úlen©Ú.0ÚvÚGs     €úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/redundancy.pyú	<genexpr>z"node_redundancy.<locals>.<genexpr>X   s1   øè è € Ð
(Ð
(˜Q�3ˆq�Œt‰9Œ9�qŠ=Ð
(Ð
(Ð
(Ð
(Ð
(Ð
(ó    zSCannot compute redundancy coefficient for a node that has fewer than two neighbors.c                 ó2   •— i | ]}|t          ‰|¦  «        “ŒS © )Ú_node_redundancyr
   s     €r   ú
<dictcomp>z#node_redundancy.<locals>.<dictcomp>^   s&   ø€ Ð5Ð5Ð5¨!ˆAÕ  1Ñ%Ô%Ð5Ð5Ð5r   )Úanyr   )r   Únodess   ` r   r   r      sh   ø€ ðV €}ØˆÝ
Ð
(Ð
(Ð
(Ð
( %Ð
(Ñ
(Ô
(Ñ(Ô(ð 
Ýð2ñ
ô 
ð 	
ð
 6Ð5Ð5Ð5¨uÐ5Ñ5Ô5Ð5r   c                 ó¬   ‡ ‡— t          ‰ ‰         ¦  «        }t          ˆ ˆfd„t          ‰ ‰         d¦  «        D ¦   «         ¦  «        }d|z  ||dz
  z  z  S )a™  Returns the redundancy of the node `v` in the bipartite graph `G`.

    If `G` is a graph with `n` nodes, the redundancy of a node is the ratio
    of the "overlap" of `v` to the maximum possible overlap of `v`
    according to its degree. The overlap of `v` is the number of pairs of
    neighbors that have mutual neighbors themselves, other than `v`.

    `v` must have at least two neighbors in `G`.

    c              3   ó€   •K  — | ]8\  }}t          ‰|         ¦  «        t          ‰|         ¦  «        z  ‰hz
  ¯4d V — Œ9dS )é   N)Úset)r   ÚuÚwr   r   s      €€r   r   z#_node_redundancy.<locals>.<genexpr>m   s`   øè è € ð ð Ùˆq�!µ#°a¸´d±)´)½cÀ!ÀAÄ$¹i¼iÑ2GÈAÈ3Ñ1NðØ	ðð ð ð ð ð r   r   r   )r	   Úsumr   )r   r   ÚnÚoverlaps   ``  r   r   r   a   sv   øø€ õ 	ˆAˆaŒD‰	Œ	€AÝð ð ð ð ð Ý$ Q q¤T¨1Ñ-Ô-ðñ ô ñ ô €Gð �‰K˜A  Q¡™KÑ(Ð(r   )N)
Ú__doc__Ú	itertoolsr   ÚnetworkxÚnxr   Ú__all__Ú_dispatchabler   r   r   r   r   ú<module>r&      s…   ðØ +Ð +à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "àÐ
€ð ÔðR6ð R6ð R6ñ ÔðR6ðj)ð )ð )ð )ð )r   