def grid_graph(dim, periodic=False): """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 """ from networkx.algorithms.operators.product import cartesian_product if not dim: return empty_graph(0) if iterable(periodic): func = (cycle_graph if p else path_graph for p in periodic) else: func = repeat(cycle_graph if periodic else path_graph) G = next(func)(dim[0]) for current_dim in dim[1:]: Gnew = next(func)(current_dim) G = cartesian_product(Gnew, G) # graph G is done but has labels of the form (1, (2, (3, 1))) so relabel H = relabel_nodes(G, flatten) return H
def test_iterable(): assert not iterable(None) assert not iterable(10) assert iterable([1, 2, 3]) assert iterable((1, 2, 3)) assert iterable({1: "A", 2: "X"}) assert iterable("ABC")
def grid_2d_graph(m, n, periodic=False, create_using=None): """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. """ G = empty_graph(0, create_using) row_name, rows = m col_name, cols = n G.add_nodes_from((i, j) for i in rows for j in cols) G.add_edges_from( ((i, j), (pi, j)) for pi, i in pairwise(rows) for j in cols) G.add_edges_from( ((i, j), (i, pj)) for i in rows for pj, j in pairwise(cols)) if iterable(periodic): periodic_r, periodic_c = periodic else: periodic_r = periodic_c = periodic if periodic_r and len(rows) > 2: first = rows[0] last = rows[-1] G.add_edges_from(((first, j), (last, j)) for j in cols) if periodic_c and len(cols) > 2: first = cols[0] last = cols[-1] G.add_edges_from(((i, first), (i, last)) for i in rows) # both directions for directed if G.is_directed(): G.add_edges_from((v, u) for u, v in G.edges()) return G
def test_graph_iterable(): K = nx.complete_graph(10) assert iterable(K) assert iterable(K.nodes()) assert iterable(K.edges())