Skip to content

GridGraph ​

A rectangular (or higher-dimensional) grid graph.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageGridGraph✓
GridGraph(dims)the Cartesian product of paths of the given lengths — GridGraph({m, n}) is an m×nm \times n grid. Vertices are numbered 1-based, the first index varying fastest, as in Wolfram.
SageMathenumeratio_graph(graphs.GridGraph(dims))
Typed signatures
TypePackageApplies
(list<integer>) -> valuecombinatoricsalways

Domain: Collections

Examples

Basic #

A 3x4 grid has 12 vertices

live ↗
InVertexCount(GridGraph([3,4]))\mathrm{VertexCount}(\mathrm{GridGraph}(\bigl\lbrack3, 4\bigr\rbrack))Out1212

m(n−1)+n(m−1)=3⋅3+4⋅2=17m(n-1) + n(m-1) = 3 \cdot 3 + 4 \cdot 2 = 17 edges

live ↗
InEdgeCount(GridGraph([3,4]))\mathrm{EdgeCount}(\mathrm{GridGraph}(\bigl\lbrack3, 4\bigr\rbrack))Out1717

Dimensions are given as a list, so a three- and a four-dimensional grid come from lists of 22s Vertices are numbered with the first index varying fastest, as in Wolfram (our own convention, not a known value).

live ↗
In[GridGraph([2,2,2]),GridGraph([2,2,2,2])]\bigl\lbrack\mathrm{GridGraph}(\bigl\lbrack2, 2, 2\bigr\rbrack), \mathrm{GridGraph}(\bigl\lbrack2, 2, 2, 2\bigr\rbrack)\bigr\rbrackOut[Graph([1,2,3,4,5,6,7,8],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(1,5),UndirectedEdge(2,4),UndirectedEdge(2,6),UndirectedEdge(3,4),UndirectedEdge(3,7),UndirectedEdge(4,8),UndirectedEdge(5,6),UndirectedEdge(5,7),UndirectedEdge(6,8),UndirectedEdge(7,8)]),Graph([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(1,5),UndirectedEdge(1,9),UndirectedEdge(2,4),UndirectedEdge(2,6),UndirectedEdge(2,10),UndirectedEdge(3,4),UndirectedEdge(3,7),UndirectedEdge(3,11),UndirectedEdge(4,8),UndirectedEdge(4,12),UndirectedEdge(5,6),UndirectedEdge(5,7),UndirectedEdge(5,13),UndirectedEdge(6,8),UndirectedEdge(6,14),UndirectedEdge(7,8),UndirectedEdge(7,15),UndirectedEdge(8,16),UndirectedEdge(9,10),UndirectedEdge(9,11),UndirectedEdge(9,13),UndirectedEdge(10,12),UndirectedEdge(10,14),UndirectedEdge(11,12),UndirectedEdge(11,15),UndirectedEdge(12,16),UndirectedEdge(13,14),UndirectedEdge(13,15),UndirectedEdge(14,16),UndirectedEdge(15,16)])]\bigl\lbrack\mathrm{Graph}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(1, 5), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(2, 6), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(3, 7), \mathrm{UndirectedEdge}(4, 8), \mathrm{UndirectedEdge}(5, 6), \mathrm{UndirectedEdge}(5, 7), \mathrm{UndirectedEdge}(6, 8), \mathrm{UndirectedEdge}(7, 8)\bigr\rbrack), \mathrm{Graph}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(1, 5), \mathrm{UndirectedEdge}(1, 9), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(2, 6), \mathrm{UndirectedEdge}(2, 10), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(3, 7), \mathrm{UndirectedEdge}(3, 11), \mathrm{UndirectedEdge}(4, 8), \mathrm{UndirectedEdge}(4, 12), \mathrm{UndirectedEdge}(5, 6), \mathrm{UndirectedEdge}(5, 7), \mathrm{UndirectedEdge}(5, 13), \mathrm{UndirectedEdge}(6, 8), \mathrm{UndirectedEdge}(6, 14), \mathrm{UndirectedEdge}(7, 8), \mathrm{UndirectedEdge}(7, 15), \mathrm{UndirectedEdge}(8, 16), \mathrm{UndirectedEdge}(9, 10), \mathrm{UndirectedEdge}(9, 11), \mathrm{UndirectedEdge}(9, 13), \mathrm{UndirectedEdge}(10, 12), \mathrm{UndirectedEdge}(10, 14), \mathrm{UndirectedEdge}(11, 12), \mathrm{UndirectedEdge}(11, 15), \mathrm{UndirectedEdge}(12, 16), \mathrm{UndirectedEdge}(13, 14), \mathrm{UndirectedEdge}(13, 15), \mathrm{UndirectedEdge}(14, 16), \mathrm{UndirectedEdge}(15, 16)\bigr\rbrack)\bigr\rbrack
Scope #

Vertices are numbered with the first index varying fastest, as in Wolfram (our own convention, not a known value).

live ↗
InGridGraph([2,3])\mathrm{GridGraph}(\bigl\lbrack2, 3\bigr\rbrack)OutGraph([1,2,3,4,5,6],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(2,4),UndirectedEdge(3,4),UndirectedEdge(3,5),UndirectedEdge(4,6),UndirectedEdge(5,6)])\mathrm{Graph}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(3, 5), \mathrm{UndirectedEdge}(4, 6), \mathrm{UndirectedEdge}(5, 6)\bigr\rbrack)
Applications #

