
    h                        S r SSKrSSKJr  SSKJrJrJr  SSKr	SSK
Jr  / SQr\	R                  " SS9SS	S
.S jj5       rS r\" S5      \	R                  " SSS9 SS	S
.S jj5       5       r\" S5      \	R                  " SSS9 SS	S
.S jj5       5       r\" S5      \	R                  " SSS9      SS	SS.S jj5       5       r\" S5      \	R                  " SSS9      S S	S
.S jj5       5       r\" S5      \	R                  " SSS9S!S j5       5       r\" S5      \	R                  " SSS9     S"S	SS.S jj5       5       r\" S5      \	R                  " SSS9SSSSSS.S j5       5       rg)#z Generators for geometric graphs.    N)bisect_left)
accumulatecombinationsproduct)py_random_state)geometric_edgesgeographical_threshold_graphnavigable_small_world_graphrandom_geometric_graphsoft_random_geometric_graph"thresholded_random_geometric_graphwaxman_graph"geometric_soft_configuration_graphpos_name)
node_attrspos)r   c                    U R                  US9 H%  u  pEUb  M
  [        R                  " SU SU S35      e   [        XX#5      $ )a  Returns edge list of node pairs within `radius` of each other.

Parameters
----------
G : networkx graph
    The graph from which to generate the edge list. The nodes in `G` should
    have an attribute ``pos`` corresponding to the node position, which is
    used to compute the distance to other nodes.
radius : scalar
    The distance threshold. Edges are included in the edge list if the
    distance between the two nodes is less than `radius`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position of each
    node in 2D coordinates. Every node in the Graph must have this attribute.
p : scalar, default=2
    The `Minkowski distance metric
    <https://en.wikipedia.org/wiki/Minkowski_distance>`_ used to compute
    distances. The default value is 2, i.e. Euclidean distance.

Returns
-------
edges : list
    List of edges whose distances are less than `radius`

Notes
-----
Radius uses Minkowski distance metric `p`.
If scipy is available, `scipy.spatial.cKDTree` is used to speed computation.

Examples
--------
Create a graph with nodes that have a "pos" attribute representing 2D
coordinates.

>>> G = nx.Graph()
>>> G.add_nodes_from(
...     [
...         (0, {"pos": (0, 0)}),
...         (1, {"pos": (3, 0)}),
...         (2, {"pos": (8, 0)}),
...     ]
... )
>>> nx.geometric_edges(G, radius=1)
[]
>>> nx.geometric_edges(G, radius=4)
[(0, 1)]
>>> nx.geometric_edges(G, radius=6)
[(0, 1), (1, 2)]
>>> nx.geometric_edges(G, radius=9)
[(0, 1), (0, 2), (1, 2)]
datazNode z (and all nodes) must have a 'z' attribute.)nodesnxNetworkXError_geometric_edges)Gradiuspr   nr   s         O/var/www/html/env/lib/python3.13/site-packages/networkx/generators/geometric.pyr   r      sV    l ''x'(;""s8
,O  ) Aq33    c                   ^ U R                  US9n SSKn[        [        U6 5      u  pUR                  R                  U5      nUR                  UT5      n[        U5       VV	s/ s H  u  pyX   X   4PM     nnn	U$ ! [         aa    UT-  n[        US5       VVV	V
s/ s H1  u  u  pxu  p[	        U4S j[        X5       5       5      U::  d  M.  Xy4PM3     Os  sn
n	nnf nn	nnn
Us $ f = fs  sn	nf )zf
Implements `geometric_edges` without input validation. See `geometric_edges`
for complete docstring.
r   r   N   c              3   H   >#    U  H  u  p[        X-
  5      T-  v   M     g 7fNabs.0abr   s      r   	<genexpr>#_geometric_edges.<locals>.<genexpr>g   s     ;{tq3qu:?{   ")r   scipyImportErrorr   sumziplistspatialcKDTreequery_pairssorted)r   r   r   r   	nodes_posspradius_pupuvpvedgesr   coordskdtreeedge_indexess     `             r   r   r   Y   s    
 X&I
 i)MEZZ'F%%fa0L.4\.BC.Bdaeh!.BECL  19 %1A$>
$> !;s2{;;xG QF$>
 

  Ds)   A= $C+= C(-CCC('C(   T)graphsreturns_graphc          
      (   [         R                  " U 5      nUc;  U VV	s0 s H,  o[        U5       V	s/ s H  oR                  5       PM     sn	_M.     nnn	[         R                  " XsU5        UR                  [        XqXF5      5        U$ s  sn	f s  sn	nf )u	  Returns a random geometric graph in the unit cube of dimensions `dim`.

The random geometric graph model places `n` nodes uniformly at
random in the unit cube. Two nodes are joined by an edge if the
distance between the nodes is at most `radius`.

Edges are determined using a KDTree when SciPy is available.
This reduces the time complexity from $O(n^2)$ to $O(n)$.

Parameters
----------
n : int or iterable
    Number of nodes or iterable of nodes
radius: float
    Distance threshold value
dim : int, optional
    Dimension of graph
pos : dict, optional
    A dictionary keyed by node with node positions as values.
p : float, optional
    Which Minkowski distance metric to use.  `p` has to meet the condition
    ``1 <= p <= infinity``.

    If this argument is not specified, the :math:`L^2` metric
    (the Euclidean distance metric), p = 2 is used.
    This should not be confused with the `p` of an Erdős-Rényi random
    graph, which represents probability.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position
    in 2D coordinates of the node in the returned graph.

Returns
-------
Graph
    A random geometric graph, undirected and without self-loops.
    Each node has a node attribute ``'pos'`` that stores the
    position of that node in Euclidean space as provided by the
    ``pos`` keyword argument or, if ``pos`` was not provided, as
    generated by this function.

Examples
--------
Create a random geometric graph on twenty nodes where nodes are joined by
an edge if their distance is at most 0.1::

>>> G = nx.random_geometric_graph(20, 0.1)

Notes
-----
This uses a *k*-d tree to build the graph.

The `pos` keyword argument can be used to specify node positions so you
can create an arbitrary distribution and domain for positions.

For example, to use a 2D Gaussian distribution of node positions with mean
(0, 0) and standard deviation 2::

>>> import random
>>> n = 20
>>> pos = {i: (random.gauss(0, 2), random.gauss(0, 2)) for i in range(n)}
>>> G = nx.random_geometric_graph(n, 0.2, pos=pos)

References
----------
.. [1] Penrose, Mathew, *Random Geometric Graphs*,
       Oxford Studies in Probability, 5, 2003.

)r   empty_graphrangerandomset_node_attributesadd_edges_fromr   )
r   r   dimr   r   seedr   r   r;   is
             r   r   r   r   s{    f 	qA {?@Aq!%*5*Q;;=*55qA18,%a=>H	 6As   BB	B	B   c                  ^^^^ [         R                  " U 5      nSU  SU SU S3Ul        Tc=  U V	V
s0 s H-  o[        U5       V
s/ s H  n
TR	                  5       PM     sn
_M/     sn
n	m[         R
                  " UTU5        Tc  S mUUUU4S jnUR                  [        U[        XTU5      5      5        U$ s  sn
f s  sn
n	f )u  Returns a soft random geometric graph in the unit cube.

The soft random geometric graph [1] model places `n` nodes uniformly at
random in the unit cube in dimension `dim`. Two nodes of distance, `dist`,
computed by the `p`-Minkowski distance metric are joined by an edge with
probability `p_dist` if the computed distance metric value of the nodes
is at most `radius`, otherwise they are not joined.

Edges within `radius` of each other are determined using a KDTree when
SciPy is available. This reduces the time complexity from :math:`O(n^2)`
to :math:`O(n)`.

Parameters
----------
n : int or iterable
    Number of nodes or iterable of nodes
radius: float
    Distance threshold value
dim : int, optional
    Dimension of graph
pos : dict, optional
    A dictionary keyed by node with node positions as values.
p : float, optional
    Which Minkowski distance metric to use.
    `p` has to meet the condition ``1 <= p <= infinity``.

    If this argument is not specified, the :math:`L^2` metric
    (the Euclidean distance metric), p = 2 is used.

    This should not be confused with the `p` of an Erdős-Rényi random
    graph, which represents probability.
p_dist : function, optional
    A probability density function computing the probability of
    connecting two nodes that are of distance, dist, computed by the
    Minkowski distance metric. The probability density function, `p_dist`,
    must be any function that takes the metric value as input
    and outputs a single probability value between 0-1. The scipy.stats
    package has many probability distribution functions implemented and
    tools for custom probability distribution definitions [2], and passing
    the .pdf method of scipy.stats distributions can be used here.  If the
    probability function, `p_dist`, is not supplied, the default function
    is an exponential distribution with rate parameter :math:`\lambda=1`.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position
    in 2D coordinates of the node in the returned graph.

Returns
-------
Graph
    A soft random geometric graph, undirected and without self-loops.
    Each node has a node attribute ``'pos'`` that stores the
    position of that node in Euclidean space as provided by the
    ``pos`` keyword argument or, if ``pos`` was not provided, as
    generated by this function.

Examples
--------
Default Graph:

G = nx.soft_random_geometric_graph(50, 0.2)

Custom Graph:

Create a soft random geometric graph on 100 uniformly distributed nodes
where nodes are joined by an edge with probability computed from an
exponential distribution with rate parameter :math:`\lambda=1` if their
Euclidean distance is at most 0.2.

Notes
-----
This uses a *k*-d tree to build the graph.

The `pos` keyword argument can be used to specify node positions so you
can create an arbitrary distribution and domain for positions.

For example, to use a 2D Gaussian distribution of node positions with mean
(0, 0) and standard deviation 2

The scipy.stats package can be used to define the probability distribution
with the .pdf method used as `p_dist`.

::

>>> import random
>>> import math
>>> n = 100
>>> pos = {i: (random.gauss(0, 2), random.gauss(0, 2)) for i in range(n)}
>>> p_dist = lambda dist: math.exp(-dist)
>>> G = nx.soft_random_geometric_graph(n, 0.2, pos=pos, p_dist=p_dist)

References
----------
.. [1] Penrose, Mathew D. "Connectivity of soft random geometric graphs."
       The Annals of Applied Probability 26.2 (2016): 986-1028.
.. [2] scipy.stats -
       https://docs.scipy.org/doc/scipy/reference/tutorial/stats.html

zsoft_random_geometric_graph(, )c                 0    [         R                  " U * 5      $ r#   )mathexp)dists    r   p_dist+soft_random_geometric_graph.<locals>.p_distF  s    88TE?"r   c                    > U u  p[        U4S j[        TU   TU   5       5       5      ST-  -  nTR                  5       T" U5      :  $ )Nc              3   H   >#    U  H  u  p[        X-
  5      T-  v   M     g 7fr#   r$   r&   s      r   r*   Csoft_random_geometric_graph.<locals>.should_join.<locals>.<genexpr>K  s     D0CCJ!O0Cr,      )r/   r0   rG   )edger9   r;   rT   r   rU   r   rK   s       r   should_join0soft_random_geometric_graph.<locals>.should_joinI  sG    DCFCF0CDD!a%P{{}vd|++r   )	r   rE   namerF   rG   rH   rI   filterr   )r   r   rJ   r   r   rU   rK   r   r   r;   rL   r\   s      ````     r   r   r      s    T 	qA+A3b3%qAAF {?@Aq!%*5*Q4;;=*55qA1c8, ~	#, ,
 VK)9!Q)QRSH! 6As   C B;!C ;C    weight)r   weight_namec          
        ^^^^^ [         R                  " U 5      n
Tc"  U
 Vs0 s H  oUR                  S5      _M     snmTc<  U
 VVs0 s H,  o[        U5       Vs/ s H  oR	                  5       PM     sn_M.     snnmTc  [
        R                  m[         R                  " U
TU	5        [         R                  " U
TU5        Tc  S mUUUUU4S jnU
R                  [        U[        U
S5      5      5        U
$ s  snf s  snf s  snnf )u  Returns a geographical threshold graph.

The geographical threshold graph model places $n$ nodes uniformly at
random in a rectangular domain.  Each node $u$ is assigned a weight
$w_u$. Two nodes $u$ and $v$ are joined by an edge if

.. math::

   (w_u + w_v)p_{dist}(r) \ge \theta

where `r` is the distance between `u` and `v`, `p_dist` is any function of
`r`, and :math:`\theta` as the threshold parameter. `p_dist` is used to
give weight to the distance between nodes when deciding whether or not
they should be connected. The larger `p_dist` is, the more prone nodes
separated by `r` are to be connected, and vice versa.

Parameters
----------
n : int or iterable
    Number of nodes or iterable of nodes
theta: float
    Threshold value
dim : int, optional
    Dimension of graph
pos : dict
    Node positions as a dictionary of tuples keyed by node.
weight : dict
    Node weights as a dictionary of numbers keyed by node.
metric : function
    A metric on vectors of numbers (represented as lists or
    tuples). This must be a function that accepts two lists (or
    tuples) as input and yields a number as output. The function
    must also satisfy the four requirements of a `metric`_.
    Specifically, if $d$ is the function and $x$, $y$,
    and $z$ are vectors in the graph, then $d$ must satisfy

    1. $d(x, y) \ge 0$,
    2. $d(x, y) = 0$ if and only if $x = y$,
    3. $d(x, y) = d(y, x)$,
    4. $d(x, z) \le d(x, y) + d(y, z)$.

    If this argument is not specified, the Euclidean distance metric is
    used.

    .. _metric: https://en.wikipedia.org/wiki/Metric_%28mathematics%29
p_dist : function, optional
    Any function used to give weight to the distance between nodes when
    deciding whether or not they should be connected. `p_dist` was
    originally conceived as a probability density function giving the
    probability of connecting two nodes that are of metric distance `r`
    apart. The implementation here allows for more arbitrary definitions
    of `p_dist` that do not need to correspond to valid probability
    density functions. The :mod:`scipy.stats` package has many
    probability density functions implemented and tools for custom
    probability density definitions, and passing the ``.pdf`` method of
    scipy.stats distributions can be used here. If ``p_dist=None``
    (the default), the exponential function :math:`r^{-2}` is used.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position
    in 2D coordinates of the node in the returned graph.
weight_name : string, default="weight"
    The name of the node attribute which represents the weight
    of the node in the returned graph.

Returns
-------
Graph
    A random geographic threshold graph, undirected and without
    self-loops.

    Each node has a node attribute ``pos`` that stores the
    position of that node in Euclidean space as provided by the
    ``pos`` keyword argument or, if ``pos`` was not provided, as
    generated by this function. Similarly, each node has a node
    attribute ``weight`` that stores the weight of that node as
    provided or as generated.

Examples
--------
Specify an alternate distance metric using the ``metric`` keyword
argument. For example, to use the `taxicab metric`_ instead of the
default `Euclidean metric`_::

    >>> dist = lambda x, y: sum(abs(a - b) for a, b in zip(x, y))
    >>> G = nx.geographical_threshold_graph(10, 0.1, metric=dist)

.. _taxicab metric: https://en.wikipedia.org/wiki/Taxicab_geometry
.. _Euclidean metric: https://en.wikipedia.org/wiki/Euclidean_distance

Notes
-----
If weights are not specified they are assigned to nodes by drawing randomly
from the exponential distribution with rate parameter $\lambda=1$.
To specify weights from a different distribution, use the `weight` keyword
argument::

>>> import random
>>> n = 20
>>> w = {i: random.expovariate(5.0) for i in range(n)}
>>> G = nx.geographical_threshold_graph(20, 50, weight=w)

If node positions are not specified they are randomly assigned from the
uniform distribution.

References
----------
.. [1] Masuda, N., Miwa, H., Konno, N.:
   Geographical threshold graphs with small-world and scale-free
   properties.
   Physical Review E 71, 036108 (2005)
.. [2]  Milan Bradonjić, Aric Hagberg and Allon G. Percus,
   Giant component and connectivity in geographical threshold graphs,
   in Algorithms and Models for the Web-Graph (WAW 2007),
   Antony Bonato and Fan Chung (Eds), pp. 209--216, 2007
rZ   c                     U S-  $ )N )rs    r   rU   ,geographical_threshold_graph.<locals>.p_dist  s    b5Lr   c                 \   > U u  pT	U   T	U   pCTU   TU   peXV-   T" T" X45      5      -  T
:  $ r#   rf   )pairr9   r;   u_posv_posu_weightv_weightmetricrU   r   thetara   s          r   r\   1geographical_threshold_graph.<locals>.should_join  sE    1vs1vu#AYq	(#vfU.B'CCuLLr   r!   )r   rE   expovariaterF   rG   rR   rT   rH   rI   r_   r   )r   rp   rJ   r   ra   ro   rU   rK   r   rb   r   r;   rL   r\   s    ` ````       r   r	   r	   R  s    J 	qA ~234!QT%%a((!4 {?@Aq!%*5*Q;;=*55qA~1fk21c8, ~	M M VKa);<=H7 5 6As   C:	DC?5D?Dc          	        ^^^^^^^ [         R                  " U 5      nUu  ppU Vs0 s H%  oTR                  X5      TR                  X5      4_M'     snm[         R                  " UTU5        Tc  [        R
                  mTc5  [        U4S j[        TR                  5       S5       5       5      mUU4S jmOUU4S jmUUUUU4S jnUR                  [        U[        US5      5      5        U$ s  snf )a  Returns a Waxman random graph.

The Waxman random graph model places `n` nodes uniformly at random
in a rectangular domain. Each pair of nodes at distance `d` is
joined by an edge with probability

.. math::
        p = \beta \exp(-d / \alpha L).

This function implements both Waxman models, using the `L` keyword
argument.

* Waxman-1: if `L` is not specified, it is set to be the maximum distance
  between any pair of nodes.
* Waxman-2: if `L` is specified, the distance between a pair of nodes is
  chosen uniformly at random from the interval `[0, L]`.

Parameters
----------
n : int or iterable
    Number of nodes or iterable of nodes
beta: float
    Model parameter
alpha: float
    Model parameter
L : float, optional
    Maximum distance between nodes.  If not specified, the actual distance
    is calculated.
domain : four-tuple of numbers, optional
    Domain size, given as a tuple of the form `(x_min, y_min, x_max,
    y_max)`.
metric : function
    A metric on vectors of numbers (represented as lists or
    tuples). This must be a function that accepts two lists (or
    tuples) as input and yields a number as output. The function
    must also satisfy the four requirements of a `metric`_.
    Specifically, if $d$ is the function and $x$, $y$,
    and $z$ are vectors in the graph, then $d$ must satisfy

    1. $d(x, y) \ge 0$,
    2. $d(x, y) = 0$ if and only if $x = y$,
    3. $d(x, y) = d(y, x)$,
    4. $d(x, z) \le d(x, y) + d(y, z)$.

    If this argument is not specified, the Euclidean distance metric is
    used.

    .. _metric: https://en.wikipedia.org/wiki/Metric_%28mathematics%29

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position
    in 2D coordinates of the node in the returned graph.

Returns
-------
Graph
    A random Waxman graph, undirected and without self-loops. Each
    node has a node attribute ``'pos'`` that stores the position of
    that node in Euclidean space as generated by this function.

Examples
--------
Specify an alternate distance metric using the ``metric`` keyword
argument. For example, to use the "`taxicab metric`_" instead of the
default `Euclidean metric`_::

    >>> dist = lambda x, y: sum(abs(a - b) for a, b in zip(x, y))
    >>> G = nx.waxman_graph(10, 0.5, 0.1, metric=dist)

.. _taxicab metric: https://en.wikipedia.org/wiki/Taxicab_geometry
.. _Euclidean metric: https://en.wikipedia.org/wiki/Euclidean_distance

Notes
-----
Starting in NetworkX 2.0 the parameters alpha and beta align with their
usual roles in the probability distribution. In earlier versions their
positions in the expression were reversed. Their position in the calling
sequence reversed as well to minimize backward incompatibility.

References
----------
.. [1]  B. M. Waxman, *Routing of multipoint connections*.
   IEEE J. Select. Areas Commun. 6(9),(1988) 1617--1622.
c              3   8   >#    U  H  u  pT" X5      v   M     g 7fr#   rf   )r'   xyro   s      r   r*   waxman_graph.<locals>.<genexpr>l  s     G)Fq)Fs   r!   c                 "   > T" TU    TU   5      $ r#   rf   )r9   r;   ro   r   s     r   rT   waxman_graph.<locals>.distn  s    #a&#a&))r   c                 *   > TR                  5       T-  $ r#   )rG   )r9   r;   LrK   s     r   rT   ry   s  s    ;;=1$$r   c                 l   > TR                  5       T[        R                  " T" U 6 * TT-  -  5      -  :  $ r#   )rG   rR   rS   )rj   r{   alphabetarT   rK   s    r   r\   !waxman_graph.<locals>.should_joinw  s1    {{}tdhhd|uqy/I&JJJJr   )r   rE   uniformrH   rR   rT   maxr   valuesrI   r_   )r   r~   r}   r{   domainro   rK   r   r   xminyminxmaxymaxr;   r\   rT   r   s    ``` ``        @@r   r   r     s    H 	qA%TLM
NAqt||D'd)ABBA
NC1c8,~ 	yGcjjlA)FGG	*
	%K K VKa);<=H7 Os   ,C1c                    US:  a  [         R                  " S5      eUS:  a  [         R                  " S5      eUS:  a  [         R                  " S5      e[         R                  " 5       n[        [	        [        U 5      US95      nU H  nS/n	U HO  n
X:X  a  M
  [        S [        X5       5       5      nX::  a  UR                  X5        U	R                  X* -  5        MQ     [        [        U	5      5      n[        U5       H5  nU[        XR                  SUS   5      5         nUR                  X5        M7     M     U$ )	a  Returns a navigable small-world graph.

A navigable small-world graph is a directed grid with additional long-range
connections that are chosen randomly.

  [...] we begin with a set of nodes [...] that are identified with the set
  of lattice points in an $n \times n$ square,
  $\{(i, j): i \in \{1, 2, \ldots, n\}, j \in \{1, 2, \ldots, n\}\}$,
  and we define the *lattice distance* between two nodes $(i, j)$ and
  $(k, l)$ to be the number of "lattice steps" separating them:
  $d((i, j), (k, l)) = |k - i| + |l - j|$.

  For a universal constant $p >= 1$, the node $u$ has a directed edge to
  every other node within lattice distance $p$---these are its *local
  contacts*. For universal constants $q >= 0$ and $r >= 0$ we also
  construct directed edges from $u$ to $q$ other nodes (the *long-range
  contacts*) using independent random trials; the $i$th directed edge from
  $u$ has endpoint $v$ with probability proportional to $[d(u,v)]^{-r}$.

  -- [1]_

Parameters
----------
n : int
    The length of one side of the lattice; the number of nodes in
    the graph is therefore $n^2$.
p : int
    The diameter of short range connections. Each node is joined with every
    other node within this lattice distance.
q : int
    The number of long-range connections for each node.
r : float
    Exponent for decaying probability of connections.  The probability of
    connecting to a node at lattice distance $d$ is $1/d^r$.
dim : int
    Dimension of grid
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

References
----------
.. [1] J. Kleinberg. The small-world phenomenon: An algorithmic
   perspective. Proc. 32nd ACM Symposium on Theory of Computing, 2000.
rZ   zp must be >= 1r   zq must be >= 0zr must be >= 0)repeatc              3   @   #    U  H  u  p[        X!-
  5      v   M     g 7fr#   r$   )r'   r(   r)   s      r   r*   .navigable_small_world_graph.<locals>.<genexpr>  s     8KDASZZKs   )r   NetworkXExceptionDiGraphr1   r   rF   r/   r0   add_edgeappendr   r   r   )r   r   qrg   rJ   rK   r   r   p1probsp2dcdf_targets                  r   r
   r
   ~  s   ` 	1u""#3441u""#3441u""#344


Aq#./EBx8CK89Av

2"LLB  :e$%qA;sLLCG,DEFFJJr"   Hr   c          
        ^^ [         R                  " U 5      n
SU  SU ST SU S3	U
l        Tc"  U
 Vs0 s H  oUR                  S5      _M     snmUc;  U
 VVs0 s H,  o[	        U5       Vs/ s H  oR                  5       PM     sn_M.     nnn[         R                  " U
TU	5        [         R                  " XU5        UU4S j[        XXh5       5       nU
R                  U5        U
$ s  snf s  snf s  snnf )u  Returns a thresholded random geometric graph in the unit cube.

The thresholded random geometric graph [1] model places `n` nodes
uniformly at random in the unit cube of dimensions `dim`. Each node
`u` is assigned a weight :math:`w_u`. Two nodes `u` and `v` are
joined by an edge if they are within the maximum connection distance,
`radius` computed by the `p`-Minkowski distance and the summation of
weights :math:`w_u` + :math:`w_v` is greater than or equal
to the threshold parameter `theta`.

Edges within `radius` of each other are determined using a KDTree when
SciPy is available. This reduces the time complexity from :math:`O(n^2)`
to :math:`O(n)`.

Parameters
----------
n : int or iterable
    Number of nodes or iterable of nodes
radius: float
    Distance threshold value
theta: float
    Threshold value
dim : int, optional
    Dimension of graph
pos : dict, optional
    A dictionary keyed by node with node positions as values.
weight : dict, optional
    Node weights as a dictionary of numbers keyed by node.
p : float, optional (default 2)
    Which Minkowski distance metric to use.  `p` has to meet the condition
    ``1 <= p <= infinity``.

    If this argument is not specified, the :math:`L^2` metric
    (the Euclidean distance metric), p = 2 is used.

    This should not be confused with the `p` of an Erdős-Rényi random
    graph, which represents probability.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
pos_name : string, default="pos"
    The name of the node attribute which represents the position
    in 2D coordinates of the node in the returned graph.
weight_name : string, default="weight"
    The name of the node attribute which represents the weight
    of the node in the returned graph.

Returns
-------
Graph
    A thresholded random geographic graph, undirected and without
    self-loops.

    Each node has a node attribute ``'pos'`` that stores the
    position of that node in Euclidean space as provided by the
    ``pos`` keyword argument or, if ``pos`` was not provided, as
    generated by this function. Similarly, each node has a nodethre
    attribute ``'weight'`` that stores the weight of that node as
    provided or as generated.

Examples
--------
Default Graph:

G = nx.thresholded_random_geometric_graph(50, 0.2, 0.1)

Custom Graph:

Create a thresholded random geometric graph on 50 uniformly distributed
nodes where nodes are joined by an edge if their sum weights drawn from
a exponential distribution with rate = 5 are >= theta = 0.1 and their
Euclidean distance is at most 0.2.

Notes
-----
This uses a *k*-d tree to build the graph.

The `pos` keyword argument can be used to specify node positions so you
can create an arbitrary distribution and domain for positions.

For example, to use a 2D Gaussian distribution of node positions with mean
(0, 0) and standard deviation 2

If weights are not specified they are assigned to nodes by drawing randomly
from the exponential distribution with rate parameter :math:`\lambda=1`.
To specify weights from a different distribution, use the `weight` keyword
argument::

::

>>> import random
>>> import math
>>> n = 50
>>> pos = {i: (random.gauss(0, 2), random.gauss(0, 2)) for i in range(n)}
>>> w = {i: random.expovariate(5.0) for i in range(n)}
>>> G = nx.thresholded_random_geometric_graph(n, 0.2, 0.1, 2, pos, w)

References
----------
.. [1] http://cole-maclean.github.io/blog/files/thesis.pdf

z#thresholded_random_geometric_graph(rO   rP   rZ   c              3   P   >#    U  H  u  pTU   TU   -   T:  d  M  X4v   M     g 7fr#   rf   )r'   r9   r;   rp   ra   s      r   r*   5thresholded_random_geometric_graph.<locals>.<genexpr>J  s3      <DA!9vay E) 	<s   &
&)	r   rE   r^   rr   rF   rG   rH   r   rI   )r   r   rp   rJ   r   ra   r   rK   r   rb   r   r;   rL   r=   s     `  `        r   r   r     s    j 	qA21#Rxr%3%qQAF ~234!QT%%a((!4 {?@Aq!%*5*Q;;=*55qA1fk218,$Q<E
 UH 5 6As   C)C3-C.C3.C3)r   gammamean_degreekappasrK   c                    U S::  a  [         R                  " S5      eUbR  [        USL USL USL 45      (       d  [         R                  " S5      e[        U5      n[	        U5      [        U5      -  nO[        USL USL USL 45      (       a  [         R                  " S5      eUS-
  US-
  -  nX6-  SSU-  -
  -  SSX-  -  -
  -  nSSU-  -
  nSSU-
  -  n	[        U5       V
s0 s H  oUSUR                  5       U-  -
  U	-  -  _M!     nn
[         R                  " 5       nUS[        R                  -  -  nU S:  aB  U [        R                  " [        R                  U -  5      -  S[        R                  -  U-  -  nO=U S:X  a   SSU-  [        R                  " U5      -  -  nOSU -
  SU -  U-  USU -
  -  -  -  nU Vs0 s H&  oUR                  SS[        R                  -  5      _M(     nnU H  n[        U5       H  n[        R                  [        R                  " [        R                  [        R                  " UU   UU   -
  5      -
  5      -
  n[        R                   " UU-  U 5      n[        R                   " XU   -  UU   -  [#        SU 5      5      nSSUU-  -   -  nUR                  5       U:  d  M  UR%                  UU5        M     UR'                  U5        M     [         R(                  " XS5        [         R(                  " XS	5        U S:  a  SOSU -  n[+        UR-                  5       5      nS[#        SU 5      -  U U-  -  nSU-  [        R                  " U[        R                  -  5      -  U[        R                  " UU-  5      -  -
  nUR/                  5        VVs0 s H#  u  nnUUU[        R                  " U5      -  -
  _M%     nnn[         R(                  " UUS
5        U$ s  sn
f s  snf s  snnf )u}  Returns a random graph from the geometric soft configuration model.

The $\mathbb{S}^1$ model [1]_ is the geometric soft configuration model
which is able to explain many fundamental features of real networks such as
small-world property, heteregenous degree distributions, high level of
clustering, and self-similarity.

In the geometric soft configuration model, a node $i$ is assigned two hidden
variables: a hidden degree $\kappa_i$, quantifying its popularity, influence,
or importance, and an angular position $\theta_i$ in a circle abstracting the
similarity space, where angular distances between nodes are a proxy for their
similarity. Focusing on the angular position, this model is often called
the $\mathbb{S}^1$ model (a one-dimensional sphere). The circle's radius is
adjusted to $R = N/2\pi$, where $N$ is the number of nodes, so that the density
is set to 1 without loss of generality.

The connection probability between any pair of nodes increases with
the product of their hidden degrees (i.e., their combined popularities),
and decreases with the angular distance between the two nodes.
Specifically, nodes $i$ and $j$ are connected with the probability

$p_{ij} = \frac{1}{1 + \frac{d_{ij}^\beta}{\left(\mu \kappa_i \kappa_j\right)^{\max(1, \beta)}}}$

where $d_{ij} = R\Delta\theta_{ij}$ is the arc length of the circle between
nodes $i$ and $j$ separated by an angular distance $\Delta\theta_{ij}$.
Parameters $\mu$ and $\beta$ (also called inverse temperature) control the
average degree and the clustering coefficient, respectively.

It can be shown [2]_ that the model undergoes a structural phase transition
at $\beta=1$ so that for $\beta<1$ networks are unclustered in the thermodynamic
limit (when $N\to \infty$) whereas for $\beta>1$ the ensemble generates
networks with finite clustering coefficient.

The $\mathbb{S}^1$ model can be expressed as a purely geometric model
$\mathbb{H}^2$ in the hyperbolic plane [3]_ by mapping the hidden degree of
each node into a radial coordinate as

$r_i = \hat{R} - \frac{2 \max(1, \beta)}{\beta \zeta} \ln \left(\frac{\kappa_i}{\kappa_0}\right)$

where $\hat{R}$ is the radius of the hyperbolic disk and $\zeta$ is the curvature,

$\hat{R} = \frac{2}{\zeta} \ln \left(\frac{N}{\pi}\right)
- \frac{2\max(1, \beta)}{\beta \zeta} \ln (\mu \kappa_0^2)$

The connection probability then reads

$p_{ij} = \frac{1}{1 + \exp\left({\frac{\beta\zeta}{2} (x_{ij} - \hat{R})}\right)}$

where

$x_{ij} = r_i + r_j + \frac{2}{\zeta} \ln \frac{\Delta\theta_{ij}}{2}$

is a good approximation of the hyperbolic distance between two nodes separated
by an angular distance $\Delta\theta_{ij}$ with radial coordinates $r_i$ and $r_j$.
For $\beta > 1$, the curvature $\zeta = 1$, for $\beta < 1$, $\zeta = \beta^{-1}$.


Parameters
----------
Either `n`, `gamma`, `mean_degree` are provided or `kappas`. The values of
`n`, `gamma`, `mean_degree` (if provided) are used to construct a random
kappa-dict keyed by node with values sampled from a power-law distribution.

beta : positive number
    Inverse temperature, controlling the clustering coefficient.
n : int (default: None)
    Size of the network (number of nodes).
    If not provided, `kappas` must be provided and holds the nodes.
gamma : float (default: None)
    Exponent of the power-law distribution for hidden degrees `kappas`.
    If not provided, `kappas` must be provided directly.
mean_degree : float (default: None)
    The mean degree in the network.
    If not provided, `kappas` must be provided directly.
kappas : dict (default: None)
    A dict keyed by node to its hidden degree value.
    If not provided, random values are computed based on a power-law
    distribution using `n`, `gamma` and `mean_degree`.
seed : int, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
Graph
    A random geometric soft configuration graph (undirected with no self-loops).
    Each node has three node-attributes:

    - ``kappa`` that represents the hidden degree.

    - ``theta`` the position in the similarity space ($\mathbb{S}^1$) which is
      also the angular position in the hyperbolic plane.

    - ``radius`` the radial position in the hyperbolic plane
      (based on the hidden degree).


Examples
--------
Generate a network with specified parameters:

>>> G = nx.geometric_soft_configuration_graph(
...     beta=1.5, n=100, gamma=2.7, mean_degree=5
... )

Create a geometric soft configuration graph with 100 nodes. The $\beta$ parameter
is set to 1.5 and the exponent of the powerlaw distribution of the hidden
degrees is 2.7 with mean value of 5.

Generate a network with predefined hidden degrees:

>>> kappas = {i: 10 for i in range(100)}
>>> G = nx.geometric_soft_configuration_graph(beta=2.5, kappas=kappas)

Create a geometric soft configuration graph with 100 nodes. The $\beta$ parameter
is set to 2.5 and all nodes with hidden degree $\kappa=10$.


References
----------
.. [1] Serrano, M. Á., Krioukov, D., & Boguñá, M. (2008). Self-similarity
   of complex networks and hidden metric spaces. Physical review letters, 100(7), 078701.

.. [2] van der Kolk, J., Serrano, M. Á., & Boguñá, M. (2022). An anomalous
   topological phase transition in spatial random graphs. Communications Physics, 5(1), 245.

.. [3] Krioukov, D., Papadopoulos, F., Kitsak, M., Vahdat, A., & Boguná, M. (2010).
   Hyperbolic geometry of complex networks. Physical Review E, 82(3), 036106.

r   z3The parameter beta cannot be smaller or equal to 0.Nz;When kappas is input, n, gamma and mean_degree must not be.zDPlease provide either kappas, or all 3 of: n, gamma and mean_degree.r!   rZ   rp   kappar   )r   r   alllenr/   anyrF   rG   GraphrR   pisinlogr   r1   fabspowr   r   add_noderH   minr   items)r~   r   r   r   r   rK   	gam_ratiokappa_0basepowerrL   r   Rmukthetasr9   r;   angledij	mu_kappasp_ijzeta	kappa_minR_cR_hatnoder   radiis                                r   r   r   S  s   N qyTUUAIu}kT.ABCC""M  K&kCK/T	5D=+*=>??""V  QY519-	)QQY71q1<?O;OP1q5yQYLQRSHUHqWDKKMD$8 8UBBBHU

A	Q[A axDHHTWWt^,,DGGk0IJ	!k/DHHQK/0$h1d7[01T?BC 8>>v!aTWW--vF>aAGGdii$))F1Iq	<Q2R(RSSE((1u9d+CQi&)!;SD\JIC)O+,D {{}t#

1a   	


1  1g.1g. q1a$hDFMMO$I
c!Tl
dTk
*CX!dgg+..txxY7O1OOEDJLLNSN[T5T53%000NES1eX.HO V ?, Ts   !&O3=-O8,*O=)r!   )r!   Nr!   N)r!   Nr!   NN)r!   NNNNN)g?g?N)r   r   rZ   rZ   NN)rZ   rZ   r!   r!   N)r!   NNr!   N)__doc__rR   bisectr   	itertoolsr   r   r   networkxr   networkx.utilsr   __all___dispatchabler   r   r   r   r	   r   r
   r   r   rf   r   r   <module>r      s&   &   7 7  *	 Z(?4 ?4 )?4D2 T2*.Y<AY 3 Yx T27;}IN} 3 }@ T2 	
	b b 3 bJ T2 


	@ @ 3 @F T2D 3 DN T2
 	
	G G 3 GT T24T$TC 3 Cr   