Skip to content

Commit 64e1aba

Browse files
committed
faster implementation of erdos_renyi
1 parent 42ba34e commit 64e1aba

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/SimpleGraphs/generators/randgraphs.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,28 @@ function erdos_renyi(
160160
seed::Union{Nothing,Integer}=nothing,
161161
)
162162
p >= 1 && return is_directed ? complete_digraph(n) : complete_graph(n)
163-
m = is_directed ? n * (n - 1) : div(n * (n - 1), 2)
164-
ne = randbn(m, p; rng=rng, seed=seed)
165-
return if is_directed
166-
SimpleDiGraph(n, ne; rng=rng, seed=seed)
167-
else
168-
SimpleGraph(n, ne; rng=rng, seed=seed)
163+
rng = getRNG(seed)
164+
m = 0
165+
fadj = [Int[] for u in 1:n]
166+
if is_directed
167+
badj = [Int[] for u in 1:n]
168+
end
169+
for u in 1:n
170+
start = is_directed ? 1 : u+1
171+
for v in start:n
172+
v == u && continue
173+
if rand(rng) < p
174+
m += 1
175+
push!(fadj[u], v)
176+
if is_directed
177+
push!(badj[v], u)
178+
else
179+
push!(fadj[v], u)
180+
end
181+
end
182+
end
169183
end
184+
return is_directed ? SimpleDiGraph(m, fadj, badj) : SimpleGraph(m, fadj)
170185
end
171186

172187
"""
@@ -289,7 +304,7 @@ end
289304
watts_strogatz(n, k, β)
290305
291306
Return a [Watts-Strogatz](https://en.wikipedia.org/wiki/Watts_and_Strogatz_model)
292-
small world random graph with `n` vertices, each with expected degree `k`
307+
small world random graph with `n` vertices, each with expected degree `k`
293308
(or `k - 1` if `k` is odd). Edges are randomized per the model based on probability `β`.
294309
295310
The algorithm proceeds as follows. First, a perfect 1-lattice is constructed,

0 commit comments

Comments
 (0)