dgl.DGLGraph.edge_id

DGLGraph.edge_id(u, v, force_multi=None, return_array=False)

Return the edge ID, or an array of edge IDs, between source node u and destination node v.

Parameters:
  • u (int) – The source node ID.
  • v (int) – The destination node ID.
  • force_multi (bool) – Deprecated (Will be deleted in the future). If False, will return a single edge ID. If True, will always return an array.
  • return_array (bool) – If False, will return a single edge ID. If True, will always return an array.
Returns:

The edge ID if return_array is False. The edge ID array otherwise.

Return type:

int or tensor

Notes

If multiply edges exist between u and v and return_array is False, the result is undefined.

Examples

The following example uses PyTorch backend.

For simple graphs:

>>> G = dgl.DGLGraph()
>>> G.add_node(3)
>>> G.add_edges([0, 0], [1, 2]) # (0, 1), (0, 2)
>>> G.edge_id(0, 2)
1
>>> G.edge_id(0, 1)
0

For multigraphs:

>>> G = dgl.DGLGraph()
>>> G.add_nodes(3)

Adding edges (0, 1), (0, 2), (0, 1), (0, 2), so edge ID 0 and 2 both connect from 0 and 1, while edge ID 1 and 3 both connect from 0 and 2.

>>> G.add_edges([0, 0, 0, 0], [1, 2, 1, 2])
>>> G.edge_id(0, 1, return_array=True)
tensor([0, 2])

See also

edge_ids()