ó ŸÃÒYc@ sidZddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZdddddgZeddgƒedd„ƒZed„Zd„Zeedd„Z eedd„Z!dS(sý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 relevent 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 iÿÿÿÿ(tdivision(tsqrt(tGraph(tset_node_attributes(tcontracted_nodes(tcartesian_product(t NetworkXError(t relabel_nodes(tflatten(tis_list_of_ints(tnodes_or_number(tpairwise(t cycle_graph(t empty_graph(t path_grapht grid_2d_grapht grid_graphthypercube_graphttriangular_lattice_graphthexagonal_lattice_graphiic sUtd|ƒ}|\}}|\}‰|j‡fd†|Dƒƒ|j‡fd†t|ƒDƒƒ|j‡fd†|Dƒƒ|tkr%t|ƒdkrÙ|d‰|d‰|j‡‡fd†ˆDƒƒntˆƒdkr%ˆd‰ˆd‰|j‡‡fd†|Dƒƒq%n|jƒrQ|jd „|jƒDƒƒn|S( s'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 (default: False) If this is ``True`` the nodes on the grid boundaries are joined to the corresponding nodes on the opposite grid boundaries. create_using : NetworkX graph (default: Graph()) If provided this graph is cleared of nodes and edges and filled with the new graph. Usually used to set the type of the graph. Returns ------- NetworkX graph The (possibly periodic) grid graph of the specified dimensions. ic3 s(|]}ˆD]}||fVq qdS(N((t.0titj(tcols(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Nsc3 s:|]0\}}ˆD]}||f||ffVqqdS(N((RtpiRR(R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Osc3 s@|]6}tˆƒD]#\}}||f||ffVqqdS(N(R (RRtpjR(R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Qsiiÿÿÿÿc3 s'|]}ˆ|fˆ|ffVqdS(N((RR(tfirsttlast(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Wsc3 s'|]}|ˆf|ˆffVqdS(N((RR(RR(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys [scs s!|]\}}||fVqdS(N((Rtutv((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys ^s(R tadd_nodes_fromtadd_edges_fromR tTruetlent is_directedtedges(tmtntperiodict create_usingtGtrow_nametrowstcol_name((RRRsq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyR1s(      #  &  c C s‘d|}|s tdƒ}|S|r,tnt}||dƒ}x9|dD]-}|jƒ}||ƒ}t||ƒ}qMWt|tƒ}|S(sReturns 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 If `periodic is True` the nodes on the grid boundaries are joined to the corresponding nodes on the opposite grid boundaries. 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 s%sii(R R RtcopyRRR( tdimR&tdlabelR(tfunct current_dimtGoldtGnewtH((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyRbs#    cC s|dg}t|ƒ}|S(sReturns 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*. i(R(R%R-R(((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyR˜s  c  s­td|ƒ‰|dks'|dkr+ˆS|rm|dksI|dkrmd}t|j||ƒƒ‚qmn|dd‰t|dƒ‰tˆdƒ‰ˆj‡‡fd†ˆDƒƒˆj‡fd†ˆ| Dƒƒˆj‡‡fd †ˆd|d…Dƒƒˆj‡‡fd †ˆd |d…Dƒƒ|tkr§x,ˆD]$}tˆ|df||fƒ‰qIWxjˆ| D]$}tˆd|fˆ|fƒ‰q|Wn7|drÞˆj‡fd †ˆdd d…Dƒƒn|r©‡fd †ˆDƒ}‡fd†ˆDƒ} ‡fd†ˆDƒ} tdƒd‰|rX‡‡fd†ˆDƒ} n‡‡fd†ˆDƒ} ‡fd†t || | | ƒDƒ} t ˆ| dƒnˆS(s¼ 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 If specified, this must be an instance of a NetworkX graph class. It will be cleared of nodes and edges and filled with the new graph. Usually used to set the type of the graph. Returns ------- NetworkX graph The *m* by *n* triangular lattice graph. iiis1m > 2 and n > 4 required for periodic. m={}, n={}iic3 s<|]2}ˆˆ D]!}||f|d|ffVqqdS(iN((RRR(tNR(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys ÷sc3 s8|].}ˆD]!}||f||dffVq qdS(iN((RRR(R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys øsc3 s@|]6}ˆˆ D]%}||f|d|dffVqqdS(iN((RRR(R4R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys úsc3 s@|]6}ˆˆ D]%}|d|f||dffVqqdS(iN((RRR(R4R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys üsNc3 s|]}ˆ|fVqdS(N((RR(R4(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc3 s"|]}ˆD] }|Vq qdS(N((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc3 s"|]}ˆD] }|Vq qdS(N((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc3 s.|]$}ˆD]}d|d|Vq qdS(gà?iN((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc3 s2|](}ˆD]}ˆ|d||Vq qdS(g{®Gáz„?N((RRR(thR*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc3 s&|]}ˆD]}ˆ|Vq qdS(N((RRR(R5R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys sc sCi|]9\}}}}||fˆkr||f||f“qS(((RRRtxty(R3(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys s tpos( R RtformattrangeRR Rtremove_nodes_fromRtzipR( R$R%R&twith_positionsR'tmsgRRtiitjjtxxtyyR8((R3R4RR5R*sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyR²sD9 !  "% -%c s³|dk r|ntƒ‰ˆjƒ|dks=|dkrAˆS|r„|ddkso|dkso|dkr„d}t|ƒ‚nd|‰tˆdƒ‰t|dƒ}‡‡fd†|Dƒ}‡fd†|| Dƒ}ˆj|ƒˆj|ƒˆjdˆdfƒˆj|ˆd|dfƒ|ríx0|| D]$} tˆ| df| ˆfƒ‰qBWx4|dD](} tˆ| df| ˆdfƒ‰quWx3ˆdˆ!D]$} tˆd| f|| fƒ‰q¯Wˆj|ˆfƒn‡fd†|Dƒ} ‡fd†|Dƒ} ‡fd †|Dƒ} td ƒd‰|ra‡‡fd †|Dƒ}n‡‡fd †|Dƒ}‡fd †t | | | |ƒDƒ}t ˆ|dƒˆS(sÚ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 odd 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 If specified, this must be an instance of a NetworkX graph class. It will be cleared of nodes and edges and filled with the new graph. Usually used to set the type of the graph. If graph is directed, edges will point up or right. Returns ------- NetworkX graph The *m* by *n* hexagonal lattice graph. iiis8periodic hexagonal lattice needs m > 1, n > 1 and even nc3 s@|]6}ˆˆd D]!}||f||dffVqqdS(iN((RRR(tMR*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Vsc3 sL|]B}ˆD]5}|d|dkr ||f|d|ffVq qdS(iiN((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys Ws c3 s"|]}ˆD] }|Vq qdS(N((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys jsc3 s"|]}ˆD] }|Vq qdS(N((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys ksc3 sB|]8}ˆD]+}d||d|d|ddVq qdS(gà?iN((RRR(R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys lsic3 s2|](}ˆD]}ˆ|d||Vq qdS(g{®Gáz„?N((RRR(R5R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys psc3 s&|]}ˆD]}ˆ|Vq qdS(N((RRR(R5R*(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys rsc sCi|]9\}}}}||fˆkr||f||f“qS(((RRRR6R7(R((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pys ts R8N( tNoneRtclearRR:Rt remove_nodeRRR<R(R$R%R&R=R'R>Rt col_edgest row_edgesRRR?R@RARBR8((R(RCR5R*sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyRsF2 .   "&"  %N("t__doc__t __future__RtmathRtnetworkx.classesRRtnetworkx.algorithms.minorsRt%networkx.algorithms.operators.productRtnetworkx.exceptionRtnetworkx.relabelRtnetworkx.utilsRR R R tnetworkx.generators.classicR R Rt__all__tFalseRDRRRR RR(((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/lattice.pyts2  0 6  e