networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
compose(G, 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.

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 :None:None:`(1, 2)` may or may not be the same in two graphs) if you use MultiGraph without keeping track of edge keys.

Parameters

G, H : graph

A NetworkX graph

Returns

C: A new graph with the same type as G

Returns a new graph of G composed with H.

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 from G. 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
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.operators.binary.compose

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/operators/binary.py#279
type: <class 'function'>
Commit: