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

Return all edge IDs between source node array u and destination node array v with the specified edge type.

  • u (list, tensor) – The node ID array of source type.
  • v (list, tensor) – The node ID array of destination type.
  • force_multi (bool, optional) – Deprecated (Will be deleted in the future). Whether to always treat the graph as a multigraph. See the “Returns” for their effects. (Default: False)
  • return_uv (bool) – See the “Returns” for their effects. (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.

  • If return_uv=False, return a single edge ID array e.

e[i] is the edge ID between u[i] and v[i].

  • Otherwise, return three arrays (eu, ev, e). e[i] is the ID

of an edge between eu[i] and ev[i]. All edges between u[i] and v[i] are returned.

Return type:

tensor, or (tensor, tensor, tensor)


If the graph is a simple graph, return_uv=False, and no edge exists between some pairs of u[i] and v[i], the result is undefined and an empty tensor is returned.

If the graph is a multi graph, return_uv=False, and multi edges exist between some pairs of u[i] and v[i], the result is undefined.


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 ids.

>>> plays_g.edge_ids([0], [2], etype=('user', 'plays', 'game'))
tensor([], dtype=torch.int64)
>>> plays_g.edge_ids([1], [2], etype=('user', 'plays', 'game'))
>>> g.edge_ids([1], [2], return_uv=True, etype=('user', 'follows', 'user'))
(tensor([1, 1]), tensor([2, 2]), tensor([1, 2]))

See also