
    \Mh|8                        d Z ddlZddlZg dZ ej        dd          dd            Z ej        dd          dd            Z ej        dd          dd            Zej	        j
                            d	           ej        dd          dd
ddd                        Zej	                            d          ej	                            d           ej        dddii          ddd                                    Zej	        j
                            d	           ej        dd          ddd
ddd                        ZdS )z3Provides explicit constructions of expander graphs.    N)margulis_gabber_galil_graphchordal_cycle_graphpaley_graphmaybe_regular_expanderis_regular_expanderrandom_regular_expander_graphT)graphsreturns_graphc                    t          j        d|t           j                  }|                                s|                                sd}t          j        |          t          j        t          |           d          D ]]\  }}|d|z  z   | z  |f|d|z  dz   z   | z  |f||d|z  z   | z  f||d|z  dz   z   | z  ffD ]\  }}|	                    ||f||f            ^d|  d|j
        d	<   |S )
a  Returns the Margulis-Gabber-Galil undirected MultiGraph on `n^2` nodes.

    The undirected MultiGraph is regular with degree `8`. Nodes are integer
    pairs. The second-largest eigenvalue of the adjacency matrix of the graph
    is at most `5 \sqrt{2}`, regardless of `n`.

    Parameters
    ----------
    n : int
        Determines the number of nodes in the graph: `n^2`.
    create_using : NetworkX graph constructor, optional (default MultiGraph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    G : graph
        The constructed undirected multigraph.

    Raises
    ------
    NetworkXError
        If the graph is directed or not a multigraph.

    r   default0`create_using` must be an undirected multigraph.   )repeat   zmargulis_gabber_galil_graph()name)nxempty_graph
MultiGraphis_directedis_multigraphNetworkXError	itertoolsproductrangeadd_edgegraph)ncreate_usingGmsgxyuvs           ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/generators/expanders.pyr   r   1   s.   4 	q,>>>A}} $aoo// $@s###!%((1555 ' '1!a%i1_a 1q519o"A&QUa a!eaiA%&	
 	' 	'DAq JJ1v1v&&&&	' :Q999AGFOH    c                    t          j        d|t           j                  }|                                s|                                sd}t          j        |          t          |           D ]L}|dz
  | z  }|dz   | z  }|dk    rt          || dz
  |           nd}|||fD ]}|                    ||           Md|  d|j	        d<   |S )	u  Returns the chordal cycle graph on `p` nodes.

    The returned graph is a cycle graph on `p` nodes with chords joining each
    vertex `x` to its inverse modulo `p`. This graph is a (mildly explicit)
    3-regular expander [1]_.

    `p` *must* be a prime number.

    Parameters
    ----------
    p : a prime number

        The number of vertices in the graph. This also indicates where the
        chordal edges in the cycle will be created.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    G : graph
        The constructed undirected multigraph.

    Raises
    ------
    NetworkXError

        If `create_using` indicates directed or not a multigraph.

    References
    ----------

    .. [1] Theorem 4.4.2 in A. Lubotzky. "Discrete groups, expanding graphs and
           invariant measures", volume 125 of Progress in Mathematics.
           Birkhäuser Verlag, Basel, 1994.

    r   r   r   r   r   zchordal_cycle_graph(r   r   )
r   r   r   r   r   r   r   powr   r   )	pr    r!   r"   r#   leftrightchordr$   s	            r'   r   r   \   s    N 	q,>>>A}} $aoo// $@s###1XX  A{Q! %&EEAq1ua   qu% 	 	AJJq!	1Q111AGFOHr(   c                 X    t          j        d|t           j                  }|                                rd}t          j        |           fdt          d           D             }t                     D ]#}|D ]}|                    |||z    z             $d  d|j        d<   |S )	a-  Returns the Paley $\frac{(p-1)}{2}$ -regular graph on $p$ nodes.

    The returned graph is a graph on $\mathbb{Z}/p\mathbb{Z}$ with edges between $x$ and $y$
    if and only if $x-y$ is a nonzero square in $\mathbb{Z}/p\mathbb{Z}$.

    If $p \equiv 1  \pmod 4$, $-1$ is a square in
    $\mathbb{Z}/p\mathbb{Z}$ and therefore $x-y$ is a square if and
    only if $y-x$ is also a square, i.e the edges in the Paley graph are symmetric.

    If $p \equiv 3 \pmod 4$, $-1$ is not a square in $\mathbb{Z}/p\mathbb{Z}$
    and therefore either $x-y$ or $y-x$ is a square in $\mathbb{Z}/p\mathbb{Z}$ but not both.

    Note that a more general definition of Paley graphs extends this construction
    to graphs over $q=p^n$ vertices, by using the finite field $F_q$ instead of
    $\mathbb{Z}/p\mathbb{Z}$.
    This construction requires to compute squares in general finite fields and is
    not what is implemented here (i.e `paley_graph(25)` does not return the true
    Paley graph associated with $5^2$).

    Parameters
    ----------
    p : int, an odd prime number.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    G : graph
        The constructed directed graph.

    Raises
    ------
    NetworkXError
        If the graph is a multigraph.

    References
    ----------
    Chapter 13 in B. Bollobas, Random Graphs. Second edition.
    Cambridge Studies in Advanced Mathematics, 73.
    Cambridge University Press, Cambridge (2001).
    r   r   z&`create_using` cannot be a multigraph.c                 8    h | ]}|d z  z  dk    |d z  z  S )r   r    ).0r#   r+   s     r'   	<setcomp>zpaley_graph.<locals>.<setcomp>   s.    EEEadaZ1__1a41*___r(   r   zpaley(r   r   )r   r   DiGraphr   r   r   r   r   )r+   r    r!   r"   
