MCPcopy
hub / github.com/dmlc/dgl / add_edges

Function add_edges

python/dgl/transforms/functional.py:1640–1748  ·  view source on GitHub ↗

r"""Add the edges to the graph and return a new graph. The i-th new edge will be from ``u[i]`` to ``v[i]``. The IDs of the new edges will start from ``g.num_edges(etype)``. Parameters ---------- u : int, Tensor or iterable[int] Source node IDs, ``u[i]`` gives the sourc

(g, u, v, data=None, etype=None)

Source from the content-addressed store, hash-verified

1638
1639
1640def add_edges(g, u, v, data=None, etype=None):
1641 r"""Add the edges to the graph and return a new graph.
1642
1643 The i-th new edge will be from ``u[i]`` to ``v[i]``. The IDs of the new
1644 edges will start from ``g.num_edges(etype)``.
1645
1646 Parameters
1647 ----------
1648 u : int, Tensor or iterable[int]
1649 Source node IDs, ``u[i]`` gives the source node for the i-th new edge.
1650 v : int, Tensor or iterable[int]
1651 Destination node IDs, ``v[i]`` gives the destination node for the i-th new edge.
1652 data : dict[str, Tensor], optional
1653 Feature data of the added edges. The keys are feature names
1654 while the values are feature data.
1655 etype : str or (str, str, str), optional
1656 The type names of the edges. The allowed type name formats are:
1657
1658 * ``(str, str, str)`` for source node type, edge type and destination node type.
1659 * or one ``str`` edge type name if the name can uniquely identify a
1660 triplet format in the graph.
1661
1662 Can be omitted if the graph has only one type of edges.
1663
1664 Return
1665 ------
1666 DGLGraph
1667 The graph with newly added edges.
1668
1669 Notes
1670 -----
1671 * If the end nodes of the given edges do not exist in :attr:`g`,
1672 :func:`dgl.add_nodes` is invoked to add those nodes.
1673 The node features of the new nodes will be filled with zeros.
1674 * For features in :attr:`g` but not in :attr:`data`,
1675 DGL assigns zero features for the newly added nodes.
1676 * For feature in :attr:`data` but not in :attr:`g`, DGL assigns zero features
1677 for the existing nodes in the graph.
1678 * This function discards the batch information. Please use
1679 :func:`dgl.DGLGraph.set_batch_num_nodes`
1680 and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph
1681 to maintain the information.
1682
1683 Examples
1684 --------
1685 The following example uses PyTorch backend.
1686
1687 >>> import dgl
1688 >>> import torch
1689
1690 **Homogeneous Graphs**
1691
1692 >>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
1693 >>> g.num_edges()
1694 2
1695 >>> g = dgl.add_edges(g, torch.tensor([1, 3]), torch.tensor([0, 1]))
1696 >>> g.num_edges()
1697 4

Callers 1

add_self_loopFunction · 0.85

Calls 2

cloneMethod · 0.45
add_edgesMethod · 0.45

Tested by

no test coverage detected