dgl.from_networkx

dgl.from_networkx(nx_graph, node_attrs=None, edge_attrs=None, edge_id_attr_name=None, idtype=None, device=None)[source]

Create a graph from a NetworkX graph and return.

Note

Creating a DGLGraph from a NetworkX graph is not fast especially for large scales. It is recommended to first convert a NetworkX graph into a tuple of node-tensors and then construct a DGLGraph with dgl.graph().

Parameters
  • nx_graph (networkx.Graph) – The NetworkX graph holding the graph structure and the node/edge attributes. DGL will relabel the nodes using consecutive integers starting from zero if it is not the case. If the input graph is undirected, DGL converts it to a directed graph by networkx.Graph.to_directed().

  • node_attrs (list[str], optional) – The names of the node attributes to retrieve from the NetworkX graph. If given, DGL stores the retrieved node attributes in ndata of the returned graph using their original names. The attribute data must be convertible to Tensor type (e.g., scalar, numpy.ndarray, list, etc.).

  • edge_attrs (list[str], optional) – The names of the edge attributes to retrieve from the NetworkX graph. If given, DGL stores the retrieved edge attributes in edata of the returned graph using their original names. The attribute data must be convertible to Tensor type (e.g., scalar, numpy.ndarray, list, etc.). It must be None if nx_graph is undirected.

  • edge_id_attr_name (str, optional) – The name of the edge attribute that stores the edge IDs. If given, DGL will assign edge IDs accordingly when creating the graph, so the attribute must be valid IDs, i.e. consecutive integers starting from zero. By default, the edge IDs of the returned graph can be arbitrary. It must be None if nx_graph is undirected.

  • idtype (int32 or int64, optional) – The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g., torch.int32). By default, DGL uses int64.

  • device (device context, optional) – The device of the resulting graph. It should be a framework-specific device object (e.g., torch.device). By default, DGL stores the graph on CPU.

Returns

The created graph.

Return type

DGLGraph

Notes

DGL internally maintains multiple copies of the graph structure in different sparse formats and chooses the most efficient one depending on the computation invoked. If memory usage becomes an issue in the case of large graphs, use dgl.DGLGraph.formats() to restrict the allowed formats.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import networkx as nx
>>> import numpy as np
>>> import torch

Create a 2-edge NetworkX graph.

>>> nx_g = nx.DiGraph()
>>> # Add 3 nodes and two features for them
>>> nx_g.add_nodes_from([0, 1, 2], feat1=np.zeros((3, 1)), feat2=np.ones((3, 1)))
>>> # Add 2 edges (1, 2) and (2, 1) with two features, one being edge IDs
>>> nx_g.add_edge(1, 2, weight=np.ones((1, 1)), eid=np.array([1]))
>>> nx_g.add_edge(2, 1, weight=np.ones((1, 1)), eid=np.array([0]))

Convert it into a DGLGraph with structure only.

>>> g = dgl.from_networkx(nx_g)

Retrieve the node/edge features of the graph.

>>> g = dgl.from_networkx(nx_g, node_attrs=['feat1', 'feat2'], edge_attrs=['weight'])

Use a pre-specified ordering of the edges.

>>> g.edges()
(tensor([1, 2]), tensor([2, 1]))
>>> g = dgl.from_networkx(nx_g, edge_id_attr_name='eid')
(tensor([2, 1]), tensor([1, 2]))

Create a graph on the first GPU with data type int32.

>>> g = dgl.from_networkx(nx_g, idtype=torch.int32, device='cuda:0')

See also

graph, from_scipy