o
    ˷e                     @   s8   d Z ddlmZ ddlmZ dgZd	ddZdd ZdS )
z%Node redundancy for bipartite graphs.    )combinations)NetworkXErrornode_redundancyNc                    s<   |du r }t  fdd|D rtd fdd|D S )u  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`.

    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   s     | ]}t  | d k V  qdS )   N)len.0vG _/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/bipartite/redundancy.py	<genexpr>U   s    z"node_redundancy.<locals>.<genexpr>zSCannot compute redundancy coefficient for a node that has fewer than two neighbors.c                    s   i | ]}|t  |qS r   )_node_redundancyr   r
   r   r   
<dictcomp>[   s    z#node_redundancy.<locals>.<dictcomp>)anyr   )r   nodesr   r
   r   r   	   s   Jc                    sB   t   }t fddt  dD }d| ||d   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   s6    | ]\}}t  | t  | @ h rd V  qdS )   N)set)r   uwr   r	   r   r   r   l   s    "
z#_node_redundancy.<locals>.<genexpr>r   r   )r   sumr   )r   r	   noverlapr   r   r   r   ^   s
   r   )N)__doc__	itertoolsr   networkxr   __all__r   r   r   r   r   r   <module>   s    
U