Merge a sequence of graphs together into a single graph.

Nodes and edges that exist in graphs[i+1] but not in dgl.merge(graphs[0:i+1]) will be added to dgl.merge(graphs[0:i+1]) along with their data. Nodes that exist in both dgl.merge(graphs[0:i+1]) and graphs[i+1] will be updated with graphs[i+1]’s data if they do not match.


graphs (list[DGLGraph]) – Input graphs.


The merged graph.

Return type



  • Inplace updates are applied to a new, empty graph.

  • Features that exist in dgl.graphs[i+1] will be created in dgl.merge(dgl.graphs[i+1]) if they do not already exist.


The following example uses PyTorch backend.

>>> import dgl
>>> import torch
>>> g = dgl.graph((torch.tensor([0,1]), torch.tensor([2,3])))
>>> g.ndata["x"] = torch.zeros(4)
>>> h = dgl.graph((torch.tensor([1,2]), torch.tensor([0,4])))
>>> h.ndata["x"] = torch.ones(5)
>>> m = dgl.merge([g, h])

m now contains edges and nodes from h and g.

>>> m.edges()
(tensor([0, 1, 1, 2]), tensor([2, 3, 0, 4]))
>>> m.nodes()
tensor([0, 1, 2, 3, 4])

g’s data has updated with h’s in m.

>>> m.ndata["x"]
tensor([1., 1., 1., 1., 1.])

See also

add_nodes, add_edges