dgl.DGLGraph.subgraph

DGLGraph.subgraph(nodes)[source]

Return the subgraph induced on given nodes.

Parameters:nodes (list, or iterable) – A node ID array to construct subgraph. All nodes must exist in the graph.
Returns:G – The subgraph. The nodes are relabeled so that node i in the subgraph is mapped to node nodes[i] in the original graph. The edges are also relabeled. One can retrieve the mapping from subgraph node/edge ID to parent node/edge ID via parent_nid and parent_eid properties of the subgraph.
Return type:DGLSubGraph

Examples

The following example uses PyTorch backend.

>>> G = dgl.DGLGraph()
>>> G.add_nodes(5)
>>> G.add_edges([0, 1, 2, 3, 4], [1, 2, 3, 4, 0])   # 5-node cycle
>>> SG = G.subgraph([0, 1, 4])
>>> SG.nodes()
tensor([0, 1, 2])
>>> SG.edges()
(tensor([0, 2]), tensor([1, 0]))
>>> SG.parent_nid
tensor([0, 1, 4])
>>> SG.parent_eid
tensor([0, 4])

See also

DGLSubGraph(), subgraphs(), edge_subgraph()