dgl.DGLGraph.add_edge

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

Add one new edge between u and v.

Parameters:
  • 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.

Notes

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

Examples

The following example uses PyTorch backend.

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

Adding new edge with features

Note

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

add_edges()