DGLHeteroGraph.edge_id(u, v, force_multi=False, etype=None)[source]

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

  • u (int) – The node ID of source type.
  • v (int) – The node ID of destination type.
  • force_multi (bool, optional) – If False, will return a single edge ID if the graph is a simple graph. If True, will always return an array. (Default: False)
  • etype (str or tuple of str, optional) – The edge type. Can be omitted if there is only one edge type in the graph.

The edge ID if force_multi == True and the graph is a simple graph. The edge ID array otherwise.

Return type:

int or tensor


The following example uses PyTorch backend.

Instantiate a heterograph.

>>> plays_g = dgl.bipartite([(0, 0), (1, 0), (1, 2), (2, 1)], 'user', 'plays', 'game')
>>> follows_g = dgl.graph([(0, 1), (1, 2), (1, 2)], 'user', 'follows')
>>> g = dgl.hetero_from_relations([plays_g, follows_g])

Query for edge id.

>>> plays_g.edge_id(1, 2, etype=('user', 'plays', 'game'))
>>> g.edge_id(1, 2, force_multi=True, etype=('user', 'follows', 'user'))
tensor([1, 2])

See also