ó
 ,µ[c           @   sm   d  Z  d d l m Z d d l m Z d d l m Z d j d d g ƒ Z d g Z	 d d	 „ Z d
 „  Z d S(   s%   Node redundancy for bipartite graphs.iÿÿÿÿ(   t   division(   t   combinations(   t   NetworkXErrors   
s%   Jordi Torrents <jtorrents@milnou.net>s   Aric Hagberg (hagberg@lanl.gov)t   node_redundancyc            sT   | d k r ˆ  } n  t ‡  f d †  | Dƒ ƒ r@ t d ƒ ‚ n  ‡  f d †  | Dƒ S(   s	  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::

        >>> import networkx as nx
        >>> 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::

        >>> import networkx as nx
        >>> 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::

        >>> import networkx as nx
        >>> 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.

    c         3   s%   |  ] } t  ˆ  | ƒ d  k  Vq d S(   i   N(   t   len(   t   .0t   v(   t   G(    sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pys	   <genexpr>b   s    sS   Cannot compute redundancy coefficient for a node that has fewer than two neighbors.c            s"   i  |  ] } t  ˆ  | ƒ | “ q S(    (   t   _node_redundancy(   R   R   (   R   (    sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pys
   <dictcomp>f   s   	 N(   t   Nonet   anyR   (   R   t   nodes(    (   R   sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pyR      s
    M	c            sP   t  ˆ  ˆ ƒ } t ‡  ‡ f d †  t ˆ  ˆ d ƒ Dƒ ƒ } d | | | d S(   s™  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   s@   |  ]6 \ } } t  ˆ  | ƒ t  ˆ  | ƒ @ˆ h r d  Vq d S(   i   N(   t   set(   R   t   ut   w(   R   R   (    sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pys	   <genexpr>w   s    	i   i   (   R   t   sumR   (   R   R   t   nt   overlap(    (   R   R   sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pyR   i   s    ,N(   t   __doc__t
   __future__R    t	   itertoolsR   t   networkxR   t   joint
   __author__t   __all__R	   R   R   (    (    (    sG   lib/python2.7/site-packages/networkx/algorithms/bipartite/redundancy.pyt   <module>   s   		V