dgl.graph¶

dgl.
graph
(data, ntype='_N', etype='_E', card=None, **kwargs)[source]¶ Create a graph with one type of nodes and edges.
In the sparse matrix perspective,
dgl.graph()
creates a graph whose adjacency matrix must be square whiledgl.bipartite()
creates a graph that does not necessarily have square adjacency matrix.Parameters:  data (graph data) –
Data to initialize graph structure. Supported data formats are
 list of edge pairs (e.g. [(0, 2), (3, 1), …])
 pair of vertex IDs representing end nodes (e.g. ([0, 3, …], [2, 1, …]))
 scipy sparse matrix
 networkx graph
 ntype (str, optional) – Node type name. (Default: _N)
 etype (str, optional) – Edge type name. (Default: _E)
 card (int, optional) – Cardinality (number of nodes in the graph). If None, infer from input data, i.e. the largest node ID plus 1. (Default: None)
 kwargs (keyword arguments, optional) –
Other key word arguments. Only comes into effect when we are using a NetworkX graph. It can consist of:
 edge_id_attr_name
Str
, key name for edge ids in the NetworkX graph. If not found, we will consider the graph not to have prespecified edge ids.
 node_attrs
List of str
, names for node features to retrieve from the NetworkX graph
 edge_attrs
List of str
, names for edge features to retrieve from the NetworkX graph
Returns: Return type: Examples
Create from pairs of edges with form (src, dst)
>>> g = dgl.graph([(0, 2), (0, 3), (1, 2)])
Create from source and destination vertex ID lists
>>> u = [0, 0, 1] >>> v = [2, 3, 2] >>> g = dgl.graph((u, v))
The IDs can also be stored in frameworkspecific tensors
>>> import torch >>> u = torch.tensor([0, 0, 1]) >>> v = torch.tensor([2, 3, 2]) >>> g = dgl.graph((u, v))
Create from scipy sparse matrix
>>> from scipy.sparse import coo_matrix >>> spmat = coo_matrix(([1,1,1], ([0, 0, 1], [2, 3, 2])), shape=(4, 4)) >>> g = dgl.graph(spmat)
Create from networkx graph
>>> import networkx as nx >>> nxg = nx.path_graph(3) >>> g = dgl.graph(nxg)
Specify node and edge type names
>>> g = dgl.graph(..., 'user', 'follows') >>> g.ntypes ['user'] >>> g.etypes ['follows'] >>> g.canonical_etypes [('user', 'follows', 'user')]
 data (graph data) –