retworkx.generators.directed_star_graph¶
- directed_star_graph(num_nodes=None, weights=None, inward=False, bidirectional=False, /)¶
Generate a directed star graph
- Parameters
num_node (int) – The number of nodes to generate the graph with. Node weights will be None if this is specified. If both
num_node
andweights
are set this will be ignored andweights
will be used.weights (list) – A list of node weights, the first element in the list will be the center node of the star graph. If both
num_node
andweights
are set this will be ignored andweights
will be used.bidirectional (bool) – Adds edges in both directions between two nodes if set to
True
. Default value isFalse
.inward (bool) – If set
True
the nodes will be directed towards the center node. This parameter is ignored ifbidirectional
is set toTrue
.
- Returns
The generated star graph
- Return type
- Raises
IndexError – If neither
num_nodes
orweights
are specified
import os import tempfile import pydot from PIL import Image import retworkx.generators graph = retworkx.generators.directed_star_graph(10) 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
import os import tempfile import pydot from PIL import Image import retworkx.generators graph = retworkx.generators.directed_star_graph(10, inward=True) 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