dgl

The dgl package contains data structure for storing structural and feature data (i.e., the DGLGraph class) and also utilities for generating, manipulating and transforming graphs.

Graph Create Ops

Operators for constructing DGLGraph from raw data formats.

graph(data[, ntype, etype, num_nodes, …])

Create a graph and return.

heterograph(data_dict[, num_nodes_dict, …])

Create a heterogeneous graph and return.

from_scipy(sp_mat[, eweight_name, idtype, …])

Create a graph from a SciPy sparse matrix and return.

from_networkx(nx_graph[, node_attrs, …])

Create a graph from a NetworkX graph and return.

bipartite_from_scipy(sp_mat, utype, etype, vtype)

Create a uni-directional bipartite graph from a SciPy sparse matrix and return.

bipartite_from_networkx(nx_graph, utype, …)

Create a unidirectional bipartite graph from a NetworkX graph and return.

rand_graph(num_nodes, num_edges[, idtype, …])

Generate a random graph of the given number of nodes/edges and return.

rand_bipartite(utype, etype, vtype, …[, …])

Generate a random uni-directional bipartite graph and return.

knn_graph(x, k[, algorithm, dist])

Construct a graph from a set of points according to k-nearest-neighbor (KNN) and return.

segmented_knn_graph(x, k, segs[, algorithm, …])

Construct multiple graphs from multiple sets of points according to k-nearest-neighbor (KNN) and return.

create_block(data_dict[, num_src_nodes, …])

Create a message flow graph (MFG) as a DGLBlock object.

block_to_graph(block)

Convert a message flow graph (MFG) as a DGLBlock object to a DGLGraph.

Subgraph Extraction Ops

Operators for extracting and returning subgraphs.

node_subgraph(graph, nodes, *[, …])

Return a subgraph induced on the given nodes.

edge_subgraph(graph, edges, *[, …])

Return a subgraph induced on the given edges.

node_type_subgraph(graph, ntypes)

Return the subgraph induced on given node types.

edge_type_subgraph(graph, etypes)

Return the subgraph induced on given edge types.

in_subgraph(graph, nodes, *[, …])

Return the subgraph induced on the inbound edges of all the edge types of the given nodes.

out_subgraph(graph, nodes, *[, …])

Return the subgraph induced on the outbound edges of all the edge types of the given nodes.

Graph Transform Ops

Operators for generating new graphs by manipulating the structure of the existing ones.

add_nodes(g, num[, data, ntype])

Add the given number of nodes to the graph and return a new graph.

add_edges(g, u, v[, data, etype])

Add the edges to the graph and return a new graph.

remove_nodes(g, nids[, ntype, store_ids])

Remove the specified nodes and return a new graph.

remove_edges(g, eids[, etype, store_ids])

Remove the specified edges and return a new graph.

add_self_loop(g[, etype])

Add self-loops for each node in the graph and return a new graph.

remove_self_loop(g[, etype])

Remove self-loops for each node in the graph and return a new graph.

add_reverse_edges(g[, readonly, copy_ndata, …])

Add an reversed edge for each edge in the input graph and return a new graph.

reverse(g[, copy_ndata, copy_edata, …])

Return a new graph with every edges being the reverse ones in the input graph.

to_bidirected(g[, copy_ndata, readonly])

Convert the graph to a bi-directional simple graph and return.

to_simple(g[, return_counts, …])

Convert a graph to a simple graph without parallel edges and return.

to_block(g[, dst_nodes, include_dst_in_src])

Convert a graph into a bipartite-structured block for message passing.

compact_graphs(graphs[, always_preserve, …])

Given a list of graphs with the same set of nodes, find and eliminate the common isolated nodes across all graphs.

to_heterogeneous(G, ntypes, etypes[, …])

Convert a homogeneous graph to a heterogeneous graph and return.

to_homogeneous(G[, ndata, edata, …])

Convert a heterogeneous graph to a homogeneous graph and return.

to_networkx(g[, node_attrs, edge_attrs])

Convert a homogeneous graph to a NetworkX graph and return.

line_graph(g[, backtracking, shared])

Return the line graph of this graph.

khop_graph(g, k[, copy_ndata])

Return the graph whose edges connect the k-hop neighbors of the original graph.

metapath_reachable_graph(g, metapath)

Return a graph where the successors of any node u are nodes reachable from u by the given metapath.

adj_product_graph(A, B, weight_name[, etype])

Create a weighted graph whose adjacency matrix is the product of the adjacency matrices of the given two graphs.

adj_sum_graph(graphs, weight_name)

Create a weighted graph whose adjacency matrix is the sum of the adjacency matrices of the given graphs, whose rows represent source nodes and columns represent destination nodes.

reorder_graph(g[, node_permute_algo, …])

Return a new graph with nodes and edges re-ordered/re-labeled according to the specified permute algorithm.

sort_csr_by_tag(g, tag[, tag_offset_name])

