Skip to content

Commit fd6960d

Browse files
committed
faster implementation of erdos_renyi
1 parent 48cb6ec commit fd6960d

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Diff for: src/SimpleGraphs/generators/randgraphs.jl

+23-4
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,28 @@ julia> erdos_renyi(10, 0.5, is_directed=true, seed=123)
122122
"""
123123
function erdos_renyi(n::Integer, p::Real; is_directed=false, seed::Integer=-1)
124124
p >= 1 && return is_directed ? complete_digraph(n) : complete_graph(n)
125-
m = is_directed ? n * (n - 1) : div(n * (n - 1), 2)
126-
ne = randbn(m, p, seed)
127-
return is_directed ? SimpleDiGraph(n, ne, seed=seed) : SimpleGraph(n, ne, seed=seed)
125+
rng = getRNG(seed)
126+
m = 0
127+
fadj = [Int[] for u in 1:n]
128+
if is_directed
129+
badj = [Int[] for u in 1:n]
130+
end
131+
for u in 1:n
132+
start = is_directed ? 1 : u+1
133+
for v in start:n
134+
v == u && continue
135+
if rand(rng) < p
136+
m += 1
137+
push!(fadj[u], v)
138+
if is_directed
139+
push!(badj[v], u)
140+
else
141+
push!(fadj[v], u)
142+
end
143+
end
144+
end
145+
end
146+
return is_directed ? SimpleDiGraph(m, fadj, badj) : SimpleGraph(m, fadj)
128147
end
129148

130149
"""
@@ -223,7 +242,7 @@ end
223242
watts_strogatz(n, k, β)
224243
225244
Return a [Watts-Strogatz](https://en.wikipedia.org/wiki/Watts_and_Strogatz_model)
226-
small world random graph with `n` vertices, each with expected degree `k`
245+
small world random graph with `n` vertices, each with expected degree `k`
227246
(or `k - 1` if `k` is odd). Edges are randomized per the model based on probability `β`.
228247
229248
The algorithm proceeds as follows. First, a perfect 1-lattice is constructed,

0 commit comments

Comments
 (0)