rustworkx.PyGraph.contract_nodes#

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

Substitute a set of nodes with a single new node.

Note

This method does not preserve the ordering of endpoints in edge tuple representations (e.g. the tuples returned from edge_list()).

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.

  • weight_combo_fn – An optional python callable that, when specified, is used to merge parallel edges introduced by the contraction, which will occur if any two edges between nodes and the rest of the graph share an endpoint. If this instance of PyGraph 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.