dgl.khop_graph

dgl.khop_graph(g, k, copy_ndata=True)[source]

返回其边连接原始图的k-跳邻居的图。

更具体地说,如果在原始图中存在从节点 u 到节点 v 的长度为 k 的路径,则在新图中存在从节点 u 到节点 v 的边。

返回图的邻接矩阵是 \(A^k\) (其中 \(A\)\(g\) 的邻接矩阵)。

Parameters:
  • g (DGLGraph) – The input graph.

  • k (int) – k-hop 图中的 \(k\)

  • copy_ndata (bool, optional) –

    如果为True,新图的节点特征将从原始图中复制。

    如果为False,新图将不会有任何节点特征。

    (默认值:True)

Returns:

返回的图表。

Return type:

DGLGraph

注释

If copy_ndata is True, the resulting graph will share the node feature tensors with the input graph. Hence, users should try to avoid in-place operations which will be visible to both graphs.

This function discards the batch information. Please use dgl.DGLGraph.set_batch_num_nodes() and dgl.DGLGraph.set_batch_num_edges() on the transformed graph to maintain the information.

示例

下面给出一个简单的例子:

>>> import dgl
>>> g = dgl.graph(([0, 1], [1, 2]))
>>> g_2 = dgl.transforms.khop_graph(g, 2)
>>> print(g_2.edges())
(tensor([0]), tensor([2]))

一个更复杂的例子:

>>> import dgl
>>> g = dgl.graph(([0,1,2,3,4,0,1,2,3,4], [0,1,2,3,4,1,2,3,4,0]))
>>> dgl.khop_graph(g, 1)
DGLGraph(num_nodes=5, num_edges=10,
         ndata_schemes={}
         edata_schemes={})
>>> dgl.khop_graph(g, 3)
DGLGraph(num_nodes=5, num_edges=40,
         ndata_schemes={}
         edata_schemes={})