square_setr#   x2s   `      r'   r   r      s    X 	q,
;;;A $6s###
 FEEEeAqkkEEEJ1XX ( ( 	( 	(BJJq1r6Q,''''	(#qmmmAGFOHr(   seedd   r    	max_triesr7   c                   ddl }| dk     rt          j        d          |dk    st          j        d          |dz  dk    st          j        d          | dz
  |k    st          j        d|dz   d	|  d
          t          j        | |          }| dk     r|S g }t	                      t          |dz            D ]}|}	t                    |dz   | z  k    r|	dz  }	|                    | dz
                                            }
|
	                    | dz
             fdt          j
                            |
d          D             }t          |          | k    r*|	                    |
                               |           |	dk    rt          j        d          t                    |dz   | z  k    |                               |S )a  Utility for creating a random regular expander.

    Returns a random $d$-regular graph on $n$ nodes which is an expander
    graph with very good probability.

    Parameters
    ----------
    n : int
      The number of nodes.
    d : int
      The degree of each node.
    create_using : Graph Instance or Constructor
      Indicator of type of graph to return.
      If a Graph-type instance, then clear and use it.
      If a constructor, call it to create an empty graph.
      Use the Graph constructor by default.
    max_tries : int. (default: 100)
      The number of allowed loops when generating each independent cycle
    seed : (default: None)
      Seed used to set random number generation state. See :ref`Randomness<randomness>`.

    Notes
    -----
    The nodes are numbered from $0$ to $n - 1$.

    The graph is generated by taking $d / 2$ random independent cycles.

    Joel Friedman proved that in this model the resulting
    graph is an expander with probability
    $1 - O(n^{-\tau})$ where $\tau = \lceil (\sqrt{d - 1}) / 2 \rceil - 1$. [1]_

    Examples
    --------
    >>> G = nx.maybe_regular_expander(n=200, d=6, seed=8020)

    Returns
    -------
    G : graph
        The constructed undirected graph.

    Raises
    ------
    NetworkXError
        If $d % 2 != 0$ as the degree must be even.
        If $n - 1$ is less than $ 2d $ as the graph is complete at most.
        If max_tries is reached

    See Also
    --------
    is_regular_expander
    random_regular_expander_graph

    References
    ----------
    .. [1] Joel Friedman,
       A Proof of Alon's Second Eigenvalue Conjecture and Related Problems, 2004
       https://arxiv.org/abs/cs/0405020

    r   Nr   zn must be a positive integerr   z$d must be greater than or equal to 2zd must be evenzNeed n-1>= d to have room for z independent cycles with z nodesc                 6    h | ]\  }}||fv||fv||fS r1   r1   )r2   r%   r&   edgess      r'   r3   z)maybe_regular_expander.<locals>.<setcomp>;  sH       Aqq6&&Aq6+>+> A+>+>+>r(   T)cyclicz-Too many iterations in maybe_regular_expander)numpyr   r   r   setr   lenpermutationtolistappendutilspairwiseupdateadd_edges_from)r   dr    r:   r7   npr!   cyclesi
iterationscycle	new_edgesr=   s               @r'   r   r      s   ~ 1uu=>>>FFEFFFEQJJ/000EQJJWQ!VWWaWWW
 
 	
 	q,''A1uuFEEE 16]] X X
%jjQUaK''!OJ $$QU++2244ELLQ   H--eD-AA  I 9~~""e$$$Y'''Q&'VWWW' %jjQUaK''* UHr(   directed
multigraphr!   weightr   )preserve_edge_attrsepsilonc                   ddl }ddl}|dk     rt          j        d          t          j        |           sdS t          j                            | j                  \  }}t          j        | t                    }|j
        j                            |ddd          }t          |          }t          t          |          d|                    |d	z
            z  |z   k               S )
