K i4dZddlmZddlmZddlZddlmZddl m Z ddl m Z m Z mZddlmZdd lmZmZmZgd Zej,dd edd gddZej,dd ddZej,dd dZej,dd  ddZej,dd  ddZy)aFunctions 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)flattennodes_or_numberpairwise) grid_2d_graph grid_graphhypercube_graphtriangular_lattice_graphhexagonal_lattice_graphT)graphs returns_graphcX td|}|\}}|\} |j fd|D|j fdt|D|j fd|D |\}} |r3t |dkDr%|d |d |j fd D| r3t dkDr% d d |j fd|D|j r&|jd |jD|S#t$r|x}} YwxYw) 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. rc34K|]}D]}||f ywN).0ijcolss a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/generators/lattice.py z grid_2d_graph..Bs!848aaV8V8sc3BK|]\}}D] }||f||ffywrr)rpirrrs rrz grid_2d_graph..Cs/Q52qDQqq!fr1g&Q&Qc3TK|]}tD]\}}||f||ff!ywr)r )rrpjrrs rrz grid_2d_graph..Ds3Q1(4.QQq!fq"g&Q&Qs%(c3.K|] }|f|ffywrr)rrfirstlasts rrz grid_2d_graph..Ns?Q5!*tQi0?c3.K|] }|f|ffywrr)rrr(r)s rrz grid_2d_graph..Rs?Q1e*q$i0?r*c3*K|] \}}||f ywrr)ruvs rrz grid_2d_graph..Us6DAq!Q6s)radd_nodes_fromadd_edges_fromr TypeErrorlen is_directededges) mnperiodic create_usingGrow_namerowscol_name periodic_r periodic_crr(r)s @@@rrr#s8 A|$ANHdNHd8T88QHTNQQQQQ+!) Jc$i!mQBx ?$??c$i!mQBx ?$??}} 6AGGI66 H +"** Z+s2D D)(D)cddlm}|s tdS d|D}t||d}|ddD]}t||}|||}t|t}|S#t$rt |rt nt }YlwxYw)aReturns 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_productc38K|]}|rtntywr)rr )rps rrzgrid_graph..sCQq j0CsrN) %networkx.algorithms.operators.productr@rr1rrr nextr r )dimr7r@funcr9 current_dimGnewHs rrrYsLH 1~?C(C T 3q6A12w' tDz+& dA &' a!A H ?Xk:>?s A(( B  B c(|dgz}t|}|S)aReturns 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)r6rEr9s rrrs, qc'C3A Hc td|}|dk(s|dk(r|S|r|dks|dkrd|d|}t||dzdzt|dztdz|jfdD|jfd d |D|jfd d|dD|jfd d |dDdd lm}|dur.D]}|||df||f}d |D]} ||d| f| f}n%|dzr |j fddd dD|rfdD} fdD} fdD} tddz |r fdD} n fdD} t| | | | D cic]\}} }}|| f|vs|| f||f}}} }}t||d|Scc}}} }w)ak 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. rz)m > 2 and n > 4 required for periodic. m=z, n=rr%c3HK|]}dD]}||f|dz|ffywNrrrrrNrs rrz+triangular_lattice_graph..s5JabqJAq!fq1uaj)J)J"c3BK|]}D]}||f||dzffyw)rNr)rrrrs rrz+triangular_lattice_graph..s/JaTJq!fq!a%j)J)Jr"Nc3NK|]}dD]}||f|dz|dzffywrPrrQs rrz+triangular_lattice_graph..s;U!DQSRSHUqq!fq1ua!en-U-U"%c3NK|]}dD]}|dz|f||dzffywrPrrQs rrz+triangular_lattice_graph..s;T!4PRQR8Taq1uaj1a!e*-T-TrVcontracted_nodesTc3&K|]}|f ywrr)rrrRs rrz+triangular_lattice_graph..s7qQF7sc30K|] }D]}|ywrrrrrr;s rrz+triangular_lattice_graph.. ,At ,!a ,a ,c30K|] }D]}|ywrrr\s rrz+triangular_lattice_graph..r]r^c3BK|]}D]}d|dzz|zywg?r%Nrr\s rrz+triangular_lattice_graph..s+ . s/C1dC!a%$(Q,&C&CrSc36K|]}D] }|z ywrrrds rrz+triangular_lattice_graph.. s!4At4!!a%4%4pos) rrranger0networkx.algorithms.minorsrYremove_nodes_fromrzipr)r5r6r7with_positionsr8rImsgrYrriijjxxyyxyrhrRrrer;s @@@@rrrsr A|$AAva q5AE=aSQCHC$ $ Q1 A Q 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. rr%rz8periodic hexagonal lattice needs m > 1, n > 1 and even nc3NK|]}ddzD]}||f||dzffywrPr)rrrMr;s rrz*hexagonal_lattice_graph..Os9K!T'AE]K1a&1a!e*%K%KrVc3ZK|]"}D]}|dz|dzk(s||f|dz|ff$yw)r%rNrr\s rrz*hexagonal_lattice_graph..Ps@X!XAQRSVWRW1a&1q5!*%X%Xs++NrXc30K|] }D]}|ywrrr\s rrz*hexagonal_lattice_graph..d (4 (a! (! (r^c30K|] }D]}|ywrrr\s rrz*hexagonal_lattice_graph..erzr^c3`K|]%}D]}d|z|dzz|dz|dzdz zz 'ywrarr\s rrz*hexagonal_lattice_graph..fsA S1d S#'AF a!eQ# 6 6 S 6 Ss+.rNc3HK|]}D]}|zd|z|zzywrcrrds rrz*hexagonal_lattice_graph..is/ ?q$ ?Qa!edQhl" ?" ?rSc36K|]}D] }|z ywrrrds rrz*hexagonal_lattice_graph..ks! 04 0aa!e 0e 0rgrh) rrrir0 remove_noderjrYrrlr)r5r6r7rmr8r9rnr col_edges row_edgesrYrrrorprqrrrsrtrhrwrer;s @@@rrrsd A|$AAvaQUaZ1q5AEHC   AA Q OC O3& H Ps F= F= )FN)F)FTN)__doc__ itertoolsrmathrnetworkxnxnetworkx.classesrnetworkx.exceptionrnetworkx.generators.classicrrr networkx.relabelr networkx.utilsr r r __all__ _dispatchablerrrrrrrKrrs 0,LL*== T2!Q1 31 hT25 35 pT2 3 4T2<@a 3a HT2<@\ 3\ rK