dgl.metapath_reachable_graph

功能

返回的是由metapath的起点和终点组成的边;新的graph的node num始终和metapath中起点type的nodes的数目一致(当metapath的起点终点是同一个type);所以最终graph中只有metapath起点node type和终点node type的nodes集合

    """Return a graph where the successors of any node ``u`` are nodes reachable from ``u`` by
    the given metapath.

    If the beginning node type ``s`` and ending node type ``t`` are the same, it will return
    a homogeneous graph with node type ``s = t``.  Otherwise, a unidirectional bipartite graph
    with source node type ``s`` and destination node type ``t`` is returned.

    In both cases, two nodes ``u`` and ``v`` will be connected with an edge ``(u, v)`` if
    there exists one path matching the metapath from ``u`` to ``v``.

    The result graph keeps the node set of type ``s`` and ``t`` in the original graph even if
    they might have no neighbor.

    The features of the source/destination node type in the original graph would be copied to
    the new graph.

    Parameters
    ----------
    g : DGLGraph
        The input graph
    metapath : list[str or tuple of str]
        Metapath in the form of a list of edge types

    Returns
    -------
    DGLGraph
        A homogeneous or unidirectional bipartite graph. It will be on CPU regardless of
        whether the input graph is on CPU or GPU.

    Notes
    -----

    This function discards the batch information. Please use
    :func:`dgl.DGLGraph.set_batch_num_nodes`
    and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph
    to maintain the information.

    Examples
    --------
    >>> g = dgl.heterograph({
    ...     ('A', 'AB', 'B'): ([0, 1, 2], [1, 2, 3]),
    ...     ('B', 'BA', 'A'): ([1, 2, 3], [0, 1, 2])})
    >>> new_g = dgl.metapath_reachable_graph(g, ['AB', 'BA'])
    >>> new_g.edges(order='eid')
    (tensor([0, 1, 2]), tensor([0, 1, 2]))
    """

示例

g = dgl.heterograph({
    ('A', 'AB', 'B'): ([0, 0,1], [0,1,1]),
    ('B', 'BA', 'A'): ([0,1,1], [0,0,1]),
    ('B', 'BC', 'C'): ([0], [0]),
    ('C', 'CB', 'B'): ([0], [0])})
new_g = dgl.metapath_reachable_graph(g, ['AB'])
new_g.edges(order='eid')
print(new_g)
>>>Graph(num_nodes={'A': 2, 'B': 2},
      num_edges={('A', '_E', 'B'): 3},
      metagraph=[('A', 'B', '_E')])
g = dgl.heterograph({
    ('A', 'AB', 'B'): ([0, 0,1], [0,1,1]),
    ('B', 'BA', 'A'): ([0,1,1], [0,0,1]),
    ('B', 'BC', 'C'): ([0], [0]),
    ('C', 'CB', 'B'): ([0], [0])})
new_g = dgl.metapath_reachable_graph(g, ['AB','BA'])
new_g.edges(order='eid')
print(new_g)
>>>Graph(num_nodes=2, num_edges=4,
      ndata_schemes={}
      edata_schemes={})
g = dgl.heterograph({
    ('A', 'AB', 'B'): ([0, 0,1], [0,1,1]),
    ('B', 'BA', 'A'): ([0,1,1], [0,0,1]),
    ('B', 'BC', 'C'): ([0], [0]),
    ('C', 'CB', 'B'): ([0], [0])})
new_g = dgl.metapath_reachable_graph(g, ['AB','BC','CB','BA'])
new_g.edges(order='eid')
print(new_g)
>>>Graph(num_nodes=2, num_edges=1,
      ndata_schemes={}
      edata_schemes={})
g = dgl.heterograph({
    ('A', 'AB', 'B'): ([0, 1, 2], [1, 2, 3]),
    ('B', 'BA', 'A'): ([1, 2, 3], [0, 1, 2])})
new_g = dgl.metapath_reachable_graph(g, ['AB', 'BA'])
print(new_g)
Graph(num_nodes=3, num_edges=3,
      ndata_schemes={}
      edata_schemes={})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值