rustworkx.PyDiGraph.compose#
- PyDiGraph.compose(other, node_map, /, node_map_func=None, edge_map_func=None)#
将该PyDiGraph对象添加到当前PyDiGraph中
- Parameters:
other (PyDiGraph) – 要添加到此图中的另一个 PyDiGraph 对象。
node_map (dict[int, tuple[int, tuple[int, T]]]) –
一个字典 将本 PyDiGraph 对象中的节点索引 映射到另一个 PyDiGraph 对象中的节点索引。 键是当前图中的一个节点索引,值是一个元组, 包含另一个图中要添加边的目标节点索引及其边的权重。 例如:
{ 1: (2, "weight"), 2: (4, "weight2") }
node_map_func (Callable) – 一个可选的Python可调用对象,它将接收一个节点权重/数据对象,并返回新的节点权重/数据对象,该对象将在从其他图添加节点到此图时使用。
edge_map_func (Callable) – 一个可选的Python可调用对象,它将接收单个边权重/数据对象并返回一个新的边权重/数据对象,该对象将在从其他图添加边到此图时使用。
- Returns:
new_node_ids: 存储从另一个PyDiGraph节点索引到合并后对应此PyDAG节点索引的字典
- Return type:
字典[整型, 整型]
举个例子,首先构建一个图:
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)
接下来构建第二个:
# 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)
最后将
other_graph合并到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)