dgl.DGLGraph.has_edges_between

DGLGraph.has_edges_between(u, v, etype=None)[source]

Return whether the graph contains the given edges.

Parameters
  • u (node IDs) –

    The source node IDs of the edges. The allowed formats are:

    • int: A single node.

    • Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is a node ID.

  • v (node IDs) –

    The destination node IDs of the edges. The allowed formats are:

    • int: A single node.

    • Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is a node ID.

  • etype (str or (str, str, str), optional) –

    The type names of the edges. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

A tensor of bool flags where each element is True if the node is in the graph. If the input is a single node, return one bool value.

Return type

bool or bool Tensor

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a homogeneous graph.

>>> g = dgl.graph((torch.tensor([0, 0, 1, 1]), torch.tensor([1, 0, 2, 3])))

Query for the edges.

>>> g.has_edges_between(1, 2)
True
>>> g.has_edges_between(torch.tensor([1, 2]), torch.tensor([2, 3]))
tensor([ True, False])

If the graph has multiple edge types, one need to specify the edge type.

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
...     ('user', 'follows', 'game'): (torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3])),
...     ('user', 'plays', 'game'): (torch.tensor([1, 3]), torch.tensor([2, 3]))
... })
>>> g.has_edges_between(torch.tensor([1, 2]), torch.tensor([2, 3]), 'plays')
tensor([ True, False])

Use a canonical edge type instead when there is ambiguity for an edge type.

>>> g.has_edges_between(torch.tensor([1, 2]), torch.tensor([2, 3]),
...                     ('user', 'follows', 'user'))
tensor([ True, False])
>>> g.has_edges_between(torch.tensor([1, 2]), torch.tensor([2, 3]),
...                     ('user', 'follows', 'game'))
tensor([True, True])