
 ,[c           @   sF   d  Z  d d l Z d d l Z d d g Z d d  Z d d  Z d S(   s5   Provides explicit constructions of expander graphs.

iNt   margulis_gabber_galil_grapht   chordal_cycle_graphc         C   s!  t  j d | d t  j } | j   s4 | j   rL d } t  j |   n  x t j t |   d d D] \ } } x | d | |  | f | d | d |  | f | | d | |  f | | d | d |  f f D]( \ } } | j	 | | f | | f  q Wqh Wd j
 |   | j d <| S(	   s  Return 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.

    i    t   defaults0   `create_using` must be an undirected multigraph.t   repeati   i   s    margulis_gabber_galil_graph({0})t   name(   t   nxt   empty_grapht
   MultiGrapht   is_directedt   is_multigrapht   NetworkXErrort	   itertoolst   productt   ranget   add_edget   formatt   graph(   t   nt   create_usingt   Gt   msgt   xt   yt   ut   v(    (    s<   lib/python2.7/site-packages/networkx/generators/expanders.pyR    .   s    (1>$c   	      C   s   t  j d | d t  j } | j   s4 | j   rL d } t  j |   n  x t |   D]t } | d |  } | d |  } | d k r t | |  d |   n d } x' | | | f D] } | j | |  q WqY Wd j	 |   | j
 d <| S(   s  Return 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.

    i    R   s0   `create_using` must be an undirected multigraph.i   i   s   chordal_cycle_graph({0})R   (   R   R   R   R   R	   R
   R   t   powR   R   R   (	   t   pR   R   R   R   t   leftt   rightt   chordR   (    (    s<   lib/python2.7/site-packages/networkx/generators/expanders.pyR   T   s    &((   t   __doc__R   t   networkxR   t   __all__t   NoneR    R   (    (    (    s<   lib/python2.7/site-packages/networkx/generators/expanders.pyt   <module>   s
   #&