-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathgenerate.jl
More file actions
52 lines (42 loc) · 1.27 KB
/
generate.jl
File metadata and controls
52 lines (42 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
rand_graph(n, m; bidirected=true, seed=-1, kws...)
Generate a random (Erdós-Renyi) `GNNGraph` with `n` nodes
and `m` edges.
If `bidirected=true` the reverse edge of each edge will be present.
If `bidirected=false` instead, `m` unrelated edges are generated.
In any case, the output graph will contain no self-loops or multi-edges.
Additional keyword arguments will be passed to the [`GNNGraph`](@ref) constructor.
# Usage
```juliarepl
julia> g = rand_graph(5, 4, bidirected=false)
GNNGraph:
num_nodes = 5
num_edges = 4
num_graphs = 1
ndata:
edata:
gdata:
julia> edge_index(g)
([1, 3, 3, 4], [5, 4, 5, 2])
# In the bidirected case, edge data will be duplicated on the reverse edges if needed.
julia> g = rand_graph(5, 4, edata=rand(16, 2))
GNNGraph:
num_nodes = 5
num_edges = 4
num_graphs = 1
ndata:
edata:
e => (16, 4)
gdata:
# Each edge has a reverse
julia> edge_index(g)
([1, 3, 3, 4], [3, 4, 1, 3])
```
"""
function rand_graph(n::Integer, m::Integer; bidirected=true, seed=-1, kws...)
if bidirected
@assert iseven(m) "Need even number of edges for bidirected graphs, given m=$m."
end
m2 = bidirected ? m÷2 : m
return GNNGraph(Graphs.erdos_renyi(n, m2; is_directed=!bidirected, seed); kws...)
end