
    h4                        S r SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  SSKJrJrJr  SSKJr  SS	KJrJrJr  / S
Qr\R,                  " SSS9\" SS/5      SS j5       5       r\R,                  " SSS9SS j5       r\R,                  " SSS9S 5       r\R,                  " SSS9 SS j5       r\R,                  " SSS9 SS j5       rg)a  Functions for generating grid graphs and lattices

The :func:`grid_2d_graph`, :func:`triangular_lattice_graph`, and
:func:`hexagonal_lattice_graph` functions correspond to the three
`regular tilings of the plane`_, the square, triangular, and hexagonal
tilings, respectively. :func:`grid_graph` and :func:`hypercube_graph`
are similar for arbitrary dimensions. Useful relevant discussion can
be found about `Triangular Tiling`_, and `Square, Hex and Triangle Grids`_

.. _regular tilings of the plane: https://en.wikipedia.org/wiki/List_of_regular_polytopes_and_compounds#Euclidean_tilings
.. _Square, Hex and Triangle Grids: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
.. _Triangular Tiling: https://en.wikipedia.org/wiki/Triangular_tiling

    )repeat)sqrtN)set_node_attributes)NetworkXError)cycle_graphempty_graph
path_graph)relabel_nodes)flattennodes_or_numberpairwise)grid_2d_graph
grid_graphhypercube_graphtriangular_lattice_graphhexagonal_lattice_graphT)graphsreturns_graph   c                   ^
^^ [        SU5      nU u  pVUu  nm
UR                  U
4S jU 5       5        UR                  U
4S j[        U5       5       5        UR                  U
4S jU 5       5         Uu  pU(       a5  [        U5      S:  a&  US   mUS   mUR                  UU4S jT
 5       5        U	(       a5  [        T
5      S:  a&  T
S   mT
S   mUR                  UU4S jU 5       5        UR                  5       (       a&  UR                  S	 UR                  5        5       5        U$ ! [         a    U=p Nf = f)
a  Returns the two-dimensional grid graph.

The grid graph has each node connected to its four nearest neighbors.

Parameters
----------
m, n : int or iterable container of nodes
    If an integer, nodes are from `range(n)`.
    If a container, elements become the coordinate of the nodes.

periodic : bool or iterable
    If `periodic` is True, both dimensions are periodic. If False, none
    are periodic.  If `periodic` is iterable, it should yield 2 bool
    values indicating whether the 1st and 2nd axes, respectively, are
    periodic.

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

Returns
-------
NetworkX graph
    The (possibly periodic) grid graph of the specified dimensions.

r   c              3   >   >#    U  H  nT  H  o!U4v   M
     M     g 7fN ).0ijcolss      M/var/www/html/env/lib/python3.13/site-packages/networkx/generators/lattice.py	<genexpr> grid_2d_graph.<locals>.<genexpr>B   s     8T4aV4VTs   c              3   H   >#    U  H  u  pT  H  o2U4X44v   M     M     g 7fr   r   )r   pir   r   r   s       r   r   r    C   s%     QN52Dq!frg&D&Ns   "c              3   Z   >#    U  H   n[        T5        H  u  p#X4X44v   M     M"     g 7fr   )r   )r   r   pjr   r   s       r   r   r    D   s)     Q1(4.qfqg&.&s   (+   c              3   4   >#    U  H  nTU4TU44v   M     g 7fr   r   )r   r   firstlasts     r   r   r    N   s     ?$Q5!*tQi0$s   c              3   2   >#    U  H  oT4UT44v   M     g 7fr   r   )r   r   r(   r)   s     r   r   r    R   s     ?$Qe*q$i0$s   c              3   ,   #    U  H
  u  pX!4v   M     g 7fr   r   )r   uvs      r   r   r    U   s     6IDA!Is   )r   add_nodes_fromadd_edges_fromr   	TypeErrorlenis_directededges)mnperiodiccreate_usingGrow_namerowscol_name
periodic_r
periodic_cr   r(   r)   s             @@@r   r   r   #   s   8 	A|$ANHNHd8T88QHTNQQQQQ+!)
 c$i!mQBx	?$??c$i!mQBx	?$??}}	6AGGI66H  +"**
Z+s   4D- -D=<D=c                 4   SSK Jn  U (       d  [        S5      $  S U 5       n[        U5      " U S   5      nU SS  H  n[        U5      " U5      nU" Xd5      nM     [        U[        5      nU$ ! [         a    [	        U(       a  [
        O[        5      n Nsf = f)a"  Returns the *n*-dimensional grid graph.

The dimension *n* is the length of the list `dim` and the size in
each dimension is the value of the corresponding list element.

Parameters
----------
dim : list or tuple of numbers or iterables of nodes
    'dim' is a tuple or list with, for each dimension, either a number
    that is the size of that dimension or an iterable of nodes for
    that dimension. The dimension of the grid_graph is the length
    of `dim`.

periodic : bool or iterable
    If `periodic` is True, all dimensions are periodic. If False all
    dimensions are not periodic. If `periodic` is iterable, it should
    yield `dim` bool values each of which indicates whether the
    corresponding axis is periodic.

Returns
-------
NetworkX graph
    The (possibly periodic) grid graph of the specified dimensions.

Examples
--------
To produce a 2 by 3 by 4 grid graph, a graph on 24 nodes:

>>> from networkx import grid_graph
>>> G = grid_graph(dim=(2, 3, 4))
>>> len(G)
24
>>> G = grid_graph(dim=(range(7, 9), range(3, 6)))
>>> len(G)
6
r   )cartesian_productc              3   F   #    U  H  o(       a  [         O[        v   M     g 7fr   )r   r	   )r   ps     r   r   grid_graph.<locals>.<genexpr>   s     C(Qqj0(s   !r   N)
%networkx.algorithms.operators.productr?   r   r0   r   r   r	   nextr
   r   )dimr6   r?   funcr8   current_dimGnewHs           r   r   r   Y   s    L H1~?C(C 	T
3q6A12wDz+&d&  	a!AH  ?Xk:>?s   	A. .&BBc                 (    U S/-  n[        U5      nU$ )a  Returns the *n*-dimensional hypercube graph.

The nodes are the integers between 0 and ``2 ** n - 1``, inclusive.

For more information on the hypercube graph, see the Wikipedia
article `Hypercube graph`_.

.. _Hypercube graph: https://en.wikipedia.org/wiki/Hypercube_graph

Parameters
----------
n : int
    The dimension of the hypercube.
    The number of nodes in the graph will be ``2 ** n``.

Returns
-------
NetworkX graph
    The hypercube graph of dimension *n*.
r%   )r   )r5   rE   r8   s      r   r   r      s    , qc'C3AH    c           
        ^^^^ [        SU5      nUS:X  d  U S:X  a  U$ U(       a  US:  d  U S:  a  SU  SU 3n[        U5      eUS-   S-  m[        U S-   5      m[        TS-   5      mUR                  UU4S jT 5       5        UR                  U4S	 jTS
U   5       5        UR                  UU4S jTSU S2    5       5        UR                  UU4S jTS
U S2    5       5        SSKJn  USL a0  T H  nU" XXS4X45      nM     TS
U   H  n	U" USU	4TU	45      nM     O+US-  (       a!  UR                  U4S jTSS
S2    5       5        U(       a  U4S jT 5       n
U4S jT 5       nU4S jT 5       n[        S5      S-  mU(       a  UU4S jT 5       nOUU4S jT 5       n[        XX5       VV	VVs0 s H  u  ppX4U;   d  M  X4X4_M     nnn	nn[        UUS5        U$ s  snnn	nf )a  Returns the $m$ by $n$ triangular lattice graph.

The `triangular lattice graph`_ is a two-dimensional `grid graph`_ in
which each square unit has a diagonal edge (each grid unit has a chord).

The returned graph has $m$ rows and $n$ columns of triangles. Rows and
columns include both triangles pointing up and down. Rows form a strip
of constant height. Columns form a series of diamond shapes, staggered
with the columns on either side. Another way to state the size is that
the nodes form a grid of `m+1` rows and `(n + 1) // 2` columns.
The odd row nodes are shifted horizontally relative to the even rows.

Directed graph types have edges pointed up or right.

Positions of nodes are computed by default or `with_positions is True`.
The position of each node (embedded in a euclidean plane) is stored in
the graph using equilateral triangles with sidelength 1.
The height between rows of nodes is thus $\sqrt(3)/2$.
Nodes lie in the first quadrant with the node $(0, 0)$ at the origin.

.. _triangular lattice graph: http://mathworld.wolfram.com/TriangularGrid.html
.. _grid graph: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
.. _Triangular Tiling: https://en.wikipedia.org/wiki/Triangular_tiling

Parameters
----------
m : int
    The number of rows in the lattice.

n : int
    The number of columns in the lattice.

periodic : bool (default: False)
    If True, join the boundary vertices of the grid using periodic
    boundary conditions. The join between boundaries is the final row
    and column of triangles. This means there is one row and one column
    fewer nodes for the periodic lattice. Periodic lattices require
    `m >= 3`, `n >= 5` and are allowed but misaligned if `m` or `n` are odd

with_positions : bool (default: True)
    Store the coordinates of each node in the graph node attribute 'pos'.
    The coordinates provide a lattice with equilateral triangles.
    Periodic positions shift the nodes vertically in a nonlinear way so
    the edges don't overlap so much.

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

Returns
-------
NetworkX graph
    The *m* by *n* triangular lattice graph.
r         z)m > 2 and n > 4 required for periodic. m=z, n=r   r%   c              3   R   >#    U  H  nTS T   H  o"U4US-   U44v   M     M     g 7fNr   r   r   r   r   Nr   s      r   r   +triangular_lattice_graph.<locals>.<genexpr>   s/     J4abqA!fq1uaj))4s   $'c              3   J   >#    U  H  nT  H  o"U4X!S -   44v   M     M     g7f)r   Nr   )r   r   r   r   s      r   r   rS      s'     J8aT!fqa%j)T)8s    #Nc              3   X   >#    U  H  nTS T   H  o"U4US-   US-   44v   M     M!     g 7frP   r   rQ   s      r   r   rS      s5     U{!DQSRSHq!fq1ua!en-H-{s   '*c              3   V   >#    U  H  nTS T   H  o"S-   U4X!S-   44v   M     M      g 7frP   r   rQ   s      r   r   rS      s3     Tz!4PRQR8a1uaj1!e*-8-z   &)contracted_nodesTc              3   ,   >#    U  H	  nTU4v   M     g 7fr   r   )r   r   rR   s     r   r   rS     s     7JqQFJs   c              3   :   >#    U  H  nT  H  o!v   M     M     g 7fr   r   r   r   r   r:   s      r   r   rS          ,At!ata   c              3   :   >#    U  H  nT  H  o"v   M     M     g 7fr   r   r\   s      r   r   rS     r]   r^   c              3   N   >#    U  H  nT  H  nS US-  -  U-   v   M     M     g7fg      ?r%   Nr   r\   s      r   r   rS     s'     <At!cQUmats   "%c              3   T   >#    U  H  nT  H  nTU-  S U-  U-  -   v   M     M     g7fg{Gz?Nr   r   r   r   hr:   s      r   r   rS   
  s+     C1d!a%$(Q,&d&   %(c              3   B   >#    U  H  nT  H
  nTU-  v   M     M     g 7fr   r   rd   s      r   r   rS     s     4At!!a%t%   pos)
r   r   ranger/   networkx.algorithms.minorsrY   remove_nodes_fromr   zipr   )r4   r5   r6   with_positionsr7   rI   msgrY   r   r   iijjxxyyxyri   rR   r   re   r:   s                    @@@@r   r   r      s   r 	A|$AAvaq5AE=aSQCHC$$	
Q1AQ<DQ<DJ4JJJ48JJUtAaE{UUTtDQqDzTT;4A FQF3A bqA QFQF3A 	
Q	7DAJ77 ,,,,<<GaKCCB44B03BB0CS0C*!vQR{~vv~0CSAsE*H Ts   9G(
	G(
c           
        ^^^ [        SU5      nU S:X  d  US:X  a  U$ U(       a"  US-  S:X  d  U S:  d  US:  a  Sn[        U5      eSU -  m[        TS-   5      m[        US-   5      nUU4S jU 5       nU4S jUSU  5       n	UR                  U5        UR                  U	5        UR	                  STS-   45        UR	                  UTS-   US-  -  45        SSKJn
  U(       aa  USU  H  nU
" X[S4UT45      nM     USS  H  nU
" X[S4UTS-   45      nM     TST  H  nU
" USU4X45      nM     UR	                  UT45        U4S	 jU 5       nU4S
 jU 5       nU4S jU 5       n[        S5      S-  mU(       a  UU4S jU 5       nOUU4S jU 5       n[        XUU5       VVVVs0 s H  u  pnnX4U;   d  M  X4UU4_M     nnnnn[        UUS5        U$ s  snnnnf )a  Returns an `m` by `n` hexagonal lattice graph.

The *hexagonal lattice graph* is a graph whose nodes and edges are
the `hexagonal tiling`_ of the plane.

The returned graph will have `m` rows and `n` columns of hexagons.
`Odd numbered columns`_ are shifted up relative to even numbered columns.

Positions of nodes are computed by default or `with_positions is True`.
Node positions creating the standard embedding in the plane
with sidelength 1 and are stored in the node attribute 'pos'.
`pos = nx.get_node_attributes(G, 'pos')` creates a dict ready for drawing.

.. _hexagonal tiling: https://en.wikipedia.org/wiki/Hexagonal_tiling
.. _Odd numbered columns: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/

Parameters
----------
m : int
    The number of rows of hexagons in the lattice.

n : int
    The number of columns of hexagons in the lattice.

periodic : bool
    Whether to make a periodic grid by joining the boundary vertices.
    For this to work `n` must be even and both `n > 1` and `m > 1`.
    The periodic connections create another row and column of hexagons
    so these graphs have fewer nodes as boundary nodes are identified.

with_positions : bool (default: True)
    Store the coordinates of each node in the graph node attribute 'pos'.
    The coordinates provide a lattice with vertical columns of hexagons
    offset to interleave and cover the plane.
    Periodic positions shift the nodes vertically in a nonlinear way so
    the edges don't overlap so much.

create_using : NetworkX graph constructor, optional (default=nx.Graph)
    Graph type to create. If graph instance, then cleared before populated.
    If graph is directed, edges will point up or right.

Returns
-------
NetworkX graph
    The *m* by *n* hexagonal lattice graph.
r   r%   r   z8periodic hexagonal lattice needs m > 1, n > 1 and even nc              3   V   >#    U  H  nTS TS-      H  o!U4XS-   44v   M     M      g 7frP   r   )r   r   r   Mr:   s      r   r   *hexagonal_lattice_graph.<locals>.<genexpr>O  s1     Kt!T'AE]a&1!e*%]%trW   c              3   f   >#    U  H&  nT  H  o!S -  US -  :X  d  M  X4US-   U44v   M     M(     g7f)r%   r   Nr   r\   s      r   r   ry   P  s;     Xx!AQRSVWRW%1&1q5!*%%xs   11NrX   c              3   :   >#    U  H  nT  H  o!v   M     M     g 7fr   r   r\   s      r   r   ry   d       	(T4a!4!Tr^   c              3   :   >#    U  H  nT  H  o"v   M     M     g 7fr   r   r\   s      r   r   ry   e  r|   r^   c              3   l   >#    U  H)  nT  H  nS U-   US-  -   US-  US-  S -
  -  -   v   M!     M+     g7fra   r   r\   s      r   r   ry   f  s=     	S1d#'AF
a!eQ#6
6d
6s   14rN   c              3   T   >#    U  H  nT  H  nTU-  S U-  U-  -   v   M     M     g7frc   r   rd   s      r   r   ry   i  s+     ?Dq$Qa!edQhl"$"Drf   c              3   B   >#    U  H  nT  H
  nTU-  v   M     M     g 7fr   r   rd   s      r   r   ry   k  s     0T4aa!e4eTrh   ri   )
r   r   rj   r/   remove_noderk   rY   r   rm   r   )r4   r5   r6   rn   r7   r8   ro   r   	col_edges	row_edgesrY   r   r   rp   rq   rr   rs   rt   ru   ri   rx   re   r:   s                       @@@r   r   r     s   d 	A|$AAvaQUaZ1q5AEHC  	AAQ<DQ<DKtKIXtBQxXIYYMM1a!e*MM1q1uQ'() <bqA FQF3A abA FQAJ7A aA QFQF3A 	q!f 
)T	(B	(T	(B	S	SBQ!A?D?0T0,/B,?
O,?jaAqA6Q;>A6Aq6>,?C
O3&H Ps   ,G
?
G
)FN)F)FTN)__doc__	itertoolsr   mathr   networkxnxnetworkx.classesr   networkx.exceptionr   networkx.generators.classicr   r   r	   networkx.relabelr
   networkx.utilsr   r   r   __all___dispatchabler   r   r   r   r   r   rK   r   <module>r      s       0 , L L * = = T2!Q1  31h T25 35p T2 34 T2<@a 3aH T2<@\ 3\rK   