dgl.DGLGraph.edge_subgraph

DGLGraph.edge_subgraph(edges, preserve_nodes=False)[source]

Return the subgraph induced on given edges.

Parameters:
  • edges (list, or iterable) – An edge ID array to construct subgraph. All edges must exist in the subgraph.
  • preserve_nodes (bool) – Indicates whether to preserve all nodes or not. If true, keep the nodes which have no edge connected in the subgraph; If false, all nodes without edge connected to it would be removed.
Returns:

G – The subgraph. The edges are relabeled so that edge i in the subgraph is mapped to edge edges[i] in the original graph. The nodes 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.edge_subgraph([0, 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])
>>> SG = G.edge_subgraph([0, 4], preserve_nodes=True)
>>> SG.nodes()
tensor([0, 1, 2, 3, 4])
>>> SG.edges()
(tensor([0, 4]), tensor([1, 0]))
>>> SG.parent_nid
tensor([0, 1, 2, 3, 4])
>>> SG.parent_eid
tensor([0, 4])

See also

DGLSubGraph(), subgraph()