r"""Add multiple new edges for the specified edge type The i-th new edge will be from ``u[i]`` to ``v[i]``. Parameters ---------- u : int, tensor, numpy.ndarray, list Source node IDs, ``u[i]`` gives the source node for the i-th new edge. v : int,
(self, u, v, data=None, etype=None)
| 433 | self._reset_cached_info() |
| 434 | |
| 435 | def add_edges(self, u, v, data=None, etype=None): |
| 436 | r"""Add multiple new edges for the specified edge type |
| 437 | |
| 438 | The i-th new edge will be from ``u[i]`` to ``v[i]``. |
| 439 | |
| 440 | Parameters |
| 441 | ---------- |
| 442 | u : int, tensor, numpy.ndarray, list |
| 443 | Source node IDs, ``u[i]`` gives the source node for the i-th new edge. |
| 444 | v : int, tensor, numpy.ndarray, list |
| 445 | Destination node IDs, ``v[i]`` gives the destination node for the i-th new edge. |
| 446 | data : dict, optional |
| 447 | Feature data of the added edges. The i-th row of the feature data |
| 448 | corresponds to the i-th new edge. |
| 449 | etype : str or tuple of str, optional |
| 450 | The type of the new edges. Can be omitted if there is |
| 451 | only one edge type in the graph. |
| 452 | |
| 453 | Notes |
| 454 | ----- |
| 455 | |
| 456 | * Inplace update is applied to the current graph. |
| 457 | * If end nodes of adding edges does not exists, add_nodes is invoked |
| 458 | to add new nodes. The node features of the new nodes will be created |
| 459 | by initializers defined with :func:`set_n_initializer` (default |
| 460 | initializer fills zeros). In certain cases, it is recommanded to |
| 461 | add_nodes first and then add_edges. |
| 462 | * If the key of ``data`` does not contain some existing feature fields, |
| 463 | those features for the new edges will be created by initializers |
| 464 | defined with :func:`set_n_initializer` (default initializer fills zeros). |
| 465 | * If the key of ``data`` contains new feature fields, those features for |
| 466 | the old edges will be created by initializers defined with |
| 467 | :func:`set_n_initializer` (default initializer fills zeros). |
| 468 | * This function discards the batch information. Please use |
| 469 | :func:`dgl.DGLGraph.set_batch_num_nodes` |
| 470 | and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph |
| 471 | to maintain the information. |
| 472 | |
| 473 | Examples |
| 474 | -------- |
| 475 | |
| 476 | The following example uses PyTorch backend. |
| 477 | |
| 478 | >>> import dgl |
| 479 | >>> import torch |
| 480 | |
| 481 | **Homogeneous Graphs or Heterogeneous Graphs with A Single Edge Type** |
| 482 | |
| 483 | >>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2]))) |
| 484 | >>> g.num_edges() |
| 485 | 2 |
| 486 | >>> g.add_edges(torch.tensor([1, 3]), torch.tensor([0, 1])) |
| 487 | >>> g.num_edges() |
| 488 | 4 |
| 489 | |
| 490 | Since ``u`` or ``v`` contains a non-existing node ID, the nodes are |
| 491 | added implicitly. |
| 492 | >>> g.num_nodes() |