DGLGraph.add_edge(u, v, data=None)[source]

Add one new edge between u and v.

  • u (int) – The source node ID. Must exist in the graph.
  • v (int) – The destination node ID. Must exist in the graph.
  • data (dict, optional) – Feature data of the added edges.


If new edges are added with features, and any of the old edges do not have some of the feature fields, those fields are filled by initializers defined with set_e_initializer (default filling with zeros).


The following example uses PyTorch backend.

>>> G = dgl.DGLGraph()
>>> G.add_nodes(3)
>>> G.add_edge(0, 1)

Adding new edge with features


Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> G.add_edge(0, 2, {'x': th.ones(1, 4)})
>>> G.edges()
(tensor([0, 0]), tensor([1, 2]))
>>> G.edata['x']
tensor([[0., 0., 0., 0.],
        [1., 1., 1., 1.]])
>>> G.edges[0, 2].data['x']
tensor([[1., 1., 1., 1.]])

See also