Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.generators.lattice.grid_graph

grid_graph(dim, periodic=False)[source]

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) – If periodic is True the nodes on the grid boundaries are joined to the corresponding nodes on the opposite grid boundaries.
Returns:

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

Return type:

NetworkX graph

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