From 1cace9f2ccaaf3d253807cecc14857cb5e8977b3 Mon Sep 17 00:00:00 2001 From: Alfredo Braunstein Date: Sat, 7 Jan 2023 15:36:54 +0100 Subject: [PATCH] faster erdos_renyi --- src/SimpleGraphs/generators/randgraphs.jl | 34 ++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 8fc22b768..1ea31f1b4 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1,4 +1,4 @@ -using Random: randperm, shuffle! +using Random: randperm, shuffle!, randsubseq using Statistics: mean using Graphs: sample! @@ -122,6 +122,23 @@ function randbn( return x end +"maps 1:binomial(n,2) into an upper triangle of [1,n]×[1,n]" +function trianglemap(r) + j = floor(Int, (1+sqrt(8r-7))/2) + 1 + i = r - binomial(j-1,2) + Edge(i, j) +end + +"maps 1:n*(n-1) into non-diagonal elements of [1,n]×[1,n]" +function nondiagmap(r,n) + j = div(r-1, n-1) + i = r - j*(n-1) + j += 1 + i += (i >= j) + Edge(i, j) +end + + """ erdos_renyi(n, p) @@ -140,7 +157,7 @@ julia> erdos_renyi(10, 0.5) {10, 20} undirected simple Int64 graph julia> erdos_renyi(10, 0.5, is_directed=true, seed=123) -{10, 49} directed simple Int64 graph +{10, 41} directed simple Int64 graph ``` """ function erdos_renyi( @@ -151,13 +168,16 @@ function erdos_renyi( seed::Union{Nothing,Integer}=nothing, ) p >= 1 && return is_directed ? complete_digraph(n) : complete_graph(n) - m = is_directed ? n * (n - 1) : div(n * (n - 1), 2) - ne = randbn(m, p; rng=rng, seed=seed) - return if is_directed - SimpleDiGraph(n, ne; rng=rng, seed=seed) + m = is_directed ? n * (n - 1) : binomial(n, 2) + seq = randsubseq(rng_from_rng_or_seed(rng, seed), 1:m, p) + g = if is_directed + SimpleDiGraphFromIterator(nondiagmap(r,n) for r in seq) else - SimpleGraph(n, ne; rng=rng, seed=seed) + SimpleGraphFromIterator(trianglemap(r) for r in seq) end + # complete to exactly n vertices + add_vertices!(g, n - nv(g)) + return g end """