compose#
- compose(G, H)[source]#
Returns a new graph of G composed with H.
Composition is the simple union of the node sets and edge sets. The node sets of G and H do not need to be disjoint.
- Parameters
- G, Hgraph
A NetworkX graph
- Returns
- C: A new graph with the same type as G
Notes
It is recommended that G and H be either both directed or both undirected. Attributes from H take precedent over attributes from G.
For MultiGraphs, the edges are identified by incident nodes AND edge-key. This can cause surprises (i.e., edge
(1, 2)
may or may not be the same in two graphs) if you use MultiGraph without keeping track of edge keys.Examples
>>> G = nx.Graph([(0, 1), (0, 2)]) >>> H = nx.Graph([(0, 1), (1, 2)]) >>> R = nx.compose(G, H) >>> R.nodes NodeView((0, 1, 2)) >>> R.edges EdgeView([(0, 1), (0, 2), (1, 2)])
By default, the attributes from
H
take precedent over attributes fromG
. If you prefer another way of combining attributes, you can update them after the compose operation:>>> G = nx.Graph([(0, 1, {'weight': 2.0}), (3, 0, {'weight': 100.0})]) >>> H = nx.Graph([(0, 1, {'weight': 10.0}), (1, 2, {'weight': -1.0})]) >>> nx.set_node_attributes(G, {0: 'dark', 1: 'light', 3: 'black'}, name='color') >>> nx.set_node_attributes(H, {0: 'green', 1: 'orange', 2: 'yellow'}, name='color') >>> GcomposeH = nx.compose(G, H)
Normally, color attribute values of nodes of GcomposeH come from H. We can workaround this as follows:
>>> node_data = {n: G.nodes[n]['color'] + " " + H.nodes[n]['color'] for n in G.nodes & H.nodes} >>> nx.set_node_attributes(GcomposeH, node_data, 'color') >>> print(GcomposeH.nodes[0]['color']) dark green
>>> print(GcomposeH.nodes[3]['color']) black
Similarly, we can update edge attributes after the compose operation in a way we prefer:
>>> edge_data = {e: G.edges[e]['weight'] * H.edges[e]['weight'] for e in G.edges & H.edges} >>> nx.set_edge_attributes(GcomposeH, edge_data, 'weight') >>> print(GcomposeH.edges[(0, 1)]['weight']) 20.0
>>> print(GcomposeH.edges[(3, 0)]['weight']) 100.0