rustworkx.PyDiGraph.contract_nodes#
- PyDiGraph.contract_nodes(nodes, obj, /, check_cycle=None, weight_combo_fn=None)#
Substitute a set of nodes with a single new node.
- Parameters:
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 toFalse
, validation is skipped. If not provided, inherits the value ofcheck_cycle
from this instance ofPyDiGraph
.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 innodes
have an outgoing edge to the same target node. If this instance ofPyDiGraph
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.
- Returns:
The index of the newly created node.
- Raises:
DAGWouldCycle – The cycle check is enabled and the contraction would introduce cycle(s).