Eccentricity on a 3×33\times3 grid: 22 at the centre, 44 at the corners

live ↗
InMap(_1⁡↦VertexEccentricity(GridGraph([3,3]),_1⁡),VertexList(GridGraph([3,3])))\mathrm{Map}(\operatorname{\_1}↦\mathrm{VertexEccentricity}(\mathrm{GridGraph}(\bigl\lbrack3, 3\bigr\rbrack), \operatorname{\_1}), \mathrm{VertexList}(\mathrm{GridGraph}(\bigl\lbrack3, 3\bigr\rbrack)))Out[4,3,4,3,2,3,4,3,4]\bigl\lbrack4, 3, 4, 3, 2, 3, 4, 3, 4\bigr\rbrack
Properties #

A 1×41\times4 grid is a path

live ↗
InIsPathGraph(GridGraph([1,4]))\mathrm{IsPathGraph}(\mathrm{GridGraph}(\bigl\lbrack1, 4\bigr\rbrack))OutTrue\mathrm{True}

Swapping the dimensions gives the same path

live ↗
InIsPathGraph(GridGraph([4,1]))\mathrm{IsPathGraph}(\mathrm{GridGraph}(\bigl\lbrack4, 1\bigr\rbrack))OutTrue\mathrm{True}

Extra size-1 dimensions add nothing, so still a path

live ↗
InIsPathGraph(GridGraph([1,4,1]))\mathrm{IsPathGraph}(\mathrm{GridGraph}(\bigl\lbrack1, 4, 1\bigr\rbrack))OutTrue\mathrm{True}

Grids are bipartite, coloured like a checkerboard

live ↗
InIsBipartiteGraph(GridGraph([3,4]))\mathrm{IsBipartiteGraph}(\mathrm{GridGraph}(\bigl\lbrack3, 4\bigr\rbrack))OutTrue\mathrm{True}

A 2×22\times2 grid is the 44-cycle Vertices are numbered with the first index varying fastest, as in Wolfram (our own convention, not a known value).

live ↗
In[GridGraph([2,2]),CycleGraph(4)]\bigl\lbrack\mathrm{GridGraph}(\bigl\lbrack2, 2\bigr\rbrack), \mathrm{CycleGraph}(4)\bigr\rbrackOut[Graph([1,2,3,4],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(2,4),UndirectedEdge(3,4)]),Graph([1,2,3,4],[UndirectedEdge(1,2),UndirectedEdge(2,3),UndirectedEdge(3,4),UndirectedEdge(4,1)])]\bigl\lbrack\mathrm{Graph}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(3, 4)\bigr\rbrack), \mathrm{Graph}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(2, 3), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(4, 1)\bigr\rbrack)\bigr\rbrack

A 2×2×22\times2\times2 grid is the cube graph Vertices are numbered with the first index varying fastest, as in Wolfram (our own convention, not a known value).

live ↗
In[GridGraph([2,2,2]),HypercubeGraph(3)]\bigl\lbrack\mathrm{GridGraph}(\bigl\lbrack2, 2, 2\bigr\rbrack), \mathrm{HypercubeGraph}(3)\bigr\rbrackOut[Graph([1,2,3,4,5,6,7,8],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(1,5),UndirectedEdge(2,4),UndirectedEdge(2,6),UndirectedEdge(3,4),UndirectedEdge(3,7),UndirectedEdge(4,8),UndirectedEdge(5,6),UndirectedEdge(5,7),UndirectedEdge(6,8),UndirectedEdge(7,8)]),Graph([1,2,3,4,5,6,7,8],[UndirectedEdge(1,2),UndirectedEdge(1,3),UndirectedEdge(1,5),UndirectedEdge(2,4),UndirectedEdge(2,6),UndirectedEdge(3,4),UndirectedEdge(3,7),UndirectedEdge(4,8),UndirectedEdge(5,6),UndirectedEdge(5,7),UndirectedEdge(6,8),UndirectedEdge(7,8)])]\bigl\lbrack\mathrm{Graph}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(1, 5), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(2, 6), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(3, 7), \mathrm{UndirectedEdge}(4, 8), \mathrm{UndirectedEdge}(5, 6), \mathrm{UndirectedEdge}(5, 7), \mathrm{UndirectedEdge}(6, 8), \mathrm{UndirectedEdge}(7, 8)\bigr\rbrack), \mathrm{Graph}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 3), \mathrm{UndirectedEdge}(1, 5), \mathrm{UndirectedEdge}(2, 4), \mathrm{UndirectedEdge}(2, 6), \mathrm{UndirectedEdge}(3, 4), \mathrm{UndirectedEdge}(3, 7), \mathrm{UndirectedEdge}(4, 8), \mathrm{UndirectedEdge}(5, 6), \mathrm{UndirectedEdge}(5, 7), \mathrm{UndirectedEdge}(6, 8), \mathrm{UndirectedEdge}(7, 8)\bigr\rbrack)\bigr\rbrack
About
Examples11
Checked againstWolfram Language 11 agreeSageMath 3 agree4 differ
CrosswalkWolfram LanguageSageMath

Implementation

mappedsagepackages/oracle/src/mappings.ts

See also: HypercubeGraph, CompleteGraph