dgl.DGLGraph.edge_ids

DGLGraph.edge_ids(u, v, force_multi=None, return_uv=False)

Return all edge IDs between source node array u and destination node array v.

Parameters:
  • u (list, tensor) – The source node ID array.
  • v (list, tensor) – The destination node ID array.
  • force_multi (bool) – Deprecated (Will be deleted in the future). Whether to always treat the graph as a multigraph.
  • return_uv (bool) – Whether return e or (eu, ev, e)
Returns:

If ‘return_uv` is 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)

Notes

If the graph is a simple graph, return_uv is False, and no edge exist between some pairs of u[i] and v[i], the result is undefined.

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

Examples

The following example uses PyTorch backend.

For simple graphs:

>>> G = dgl.DGLGraph()
>>> G.add_nodes(3)
>>> G.add_edges([0, 0], [1, 2]) # (0, 1), (0, 2)
>>> G.edge_ids([0, 0], [2, 1])  # get edge ID of (0, 2) and (0, 1)
>>> G.edge_ids([0, 0], [2, 1])
tensor([1, 0])

For multigraphs

>>> G = dgl.DGLGraph()
>>> G.add_nodes(4)
>>> G.add_edges([0, 0, 0], [1, 1, 2])   # (0, 1), (0, 1), (0, 2)

Get all edges between (0, 1), (0, 2), (0, 3). Note that there is no edge between 0 and 3:

>>> G.edge_ids([0, 0, 0], [1, 2, 3], return_uv=True)
(tensor([0, 0, 0]), tensor([1, 1, 2]), tensor([0, 1, 2]))

See also

edge_id()