# dgl.transform.khop_graph¶

dgl.transform.khop_graph(g, k)[source]

Return the graph that includes all $$k$$-hop neighbors of the given graph as edges. The adjacency matrix of the returned graph is $$A^k$$ (where $$A$$ is the adjacency matrix of $$g$$).

Parameters: g (dgl.DGLGraph) – The input graph. k (int) – The $$k$$ in k-hop graph. The returned DGLGraph. dgl.DGLGraph

Examples

Below gives an easy example:

>>> import dgl
>>> g = dgl.DGLGraph()
>>> g_2 = dgl.transform.khop_graph(g, 2)
>>> print(g_2.edges())
(tensor([0]), tensor([2]))


A more complicated example:

>>> import dgl
>>> g = dgl.DGLGraph()