dgl.line_graph

dgl.line_graph(g, backtracking=True, shared=False)[source]

返回此图的线图。

给定图G的线图L(G)被定义为另一个图,其中L(G)中的节点对应于G中的边。对于G中的任何一对边(u, v)(v, w)L(G)中边(u, v)对应的节点将有一条边连接到边(v, w)对应的节点。

Parameters:
  • g (DGLGraph) – 输入图。必须是同质的。

  • 回溯 (bool, 可选) –

    如果为False,对应于边 (u, v) 的线图节点将不会有一条边连接到对应于边 (v, u) 的线图节点。

    默认值:True。

  • shared (bool, optional) – 是否将原始图的边特征复制为结果线图的节点特征。

Returns:

G – 该图的线图。

Return type:

DGLGraph

注释

  • 如果 shared 为 True,则结果图的节点特征与输入图的边特征共享相同的存储。因此,用户应尽量避免对两个图都可见的原地操作。

  • 该函数支持在GPU上输入图形,但在计算期间会将其复制到CPU。

  • 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.

示例

假设图具有以下邻接矩阵:

A = [[0, 0, 1],
     [1, 0, 1],
     [1, 1, 0]]
>>> g = dgl.graph(([0, 1, 1, 2, 2],[2, 0, 2, 0, 1]), 'user', 'follows')
>>> lg = g.line_graph()
>>> lg
Graph(num_nodes=5, num_edges=8,
ndata_schemes={}
edata_schemes={})
>>> lg.edges()
(tensor([0, 0, 1, 2, 2, 3, 4, 4]), tensor([3, 4, 0, 3, 4, 0, 1, 2]))
>>> lg = g.line_graph(backtracking=False)
>>> lg
Graph(num_nodes=5, num_edges=4,
ndata_schemes={}
edata_schemes={})
>>> lg.edges()
(tensor([0, 1, 2, 4]), tensor([4, 0, 3, 1]))