dgl.DGLGraph.add_edges

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

Add multiple edges for list of source nodes u and destination nodes v. A single edge is added between every pair of u[i] and v[i].

Parameters:
  • u (list, tensor) – The source node IDs. All nodes must exist in the graph.
  • v (list, tensor) – The destination node IDs. All nodes 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(4)
>>> G.add_edges([0, 2], [1, 3]) # add edges (0, 1) and (2, 3)

Adding new edges 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_edges([1, 3], [2, 0], {'x': th.ones(2, 4)}) # (1, 2), (3, 0)
>>> G.edata['x']
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])

See also

add_edge()