K i' dZddlZddlmZddlmZddgZedd ejd d Zd Z edd ejd Z y)z8Algorithms to calculate reciprocity in a directed graph.N) NetworkXError)not_implemented_for reciprocityoverall_reciprocity undirected multigraphc| t|S||vr'tt||d}| td|St t||S)afCompute 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. zNot defined for isolated nodes.)rnext_reciprocity_iterrdict)Gnodesrs e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/reciprocity.pyrr sbB }"1%% z,Q67:   AB B  !!U+ ,,c#"K|j|}|D]u}t|j|}t|j|}||z}t |t |z}|dk(r|df_dt |z|z }||fwyw)z*Return an iterator of (node, reciprocity).rNr) nbunch_iterset predecessors successorslen) rrnnodepredsuccoverlapn_totalrs rr r =s eA &1>>$'(1<<%&+d)c$i' a<, c'l*W4K% % &sB Bc|j}||jjz dz}|dk(r td||z S)zCompute the reciprocity for the whole graph. See the doc of reciprocity for the definition. Parameters ---------- G : graph A networkx graph rrzNot defined for empty graphs)number_of_edges to_undirectedr)r n_all_edgen_overlap_edges rrrOsO""$J 1??#4#D#D#FF!KNQ:;; J &&r)N) __doc__networkxnxrutilsr__all__ _dispatchablerr rrrr+st>"' / 0\<0--1--`&$\<0'1'r