r"""Convert the graph to a bi-directional simple graph and return. For an input graph :math:`G`, return a new graph :math:`G'` such that an edge :math:`(u, v)\in G'` exists if and only if there exists an edge :math:`(v, u)\in G`. The resulting graph :math:`G'` is a simple graph, mea
(g, copy_ndata=False, readonly=None)
| 786 | |
| 787 | |
| 788 | def to_bidirected(g, copy_ndata=False, readonly=None): |
| 789 | r"""Convert the graph to a bi-directional simple graph and return. |
| 790 | |
| 791 | For an input graph :math:`G`, return a new graph :math:`G'` such that an edge |
| 792 | :math:`(u, v)\in G'` exists if and only if there exists an edge |
| 793 | :math:`(v, u)\in G`. The resulting graph :math:`G'` is a simple graph, |
| 794 | meaning there is no parallel edge. |
| 795 | |
| 796 | The operation only works for edges whose two endpoints belong to the same node type. |
| 797 | DGL will raise error if the input graph is heterogeneous and contains edges |
| 798 | with different types of endpoints. |
| 799 | |
| 800 | Parameters |
| 801 | ---------- |
| 802 | g : DGLGraph |
| 803 | The input graph. |
| 804 | copy_ndata: bool, optional |
| 805 | If True, the node features of the bidirected graph are copied from the |
| 806 | original graph. If False, the bidirected graph will not have any node features. |
| 807 | (Default: False) |
| 808 | readonly : bool |
| 809 | **DEPRECATED**. |
| 810 | |
| 811 | Returns |
| 812 | ------- |
| 813 | DGLGraph |
| 814 | The bidirected graph |
| 815 | |
| 816 | Notes |
| 817 | ----- |
| 818 | If :attr:`copy_ndata` is True, the resulting graph will share the node feature |
| 819 | tensors with the input graph. Hence, users should try to avoid in-place operations |
| 820 | which will be visible to both graphs. |
| 821 | |
| 822 | This function discards the batch information. Please use |
| 823 | :func:`dgl.DGLGraph.set_batch_num_nodes` |
| 824 | and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph |
| 825 | to maintain the information. |
| 826 | |
| 827 | Examples |
| 828 | -------- |
| 829 | The following examples use PyTorch backend. |
| 830 | |
| 831 | >>> import dgl |
| 832 | >>> import torch as th |
| 833 | >>> g = dgl.graph((th.tensor([0, 1, 2]), th.tensor([1, 2, 0]))) |
| 834 | >>> bg1 = dgl.to_bidirected(g) |
| 835 | >>> bg1.edges() |
| 836 | (tensor([0, 1, 2, 1, 2, 0]), tensor([1, 2, 0, 0, 1, 2])) |
| 837 | |
| 838 | The graph already have i->j and j->i |
| 839 | |
| 840 | >>> g = dgl.graph((th.tensor([0, 1, 2, 0]), th.tensor([1, 2, 0, 2]))) |
| 841 | >>> bg1 = dgl.to_bidirected(g) |
| 842 | >>> bg1.edges() |
| 843 | (tensor([0, 1, 2, 1, 2, 0]), tensor([1, 2, 0, 0, 1, 2])) |
| 844 | |
| 845 | **Heterogeneous graphs with Multiple Edge Types** |
no test coverage detected