
    h                         S r SSKrSSKrSSKJr  S/r\" S5      \" S5      \R                  " SS9S
S	 j5       5       5       rg)z#Computation of graph non-randomness    N)not_implemented_fornon_randomnessdirected
multigraphweight)
edge_attrsc           	      8   SSK n[        R                  " U 5      (       a  [        R                  " S5      e[        R                  " U 5      (       d  [        R
                  " S5      e[        [        [        R                  " U 5      5      5      S:  a  [        R                  " S5      eUc1  [        [        [        R                  R                  U 5      5      5      nUR                  R                  [        R                  " XS95      n[        UR!                  UR#                  USU 5      5      5      nU R%                  5       nU R'                  5       nSU-  U-  XfU-
  -  -  nXVSU-  -
  U-  U-   -
  [(        R*                  " SU-  U-  SU-
  -  5      -  n	XY4$ )	a  Compute the non-randomness of graph G.

The first returned value nr is the sum of non-randomness values of all
edges within the graph (where the non-randomness of an edge tends to be
small when the two nodes linked by that edge are from two different
communities).

The second computed value nr_rd is a relative measure that indicates
to what extent graph G is different from random graphs in terms
of probability. When it is close to 0, the graph tends to be more
likely generated by an Erdos Renyi model.

Parameters
----------
G : NetworkX graph
    Graph must be symmetric, connected, and without self-loops.

k : int
    The number of communities in G.
    If k is not set, the function will use a default community
    detection algorithm to set it.

weight : string or None, optional (default=None)
    The name of an edge attribute that holds the numerical value used
    as a weight. If None, then each edge has weight 1, i.e., the graph is
    binary.

Returns
-------
non-randomness : (float, float) tuple
    Non-randomness, Relative non-randomness w.r.t.
    Erdos Renyi random graphs.

Raises
------
NetworkXException
    if the input graph is not connected.
NetworkXError
    if the input graph contains self-loops or if graph has no edges.

Examples
--------
>>> G = nx.karate_club_graph()
>>> nr, nr_rd = nx.non_randomness(G, 2)
>>> nr, nr_rd = nx.non_randomness(G, 2, "weight")

Notes
-----
This computes Eq. (4.4) and (4.5) in Ref. [1]_.

If a weight field is passed, this algorithm will use the eigenvalues
of the weighted adjacency matrix to compute Eq. (4.4) and (4.5).

References
----------
.. [1] Xiaowei Ying and Xintao Wu,
       On Randomness Measures for Social Networks,
       SIAM International Conference on Data Mining. 2009
r   Nz-non_randomness not applicable to empty graphszNon connected graph.z!Graph must not contain self-loops)r         )numpynxis_emptyNetworkXErroris_connectedNetworkXExceptionlenlistselfloop_edgestuple	communitylabel_propagation_communitieslinalgeigvalsto_numpy_arrayfloatrealsumnumber_of_nodesnumber_of_edgesmathsqrt)
Gkr   npeigenvaluesnrnmpnr_rds
             T/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/non_randomness.pyr   r      sU   ~  
{{1~~NOO??1""#9::
4!!!$%&*BCCybll@@CDE ))##B$5$5a$GHK	rwwrvvk"1o./	0B	A	A	
QqE{#A AI?Q&'499QUQY!a%5H+IIE9    )Nr   )	__doc__r    networkxr   networkx.utilsr   __all___dispatchabler    r,   r+   <module>r3      sT    *   .
 Z \"X&T ' # !Tr,   