rustworkx.PyDAG.compose#

PyDAG.compose(other, node_map, /, node_map_func=None, edge_map_func=None)#

Add another PyDiGraph object into this PyDiGraph

Parameters:
  • other (PyDiGraph) – The other PyDiGraph object to add onto this graph.

  • node_map (dict) –

    A dictionary mapping node indices from this PyDiGraph object to node indices in the other PyDiGraph object. The keys are a node index in this graph and the value is a tuple of the node index in the other graph to add an edge to and the weight of that edge. For example:

    {
        1: (2, "weight"),
        2: (4, "weight2")
    }
    

  • node_map_func – An optional python callable that will take in a single node weight/data object and return a new node weight/data object that will be used when adding an node from other onto this graph.

  • edge_map_func – An optional python callable that will take in a single edge weight/data object and return a new edge weight/data object that will be used when adding an edge from other onto this graph.

Returns:

new_node_ids: A dictionary mapping node index from the other PyDiGraph to the corresponding node index in this PyDAG after they’ve been combined

Return type:

dict

For example, start by building a graph:

import rustworkx as rx
from rustworkx.visualization import mpl_draw

# Build first graph and visualize:
graph = rx.PyDiGraph()
node_a = graph.add_node('A')
node_b = graph.add_child(node_a, 'B', 'A to B')
node_c = graph.add_child(node_b, 'C', 'B to C')
mpl_draw(graph, with_labels=True, labels=str, edge_labels=str)
../_images/rustworkx.PyDAG.compose_0_0.png

Then build a second one:

# Build second graph and visualize:
other_graph = rx.PyDiGraph()
node_d = other_graph.add_node('D')
other_graph.add_child(node_d, 'E', 'D to E')
mpl_draw(other_graph, with_labels=True, labels=str, edge_labels=str)
../_images/rustworkx.PyDAG.compose_1_0.png

Finally compose the other_graph onto graph

node_map = {node_b: (node_d, 'B to D')}
graph.compose(other_graph, node_map)
mpl_draw(graph, with_labels=True, labels=str, edge_labels=str)
../_images/rustworkx.PyDAG.compose_2_0.png