ó žÃÒYc@srdZddlmZddlmZddgZeddƒd d „ƒZd „Zeddƒd „ƒZ d S( s8Algorithms to calculate reciprocity in a directed graph.iÿÿÿÿ(t NetworkXErrori(tnot_implemented_fort reciprocitytoverall_reciprocityt undirectedt multigraphcCsp|dkrt|ƒS||kr]tt||ƒƒd}|dkrVtdƒ‚q]|Sntt||ƒƒS(sfCompute 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. isNot defined for isolated nodes.N(tNoneRtnextt_reciprocity_iterRtdict(tGtnodesR((su/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/reciprocity.pyRs    c cs³|j|ƒ}x|D]•}t|j|ƒƒ}t|j|ƒƒ}||@}t|ƒt|ƒ}|dkr€|dfVqdtt|ƒƒt|ƒ}||fVqWdS(s0 Return an iterator of (node, reciprocity). ig@N(t nbunch_itertsett predecessorst successorstlenRtfloat( R R tntnodetpredtsucctoverlaptn_totalR((su/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/reciprocity.pyRBs    cCsU|jƒ}||jƒjƒd}|dkrAtdƒ‚nt|ƒt|ƒS(s«Compute the reciprocity for the whole graph. See the doc of reciprocity for the definition. Parameters ---------- G : graph A networkx graph iisNot defined for empty graphs(tnumber_of_edgest to_undirectedRR(R t n_all_edgetn_overlap_edge((su/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/reciprocity.pyRUs  N( t__doc__tnetworkxRtutilsRt__all__RRRR(((su/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/reciprocity.pyt s  0