dgl.DGLGraph.has_edges_between

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

返回图是否包含给定的边。

Parameters:
  • u (节点ID) –

    边的源节点ID。允许的格式有:

    • int: 单个节点。

    • Int Tensor: 每个元素都是一个节点ID。张量必须具有与图相同的设备类型和ID数据类型。

    • iterable[int]: 每个元素都是一个节点ID。

  • v (节点ID) –

    边的目标节点ID。允许的格式有:

    • int: 单个节点。

    • Int Tensor: 每个元素都是一个节点ID。张量必须具有与图相同的设备类型和ID数据类型。

    • iterable[int]: 每个元素都是一个节点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:

一个布尔标志的张量,如果节点在图中,则每个元素为True。 如果输入是单个节点,则返回一个布尔值。

Return type:

bool 或布尔张量

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

创建一个同构图。

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

查询边的请求。

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

如果图具有多种边类型,则需要指定边类型。

>>> 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])

当边类型存在歧义时,请使用规范的边类型。

>>> 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])