rustworkx.union#

union(first, second, merge_nodes=False, merge_edges=False)[source]#

Return a new graph by forming a union from two input graph objects

The algorithm in this function operates in three phases:

1. Add all the nodes from second into first. operates in \(\mathcal{O}(n_2)\), with \(n_2\) being number of nodes in second. 2. Merge nodes from second over first given that:

  • The merge_nodes is True. operates in \(\mathcal{O}(n_1 n_2)\), with \(n_1\) being the number of nodes in first and \(n_2\) the number of nodes in second

  • The respective node in second and first share the same weight/data payload.

  1. Adds all the edges from second to first. If the merge_edges parameter is True and the respective edge in second and first share the same weight/data payload they will be merged together.

Parameters:
  • first – The first graph object

  • second – The second graph object

  • merge_nodes (bool) – If set to True nodes will be merged between second and first if the weights are equal. Default: False.

  • merge_edges (bool) – If set to True edges will be merged between second and first if the weights are equal. Default: False.

Returns:

A new graph object that is the union of second and first. It’s worth noting the weight/data payload objects are passed by reference from first and second to this new object.

Return type:

PyGraph or PyDiGraph