ó
 ,ľ[c           @   sr   d  Z  d d l m Z d d l m Z d d g Z e d d  d d	   Z d
   Z e d d  d    Z	 d S(   s8   Algorithms to calculate reciprocity in a directed graph.i˙˙˙˙(   t   NetworkXErrori   (   t   not_implemented_fort   reciprocityt   overall_reciprocityt
   undirectedt
   multigraphc         C   sp   | d k r t |   S| |  k r] t t |  |   d } | d k rV t d   q] | Sn  t t |  |   S(   sf  Compute the reciprocity in a directed graph.

    The reciprocity of a directed graph is defined as the ratio
    of the number of edges pointing in both directions to the total
    number of edges in the graph.
    Formally, $r = |{(u,v) \in G|(v,u) \in G}| / |{(u,v) \in G}|$.

    The reciprocity of a single node u is defined similarly,
    it is the ratio of the number of edges in both directions to
    the total number of edges attached to node u.

    Parameters
    ----------
    G : graph
       A networkx directed graph
    nodes : container of nodes, optional (default=whole graph)
       Compute reciprocity for nodes in this container.

    Returns
    -------
    out : dictionary
       Reciprocity keyed by node label.

    Notes
    -----
    The reciprocity is not defined for isolated nodes.
    In such cases this function will return None.

    i   s   Not defined for isolated nodes.N(   t   NoneR   t   nextt   _reciprocity_iterR    t   dict(   t   Gt   nodesR   (    (    s>   lib/python2.7/site-packages/networkx/algorithms/reciprocity.pyR      s     
c   	      c   sł   |  j  |  } x | D] } t |  j |   } t |  j |   } | | @} t |  t |  } | d k r | d f Vq d t t |   t |  } | | f Vq Wd S(   s0    Return an iterator of (node, reciprocity).
    i    g       @N(   t   nbunch_itert   sett   predecessorst
   successorst   lenR   t   float(	   R
   R   t   nt   nodet   predt   succt   overlapt   n_totalR   (    (    s>   lib/python2.7/site-packages/networkx/algorithms/reciprocity.pyR   B   s    
 c         C   sU   |  j    } | |  j   j    d } | d k rA t d   n  t |  t |  S(   sŤ   Compute the reciprocity for the whole graph.

    See the doc of reciprocity for the definition.

    Parameters
    ----------
    G : graph
       A networkx graph

    i   i    s   Not defined for empty graphs(   t   number_of_edgest   to_undirectedR    R   (   R
   t
   n_all_edget   n_overlap_edge(    (    s>   lib/python2.7/site-packages/networkx/algorithms/reciprocity.pyR   U   s
    N(
   t   __doc__t   networkxR    t   utilsR   t   __all__R   R   R   R   (    (    (    s>   lib/python2.7/site-packages/networkx/algorithms/reciprocity.pyt   <module>
   s   0	