dgl.khop_graph
- dgl.khop_graph(g, k, copy_ndata=True)[source]
返回其边连接原始图的
k
-跳邻居的图。更具体地说,如果在原始图中存在从节点
u
到节点v
的长度为k
的路径,则在新图中存在从节点u
到节点v
的边。返回图的邻接矩阵是 \(A^k\) (其中 \(A\) 是 \(g\) 的邻接矩阵)。
- Parameters:
- Returns:
返回的图表。
- Return type:
注释
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()
anddgl.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={})