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.

G, Hgraph

A NetworkX graph

C: A new graph with the same type as G


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.


>>> 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)])