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
intofirst
. operates in \(\mathcal{O}(n_2)\), with \(n_2\) being number of nodes insecond
. 2. Merge nodes fromsecond
overfirst
given that:The
merge_nodes
isTrue
. operates in \(\mathcal{O}(n_1 n_2)\), with \(n_1\) being the number of nodes infirst
and \(n_2\) the number of nodes insecond
The respective node in
second
andfirst
share the same weight/data payload.
Adds all the edges from
second
tofirst
. If themerge_edges
parameter isTrue
and the respective edge insecond
andfirst
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 betweensecond
andfirst
if the weights are equal. Default:False
.merge_edges (bool) – If set to
True
edges will be merged betweensecond
andfirst
if the weights are equal. Default:False
.
- Returns:
A new graph object that is the union of
second
andfirst
. It’s worth noting the weight/data payload objects are passed by reference fromfirst
andsecond
to this new object.- Return type: