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]
andv[i]
.Parameters: 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
withmxnet.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