U
    ¹mœdÞ  ã                   @   s@   d Z ddlZddlZdddgZd	dd„Zd
dd„Zddd„ZdS )z5Provides explicit constructions of expander graphs.

é    NÚmargulis_gabber_galil_graphÚchordal_cycle_graphÚpaley_graphc                 C   sÔ   t jd|t jd�}| ¡ s"| ¡ s0d}t  |¡‚tjt| ƒdd�D ]z\}}|d|  |  |f|d| d  |  |f||d|  |  f||d| d  |  ffD ]\}}| 	||f||f¡ qžqBd| › 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   ©Údefaultú0`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Úv© r    úV/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/networkx/generators/expanders.pyr   +   s    
üc           	      C   s¤   t jd|t jd�}| ¡ s"| ¡ s0d}t  |¡‚t| ƒD ]T}|d |  }|d |  }|dkrlt|| d | ƒnd}|||fD ]}| ||¡ qzq8d| › 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ÚrightZchordr   r    r    r!   r   U   s    &
c                    s‚   t jd|t jd�}| ¡ r(d}t  |¡‚‡ fdd„tdˆ ƒD ƒ}tˆ ƒD ]"}|D ]}| ||| ˆ  ¡ qPqHdˆ › 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                    s(   h | ] }|d  ˆ  dkr|d  ˆ  ’qS )r   r   r    )Ú.0r   ©r#   r    r!   Ú	<setcomp>Å   s      zpaley_graph.<locals>.<setcomp>r
   zpaley(r   r   )r   r   ZDiGraphr   r   r   r   r   )r#   r   r   r   Z
square_setr   Zx2r    r'   r!   r   ”   s    )
)N)N)N)Ú__doc__r   Znetworkxr   Ú__all__r   r   r   r    r    r    r!   Ú<module>   s   
#
*
?