dgl.transform.to_bidirected

dgl.transform.to_bidirected(g, readonly=True)[source]

Convert the graph to a bidirected graph.

The function generates a new graph with no node/edge feature. If g has an edge for i->j but no edge for j->i, then the returned graph will have both i->j and j->i.

If the input graph is a multigraph (there are multiple edges from node i to node j), the returned graph isn’t well defined.

Parameters:
  • g (DGLGraph) – The input graph.
  • readonly (bool, default to be True) – Whether the returned bidirected graph is readonly or not.

Notes

Please make sure g is a single graph, otherwise the return value is undefined.

Returns:
Return type:DGLGraph

Examples

The following two examples use PyTorch backend, one for non-multi graph and one for multi-graph.

>>> g = dgl.DGLGraph()
>>> g.add_nodes(2)
>>> g.add_edges([0, 0], [0, 1])
>>> bg1 = dgl.to_bidirected(g)
>>> bg1.edges()
(tensor([0, 1, 0]), tensor([0, 0, 1]))