This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.


PyDAG.contract_nodes(nodes, obj, /, check_cycle=None, weight_combo_fn=None)#

Substitute a set of nodes with a single new node.

  • nodes (list) – A set of nodes to be removed and replaced by the new node. Any nodes not in the graph are ignored. If empty, this method behaves like add_node() (but slower).

  • obj (object) – The data/weight to associate with the new node.

  • check_cycle (bool) – If set to True, validates that the contraction will not introduce cycles before modifying the graph. If set to False, validation is skipped. If not provided, inherits the value of check_cycle from this instance of PyDiGraph.

  • weight_combo_fn – An optional python callable that, when specified, is used to merge parallel edges introduced by the contraction, which will occur when multiple nodes in nodes have an incoming edge from the same source node or when multiple nodes in nodes have an outgoing edge to the same target node. If this instance of PyDiGraph is a multigraph, leave this unspecified to preserve parallel edges. If unspecified when not a multigraph, parallel edges and their weights will be combined by choosing one of the edge’s weights arbitrarily based on an internal iteration order, subject to change.


The index of the newly created node.


DAGWouldCycle – The cycle check is enabled and the contraction would introduce cycle(s).