
    \Mh'                         d Z ddlZddlmZ ddlmZ ddgZ edd	          ej        dd
                        Zd Z	 edd	          ej        d                         Z
dS )z8Algorithms to calculate reciprocity in a directed graph.    N)NetworkXError   )not_implemented_forreciprocityoverall_reciprocity
undirected
multigraphc                     |t          |           S || v r6t          t          | |                    d         }|t          d          |S t	          t          | |                    S )af  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.

    N   zNot defined for isolated nodes.)r   next_reciprocity_iterr   dict)Gnodesr   s      _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/reciprocity.pyr   r      sv    B }"1%%% zz,Q6677: ABBB !!U++,,,    c              #   ^  K   |                      |          }|D ]}t          |                     |                    }t          |                     |                    }||z  }t	          |          t	          |          z   }|dk    r|dfV  wdt	          |          z  |z  }||fV  dS )z*Return an iterator of (node, reciprocity).r   Nr   )nbunch_itersetpredecessors
successorslen)	r   r   nnodepredsuccoverlapn_totalr   s	            r   r   r   =   s      	eA & &1>>$''((1<<%%&&+d))c$ii' a<<,c'll*W4K%%%%%& &r   c                     |                                  }||                                                                  z
  dz  }|dk    rt          d          ||z  S )zCompute the reciprocity for the whole graph.

    See the doc of reciprocity for the definition.

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

    r   r   zNot defined for empty graphs)number_of_edgesto_undirectedr   )r   
n_all_edgen_overlap_edges      r   r   r   O   s]     ""$$J 1??#4#4#D#D#F#FF!KNQ:;;;J&&r   )N)__doc__networkxnxr   utilsr   __all___dispatchabler   r   r    r   r   <module>r+      s    > >     " " " " " " ' ' ' ' ' '/
0 \<00-- -- --  10--`& & &$ \<00' '  10' ' 'r   