Return a new graph whose CSR matrix is sorted by the given tag.

sort_csc_by_tag(g, tag[, tag_offset_name])

Return a new graph whose CSC matrix is sorted by the given tag.

Graph Partition Utilities

metis_partition(g, k[, extra_cached_hops, …])

This is to partition a graph with Metis partitioning.

metis_partition_assignment(g, k[, …])

This assigns nodes to different partitions with Metis partitioning algorithm.

partition_graph_with_halo(g, node_part, …)

Partition a graph.

Batching and Reading Out Ops

Operators for batching multiple graphs into one for batch processing and operators for computing graph-level representation for both single and batched graphs.

batch(graphs[, ndata, edata, node_attrs, …])

Batch a collection of DGLGraph s into one graph for more efficient graph computation.

unbatch(g[, node_split, edge_split])

Revert the batch operation by split the given graph into a list of small ones.

slice_batch(g, gid[, store_ids])

Get a particular graph from a batch of graphs.

readout_nodes(graph, feat[, weight, op, ntype])

Generate a graph-level representation by aggregating node features feat.

readout_edges(graph, feat[, weight, op, etype])

Sum the edge feature feat in graph, optionally multiplies it by a edge weight.

sum_nodes(graph, feat[, weight, ntype])

Syntax sugar for dgl.readout_nodes(graph, feat, weight, ntype=ntype, op='sum').

sum_edges(graph, feat[, weight, etype])

Syntax sugar for dgl.readout_edges(graph, feat, weight, etype=etype, op='sum').

mean_nodes(graph, feat[, weight, ntype])

Syntax sugar for dgl.readout_nodes(graph, feat, weight, ntype=ntype, op='mean').

mean_edges(graph, feat[, weight, etype])

Syntax sugar for dgl.readout_edges(graph, feat, weight, etype=etype, op='mean').

max_nodes(graph, feat[, weight, ntype])

Syntax sugar for dgl.readout_nodes(graph, feat, weight, ntype=ntype, op='max').

max_edges(graph, feat[, weight, etype])

Syntax sugar for dgl.readout_edges(graph, feat, weight, etype=etype, op='max').

softmax_nodes(graph, feat, *[, ntype])

Perform graph-wise softmax on the node features.

softmax_edges(graph, feat, *[, etype])

Perform graph-wise softmax on the edge features.

broadcast_nodes(graph, graph_feat, *[, ntype])

Generate a node feature equal to the graph-level feature graph_feat.

broadcast_edges(graph, graph_feat, *[, etype])

Generate an edge feature equal to the graph-level feature graph_feat.

topk_nodes(graph, feat, k, *[, descending, …])

Return a graph-level representation by a graph-wise top-k on node features feat in graph by feature at index sortby.

topk_edges(graph, feat, k, *[, descending, …])

Return a graph-level representation by a graph-wise top-k on edge features feat in graph by feature at index sortby.

Graph Traversal & Message Propagation

DGL implements graph traversal algorithms implemented as python generators, which returns the visited set of nodes or edges (in ID tensor) at each iteration. The naming convention is <algorithm>_[nodes|edges]_generator. An example usage is as follows.

g = ...  # some DGLGraph
for nodes in dgl.bfs_nodes_generator(g, 0):
    do_something(nodes)

bfs_nodes_generator(graph, source[, reverse])

Node frontiers generator using breadth-first search.

bfs_edges_generator(graph, source[, reverse])

Edges frontiers generator using breadth-first search.

topological_nodes_generator(graph[, reverse])

Node frontiers generator using topological traversal.

dfs_edges_generator(graph, source[, reverse])

Edge frontiers generator using depth-first-search (DFS).

dfs_labeled_edges_generator(graph, source[, …])

Produce edges in a depth-first-search (DFS) labeled by type.

DGL provides APIs to perform message passing following graph traversal order. prop_nodes_XXX calls traversal algorithm XXX and triggers pull() on the visited node set at each iteration. prop_edges_YYY applies traversal algorithm YYY and triggers send_and_recv() on the visited edge set at each iteration.

prop_nodes(graph, nodes_generator[, …])

Functional method for dgl.DGLGraph.prop_nodes().

prop_nodes_bfs(graph, source, message_func, …)

Message propagation using node frontiers generated by BFS.

prop_nodes_topo(graph, message_func, reduce_func)

Message propagation using node frontiers generated by topological order.

prop_edges(graph, edges_generator[, …])

Functional method for dgl.DGLGraph.prop_edges().

prop_edges_dfs(graph, source, message_func, …)

Message propagation using edge frontiers generated by labeled DFS.

Utilities

Other utilities for controlling randomness, saving and loading graphs, etc.

seed(val)

Set the random seed of DGL.

save_graphs(filename, g_list[, labels])

Save graphs and optionally their labels to file.

load_graphs(filename[, idx_list])

Load graphs and optionally their labels from file saved by save_graphs().