dgl.edge_type_subgraph(graph, etypes)[source]

Return the subgraph induced on given edge types.

An edge-type-induced subgraph contains all the edges of the given subset of the edge types of a graph and the nodes incident by those edges. In addition to extracting the subgraph, DGL also copies the features of the extracted nodes and edges to the resulting graph. The copy is lazy and incurs data movement only when needed.

  • graph (DGLGraph) – The graph to extract subgraphs from.

  • etypes (list[str] or list[(str, str, str)]) –

    The type names of the edges in the subgraph. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str for the edge type name if the name can uniquely identify a triplet format in the graph.


G – The subgraph.

Return type



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.


The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Instantiate a heterograph.

>>> g = dgl.heterograph({
>>>     ('user', 'plays', 'game'): ([0, 1, 1, 2], [0, 0, 2, 1]),
>>>     ('user', 'follows', 'user'): ([0, 1, 1], [1, 2, 2])
>>> })
>>> # Set edge features
>>> g.edges['follows'].data['h'] = torch.tensor([[0.], [1.], [2.]])

Get subgraphs.

>>> sub_g = g.edge_type_subgraph(['follows'])
>>> sub_g
Graph(num_nodes=3, num_edges=3,
      edata_schemes={'h': Scheme(shape=(1,), dtype=torch.float32)})

Get the shared edge features.

>>> sub_g.edges['follows'].data['h']