r"""Merge a sequence of graphs together into a single graph. Nodes and edges that exist in ``graphs[i+1]`` but not in ``dgl.merge(graphs[0:i+1])`` will be added to ``dgl.merge(graphs[0:i+1])`` along with their data. Nodes that exist in both ``dgl.merge(graphs[0:i+1])`` and ``graphs[i+1]
(graphs)
| 9 | |
| 10 | |
| 11 | def merge(graphs): |
| 12 | r"""Merge a sequence of graphs together into a single graph. |
| 13 | |
| 14 | Nodes and edges that exist in ``graphs[i+1]`` but not in ``dgl.merge(graphs[0:i+1])`` |
| 15 | will be added to ``dgl.merge(graphs[0:i+1])`` along with their data. |
| 16 | Nodes that exist in both ``dgl.merge(graphs[0:i+1])`` and ``graphs[i+1]`` |
| 17 | will be updated with ``graphs[i+1]``'s data if they do not match. |
| 18 | |
| 19 | Parameters |
| 20 | ---------- |
| 21 | graphs : list[DGLGraph] |
| 22 | Input graphs. |
| 23 | |
| 24 | Returns |
| 25 | ------- |
| 26 | DGLGraph |
| 27 | The merged graph. |
| 28 | |
| 29 | Notes |
| 30 | ---------- |
| 31 | * Inplace updates are applied to a new, empty graph. |
| 32 | * Features that exist in ``dgl.graphs[i+1]`` will be created in |
| 33 | ``dgl.merge(dgl.graphs[i+1])`` if they do not already exist. |
| 34 | |
| 35 | Examples |
| 36 | ---------- |
| 37 | |
| 38 | The following example uses PyTorch backend. |
| 39 | |
| 40 | >>> import dgl |
| 41 | >>> import torch |
| 42 | >>> g = dgl.graph((torch.tensor([0,1]), torch.tensor([2,3]))) |
| 43 | >>> g.ndata["x"] = torch.zeros(4) |
| 44 | >>> h = dgl.graph((torch.tensor([1,2]), torch.tensor([0,4]))) |
| 45 | >>> h.ndata["x"] = torch.ones(5) |
| 46 | >>> m = dgl.merge([g, h]) |
| 47 | |
| 48 | ``m`` now contains edges and nodes from ``h`` and ``g``. |
| 49 | |
| 50 | >>> m.edges() |
| 51 | (tensor([0, 1, 1, 2]), tensor([2, 3, 0, 4])) |
| 52 | >>> m.nodes() |
| 53 | tensor([0, 1, 2, 3, 4]) |
| 54 | |
| 55 | ``g``'s data has updated with ``h``'s in ``m``. |
| 56 | |
| 57 | >>> m.ndata["x"] |
| 58 | tensor([1., 1., 1., 1., 1.]) |
| 59 | |
| 60 | See Also |
| 61 | ---------- |
| 62 | add_nodes |
| 63 | add_edges |
| 64 | """ |
| 65 | |
| 66 | if len(graphs) == 0: |
| 67 | raise DGLError("The input list of graphs cannot be empty.") |
| 68 |
nothing calls this directly
no test coverage detected