a  Determines whether the graph G is a regular expander. [1]_

    An expander graph is a sparse graph with strong connectivity properties.

    More precisely, this helper checks whether the graph is a
    regular $(n, d, \lambda)$-expander with $\lambda$ close to
    the Alon-Boppana bound and given by
    $\lambda = 2 \sqrt{d - 1} + \epsilon$. [2]_

    In the case where $\epsilon = 0$ then if the graph successfully passes the test
    it is a Ramanujan graph. [3]_

    A Ramanujan graph has spectral gap almost as large as possible, which makes them
    excellent expanders.

    Parameters
    ----------
    G : NetworkX graph
    epsilon : int, float, default=0

    Returns
    -------
    bool
        Whether the given graph is a regular $(n, d, \lambda)$-expander
        where $\lambda = 2 \sqrt{d - 1} + \epsilon$.

    Examples
    --------
    >>> G = nx.random_regular_expander_graph(20, 4)
    >>> nx.is_regular_expander(G)
    True

    See Also
    --------
    maybe_regular_expander
    random_regular_expander_graph

    References
    ----------
    .. [1] Expander graph, https://en.wikipedia.org/wiki/Expander_graph
    .. [2] Alon-Boppana bound, https://en.wikipedia.org/wiki/Alon%E2%80%93Boppana_bound
    .. [3] Ramanujan graphs, https://en.wikipedia.org/wiki/Ramanujan_graph

    r   Nzepsilon must be non negativeF)dtypeLMr   )whichkreturn_eigenvectorsr   )r?   scipyr   r   
is_regularrE   arbitrary_elementdegreeadjacency_matrixfloatsparselinalgeigshminboolabssqrt)	r!   rU   rJ   sp_rI   Alamslambda2s	            r'   r   r   N  s    b {{=>>>= u8%%ah//DAq
AU+++A9!!!41%!PPD $iiG Gq2771q5>>1G;;<<<r(   )rU   r    r:   r7   c                    t          | ||||          }|}t          ||          sD|dz  }t          | ||||          }|dk    rt          j        d          t          ||          D|S )a  Returns a random regular expander graph on $n$ nodes with degree $d$.

    An expander graph is a sparse graph with strong connectivity properties. [1]_

    More precisely the returned graph is a $(n, d, \lambda)$-expander with
    $\lambda = 2 \sqrt{d - 1} + \epsilon$, close to the Alon-Boppana bound. [2]_

    In the case where $\epsilon = 0$ it returns a Ramanujan graph.
    A Ramanujan graph has spectral gap almost as large as possible,
    which makes them excellent expanders. [3]_

    Parameters
    ----------
    n : int
      The number of nodes.
    d : int
      The degree of each node.
    epsilon : int, float, default=0
    max_tries : int, (default: 100)
      The number of allowed loops, also used in the maybe_regular_expander utility
    seed : (default: None)
      Seed used to set random number generation state. See :ref`Randomness<randomness>`.

    Raises
    ------
    NetworkXError
        If max_tries is reached

    Examples
    --------
    >>> G = nx.random_regular_expander_graph(20, 4)
    >>> nx.is_regular_expander(G)
    True

    Notes
    -----
    This loops over `maybe_regular_expander` and can be slow when
    $n$ is too big or $\epsilon$ too small.

    See Also
    --------
    maybe_regular_expander
    is_regular_expander

    References
    ----------
    .. [1] Expander graph, https://en.wikipedia.org/wiki/Expander_graph
    .. [2] Alon-Boppana bound, https://en.wikipedia.org/wiki/Alon%E2%80%93Boppana_bound
    .. [3] Ramanujan graphs, https://en.wikipedia.org/wiki/Ramanujan_graph

    r9   rT   r   )r   rI   r    r:   r7   r   z4Too many iterations in random_regular_expander_graph)r   r   r   r   )r   rI   rU   r    r:   r7   r!   rM   s           r'   r   r     s    p 		1<94	 	 	A J!!W555 	a
"1<94
 
 
 ??"F   "!W555 	 Hr(   )N)__doc__r   networkxr   __all___dispatchabler   r   r   rE   
decoratorsnp_random_stater   not_implemented_forr   r   r1   r(   r'   <module>rv      s"   9 9          T T222' ' ' 32'T T222< < < 32<~ T2229 9 9 329x $$V,,T222154 p p p p 32 -,pf j))l++sXqM&:;;;&' @= @= @= @= <; ,+ *)@=F $$V,,T222TStF F F F 32 -,F F Fr(   