Warning
The documentation for retworkx has migrated to:
https://qiskit.org/documentation/retworkx
These docs will no longer be updated.
retworkx.generators.directed_grid_graph¶
- directed_grid_graph(rows=None, cols=None, weights=None, bidirectional=False, /)¶
- Generate a directed grid graph. The edges propagate towards right and
bottom direction if
bidirectional
isfalse
- Parameters
rows (int) – The number of rows to generate the graph with. If specified, cols also need to be specified.
cols (list) – The number of rows to generate the graph with. If specified, rows also need to be specified. rows*cols defines the number of nodes in the graph.
weights (list) – A list of node weights. Nodes are filled row wise. If rows and cols are not specified, then a linear graph containing all the values in weights list is created. If number of nodes(rows*cols) is less than length of weights list, the trailing weights are ignored. If number of nodes(rows*cols) is greater than length of weights list, extra nodes with None weight are appended.
bidirectional – A parameter to indicate if edges should exist in both directions between nodes
- Returns
The generated grid graph
- Return type
- Raises
IndexError – If neither
rows
orcols
andweights
are specified
import os import tempfile import pydot from PIL import Image import retworkx.generators graph = retworkx.generators.directed_grid_graph(2, 3) dot_str = graph.to_dot( lambda node: dict( color='black', fillcolor='lightblue', style='filled')) dot = pydot.graph_from_dot_data(dot_str)[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'dag.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image