MCPcopy
hub / github.com/dmlc/dgl / rand_graph

Function rand_graph

python/dgl/generators.py:8–57  ·  view source on GitHub ↗

Generate a random graph of the given number of nodes/edges and return. It uniformly chooses ``num_edges`` from all possible node pairs and form a graph. The random choice is without replacement, which means there will be no multi-edge in the resulting graph. To control the randomne

(num_nodes, num_edges, idtype=F.int64, device=F.cpu())

Source from the content-addressed store, hash-verified

6
7
8def rand_graph(num_nodes, num_edges, idtype=F.int64, device=F.cpu()):
9 """Generate a random graph of the given number of nodes/edges and return.
10
11 It uniformly chooses ``num_edges`` from all possible node pairs and form a graph.
12 The random choice is without replacement, which means there will be no multi-edge
13 in the resulting graph.
14
15 To control the randomness, set the random seed via :func:`dgl.seed`.
16
17 Parameters
18 ----------
19 num_nodes : int
20 The number of nodes
21 num_edges : int
22 The number of edges
23 idtype : int32, int64, optional
24 The data type for storing the structure-related graph information
25 such as node and edge IDs. It should be a framework-specific data type object
26 (e.g., torch.int32). By default, DGL uses int64.
27 device : Device context, optional
28 The device of the resulting graph. It should be a framework-specific device
29 object (e.g., torch.device). By default, DGL stores the graph on CPU.
30
31 Returns
32 -------
33 DGLGraph
34 The generated random graph.
35
36 See Also
37 --------
38 rand_bipartite
39
40 Examples
41 --------
42 >>> import dgl
43 >>> dgl.rand_graph(100, 10)
44 Graph(num_nodes=100, num_edges=10,
45 ndata_schemes={}
46 edata_schemes={})
47 """
48 # TODO(minjie): support RNG as one of the arguments.
49 eids = random.choice(num_nodes * num_nodes, num_edges, replace=False)
50 eids = F.zerocopy_to_numpy(eids)
51 rows = F.zerocopy_from_numpy(eids // num_nodes)
52 cols = F.zerocopy_from_numpy(eids % num_nodes)
53 rows = F.copy_to(F.astype(rows, idtype), device)
54 cols = F.copy_to(F.astype(cols, idtype), device)
55 return convert.graph(
56 (rows, cols), num_nodes=num_nodes, idtype=idtype, device=device
57 )
58
59
60def rand_bipartite(

Callers 3

test_basicsFunction · 0.90

Calls 4

cpuMethod · 0.45
copy_toMethod · 0.45
astypeMethod · 0.45
graphMethod · 0.45

Tested by 3

test_basicsFunction · 0.72