
    h4                        S r SSKrSSKJrJr  / SQr\" S5      \" S5      \" S5      \R                  " SS	9SS
 j5       5       5       5       r\" S5      \" S5      \" S5      \R                  " SS	9SS j5       5       5       5       r	\" S5      \" S5      \" S5      \R                  SS j5       5       5       5       r
\" S5      \" S5      \" S5      \R                  SS j5       5       5       5       rg)aG  Functions for estimating the small-world-ness of graphs.

A small world network is characterized by a small average shortest path length,
and a large clustering coefficient.

Small-worldness is commonly measured with the coefficient sigma or omega.

Both coefficients compare the average clustering coefficient and shortest path
length of a given graph against the same quantities for an equivalent random
or lattice graph.

For more information, see the Wikipedia article on small-world network [1]_.

.. [1] Small-world network:: https://en.wikipedia.org/wiki/Small-world_network

    N)not_implemented_forpy_random_state)random_referencelattice_referencesigmaomegadirected
multigraph   T)returns_graphc                    [        U 5      S:  a  [        R                  " S5      e[        U R                  5      S:  a  [        R                  " S5      eSSKJnJn  [        R                  R                  nU R                  5       n [        U R                  5       6 u  pxU" U5      n	[        U 5      n
[        R                  " U 5      nX-  n[        X-  XS-
  -  S-  -  5      nSn[        U5       GH\  nSnX:  d  M  U" SXS9u  nnUU:X  a  M  UU   nUU   nUR                  [!        U R#                  U5      5      5      nUR                  [!        U R#                  U5      5      5      nUUUU4;   d	  UUUU4;   a  M  UU U   ;  a  UU U   ;  a  U R%                  UU5        U R%                  UU5        U R'                  UU5        U R'                  UU5        U(       aW  U" U UU5      S:X  aI  U R'                  UU5        U R'                  UU5        U R%                  UU5        U R%                  UU5        OUS-  nGMO  US-  nX:  a  GMO  GM_     U $ )	aq  Compute a random graph by swapping edges of a given graph.

Parameters
----------
G : graph
    An undirected graph with 4 or more nodes.

niter : integer (optional, default=1)
    An edge is rewired approximately `niter` times.

connectivity : boolean (optional, default=True)
    When True, ensure connectivity for the randomized graph.

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : graph
    The randomized graph.

Raises
------
NetworkXError
    If there are fewer than 4 nodes or 2 edges in `G`

Notes
-----
The implementation is adapted from the algorithm by Maslov and Sneppen
(2002) [1]_.

References
----------
.. [1] Maslov, Sergei, and Kim Sneppen.
       "Specificity and stability in topology of protein networks."
       Science 296.5569 (2002): 910-913.
    Graph has fewer than four nodes.   Graph has fewer that 2 edgesr   cumulative_distributiondiscrete_sequence   cdistributionseed)lennxNetworkXErroredgesnetworkx.utilsr   r   connectivitylocal_edge_connectivitycopyzipdegreenumber_of_edgesintrangechoicelist	neighborsadd_edgeremove_edge)Gniterr   r   r   r   
local_connkeysdegreescdfnnodesnedgesntries	swapcountinaiciacbds                         P/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/smallworld.pyr   r      s   V 1vzABB
177|a=>>I88J	A$MD
!'
*CVF"FNEFqj$9A$=>?FI5\j )#IHRRxRARADQ01ADQ01AQ1I~q!Qi 1AQqTM

1a 

1a a#a#  Jq!Q$71$<MM!Q'MM!Q'JJq!$JJq!$NIFA? jj D H    r   c                    SSK nSSKJnJn  [        R
                  R                  n[        U 5      S:  a  [        R                  " S5      e[        U R                  5      S:  a  [        R                  " S5      eU R                  5       n [        U R                  5       6 u  pU" U
5      n[        U 5      n[        R                  " U 5      nUc  UR                  X45      nUR                  SU5      nUR                  US-
  SS	5      nUR!                  S
UR#                  X:  X5      5      n[%        ['        UR)                  US-  5      5      5       HH  nUR!                  UUS-   S USUS-    5      X,U-
  S-
  SS24'   X,U-
  S-
  SS24   SSS	2   UUSS24'   MJ     X-  n['        X-  XS-
  -  S-  -  5      n[%        U5       GH  nSnUU:  d  M  U" SXS9u  nnUU:X  a  M  U	U   nU	U   nUR+                  [-        U R/                  U5      5      5      nUR+                  [-        U R/                  U5      5      5      nU	R1                  U5      nU	R1                  U5      nUUUU4;   d	  UUUU4;   a  M  UU U   ;  a  UU U   ;  a  UUU4   UUU4   -   UUU4   UUU4   -   :  a  U R3                  UU5        U R3                  UU5        U R5                  UU5        U R5                  UU5        U(       aW  U" U UU5      S:X  aI  U R5                  UU5        U R5                  UU5        U R3                  UU5        U R3                  UU5        OGM  US-  nUU:  a  GM  GM     U $ )aX  Latticize the given graph by swapping edges.

Parameters
----------
G : graph
    An undirected graph.

niter : integer (optional, default=1)
    An edge is rewired approximately niter times.

D : numpy.array (optional, default=None)
    Distance to the diagonal matrix.

connectivity : boolean (optional, default=True)
    Ensure connectivity for the latticized graph when set to True.

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : graph
    The latticized graph.

Raises
------
NetworkXError
    If there are fewer than 4 nodes or 2 edges in `G`

Notes
-----
The implementation is adapted from the algorithm by Sporns et al. [1]_.
which is inspired from the original work by Maslov and Sneppen(2002) [2]_.

References
----------
.. [1] Sporns, Olaf, and Jonathan D. Zwi.
   "The small world of the cerebral cortex."
   Neuroinformatics 2.2 (2004): 145-162.
.. [2] Maslov, Sergei, and Kim Sneppen.
   "Specificity and stability in topology of protein networks."
   Science 296.5569 (2002): 910-913.
r   Nr   r   r   r   r   r   )r   r   )numpyr   r   r   r   r   r   r   r   r   r    r!   r"   r#   zerosarangeappendwherer%   r$   ceilr&   r'   r(   indexr)   r*   )r+   r,   Dr   r   npr   r   r-   r.   r/   r0   r1   r2   unumuvmax_attempts_r6   r7   r8   r9   r:   r;   r<   bidis                                r=   r   r   z   s:   b I88J
1vzABB
177|a=>> 	
A$MD
!'
*CVF"FyHHf%&YYq&!YYvz1b)IIdBHHRWb56s2776A:./0A#%99Qq1uwZ7QU#DAqj1na 
Q)*4R40AadG 1 NEv&QJ*?!*CDEL5\, )#IHRRxRARADQ01ADQ01AABABQ1I~q!Qi 1AQqTMRV9qRy(Ab"fI"b&	,AAJJq!$JJq!$MM!Q'MM!Q' $
1a(;q(@a+a+

1a(

1a(FAG , N Hr>   c                    SSK n/ / S.n[        U5       H]  n[        XUS9nUS   R                  [        R
                  " U5      5        US   R                  [        R                  " U5      5        M_     [        R
                  " U 5      n[        R                  " U 5      n	UR                  US   5      n
UR                  US   5      nX-  X-  -  n[        U5      $ )a  Returns the small-world coefficient (sigma) of the given graph.

The small-world coefficient is defined as:
sigma = C/Cr / L/Lr
where C and L are respectively the average clustering coefficient and
average shortest path length of G. Cr and Lr are respectively the average
clustering coefficient and average shortest path length of an equivalent
random graph.

A graph is commonly classified as small-world if sigma>1.

Parameters
----------
G : NetworkX graph
    An undirected graph.
niter : integer (optional, default=100)
    Approximate number of rewiring per edge to compute the equivalent
    random graph.
nrand : integer (optional, default=10)
    Number of random graphs generated to compute the average clustering
    coefficient (Cr) and average shortest path length (Lr).
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
sigma : float
    The small-world coefficient of G.

Notes
-----
The implementation is adapted from Humphries et al. [1]_ [2]_.

References
----------
.. [1] The brainstem reticular formation is a small-world, not scale-free,
       network M. D. Humphries, K. Gurney and T. J. Prescott,
       Proc. Roy. Soc. B 2006 273, 503-511, doi:10.1098/rspb.2005.3354.
.. [2] Humphries and Gurney (2008).
       "Network 'Small-World-Ness': A Quantitative Method for Determining
       Canonical Network Equivalence".
       PLoS One. 3 (4). PMID 18446219. doi:10.1371/journal.pone.0002051.
r   NCLr,   r   rT   rU   )	rA   r%   r   rD   r   transitivityaverage_shortest_path_lengthmeanfloat)r+   r,   nrandr   rI   randMetricsr5   GrrT   rU   CrLrr   s                r=   r   r      s    b  $K5\a48C 34C ? ? CD 
 	A
''*A	S!	"B	S!	"BVE<r>   c                    SSK n/ / S.n[        R                  " U 5      nUnUS-  n[        U5       H^  n	[	        XUS9n
US   R                  [        R                  " U
5      5        [        XUS9n[        R                  " U5      nX:  d  M\  UnM`     [        R                  " U 5      n[        R                  " U 5      nUR                  US   5      nX-  X-  -
  n[        U5      $ )a   Returns the small-world coefficient (omega) of a graph

The small-world coefficient of a graph G is:

omega = Lr/L - C/Cl

where C and L are respectively the average clustering coefficient and
average shortest path length of G. Lr is the average shortest path length
of an equivalent random graph and Cl is the average clustering coefficient
of an equivalent lattice graph.

The small-world coefficient (omega) measures how much G is like a lattice
or a random graph. Negative values mean G is similar to a lattice whereas
positive values mean G is a random graph.
Values close to 0 mean that G has small-world characteristics.

Parameters
----------
G : NetworkX graph
    An undirected graph.

niter: integer (optional, default=5)
    Approximate number of rewiring per edge to compute the equivalent
    random graph.

nrand: integer (optional, default=10)
    Number of random graphs generated to compute the maximal clustering
    coefficient (Cr) and average shortest path length (Lr).

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.


Returns
-------
omega : float
    The small-world coefficient (omega)

Notes
-----
The implementation is adapted from the algorithm by Telesford et al. [1]_.

References
----------
.. [1] Telesford, Joyce, Hayasaka, Burdette, and Laurienti (2011).
       "The Ubiquity of Small-World Networks".
       Brain Connectivity. 1 (0038): 367-75.  PMC 3604768. PMID 22432451.
       doi:10.1089/brain.2011.0038.
r   NrS   r   rV   rU   )
rA   r   average_clusteringr%   r   rD   rX   r   rY   rZ   )r+   r,   r[   r   rI   r\   Clniter_lattice_referenceniter_random_referencerO   r]   GlCl_temprT   rU   r_   r   s                    r=   r   r   ;  s    n  $K
 
		q	!B#"QY5\aDIC ? ? CD qdK ''+<B  	a A
''*A	S!	"BVE<r>   )r   TN)   NTN)d   
   N)rg   ri   N)__doc__networkxr   r   r   r   __all___dispatchabler   r   r   r    r>   r=   <module>ro      s.  "  ?
E Z \"%[ &  # ![| Z \"%u &  # !up Z \">   # !>B Z \"U   # !Ur>   