From 061f7a7b618f923a1447e3dffab128c7c748c392 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 11:39:17 +0100 Subject: [PATCH 01/46] Fix randbn --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 52b749784..349ef0fd4 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -114,7 +114,7 @@ function randbn( log_q = log(1.0 - p) x = 0 sum = 0.0 - while true + for _ in 1:n sum += log(rand(rng)) / (n - x) sum < log_q && break x += 1 From cad14af44fb570b90666d6ea39f7f0cad4fee715 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 12:57:18 +0100 Subject: [PATCH 02/46] Add tests randbn --- test/simplegraphs/generators/randgraphs.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 9853aadb6..49e2ef6eb 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -364,4 +364,24 @@ @test isvalid_simplegraph(rog3) @test !is_cyclic(rog3) end + + @testset "randbn" begin + for i in 1:10 + @test Graphs.SimpleGraphs.randbn(i, 0.0; rng=rng) == 0 + @test Graphs.SimpleGraphs.randbn(i, 1.0; rng=rng) == i + end + N = 30 + s1 = zeros(N) + s2 = zeros(N) + for i in 1:N + s1[i] = Graphs.SimpleGraphs.randbn(5, 0.3) + s2[i] = Graphs.SimpleGraphs.randbn(3, 0.7) + end + μ1 = mean(s1) + μ2 = mean(s2) + sv1 = sqrt((1 / N) * sum((s1 .- μ1) .^ 2)) + sv2 = sqrt((1 / N) * sum((s2 .- μ2) .^ 2)) + @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 + @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 + end end From c9fe4341a72fc78d6edd22cbd3f9ed91cd8bf0d8 Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:30:07 +0100 Subject: [PATCH 03/46] Add comment Co-authored-by: Emanuele Natale --- test/simplegraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 49e2ef6eb..f02246955 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -381,7 +381,7 @@ μ2 = mean(s2) sv1 = sqrt((1 / N) * sum((s1 .- μ1) .^ 2)) sv2 = sqrt((1 / N) * sum((s2 .- μ2) .^ 2)) - @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 + @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 end end From 3876f6e192a052d77edbc351e62550639ad7e4f8 Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:30:41 +0100 Subject: [PATCH 04/46] Use statistics Co-authored-by: Emanuele Natale --- test/simplegraphs/generators/randgraphs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index f02246955..330d027a3 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -379,8 +379,8 @@ end μ1 = mean(s1) μ2 = mean(s2) - sv1 = sqrt((1 / N) * sum((s1 .- μ1) .^ 2)) - sv2 = sqrt((1 / N) * sum((s2 .- μ2) .^ 2)) + sv1 = std(s1) + sv2 = std(s2) @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 end From a023ad9abd9b4b3f0eb00dcb044bc20fd1f4cf9a Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 14:35:16 +0100 Subject: [PATCH 05/46] Add std from Statistics for test --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 5e24af649..786bdb949 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,7 +11,7 @@ using Compat using DelimitedFiles using Base64 using Random -using Statistics: mean +using Statistics: mean, std using StableRNGs const testdir = dirname(@__FILE__) From 48f12c493263a6441c9bc0d29f8668f8e0357d18 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 14:55:30 +0100 Subject: [PATCH 06/46] Add bernoulli graph --- src/SimpleGraphs/generators/randgraphs.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 349ef0fd4..46f1bd3c9 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1422,3 +1422,25 @@ function random_orientation_dag( end return g2 end + +""" + bernoulli_graph(Λ) + +Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exits with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. +""" +function bernoulli_graph( + Λ::Matrix{Float64}; + rng::Union{Nothing,AbstractRNG}=nothing, + seed::Union{Nothing,Integer}=nothing, +) + n = size(Λ)[1] + A = SimpleGraph(n) + for j in 1:n + for i in (j + 1):n + if Bool(randbn(1, Λ[i, j]; rng=rng, seed=seed)) + add_edge!(A, i, j) + end + end + end + return A +end From 1c7bd096c78827e6ceb046317c4dbb72c96ccf08 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 14:56:58 +0100 Subject: [PATCH 07/46] Add correlated bernoulli graphs --- src/SimpleGraphs/generators/randgraphs.jl | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 46f1bd3c9..0b9fb17b5 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1444,3 +1444,28 @@ function bernoulli_graph( end return A end + +""" +rho_correlated_bernoulli_graphs(Λ,ρ) + +Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. +""" +function rho_correlated_bernoulli_graphs( +Λ::Matrix{Float64}, +ρ::Float64; +rng::Union{Nothing,AbstractRNG}=nothing, +seed::Union{Nothing,Integer}=nothing, +) +n = size(Λ)[1] +B = SimpleGraph(n) +A = bernoulli_graph(Λ; rng=rng, seed=seed) +A_adj = Int.(adjacency_matrix(A)) +for j in 1:n + for i in (j + 1):n + if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * A_adj[i, j]; rng=rng, seed=seed)) + add_edge!(B, i, j) + end + end +end +return A, B +end \ No newline at end of file From b8eb7f6c0ffeeb41eed8ae2b347d66bdc3160b65 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 14:57:52 +0100 Subject: [PATCH 08/46] Add tests --- test/simplegraphs/generators/randgraphs.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 330d027a3..125be392a 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -384,4 +384,15 @@ @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 end + + @testset "bernoulli graphs" begin + n = 50 + Λ = rand(n, n) + ρ = 1.0 + g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) + g1_adj = Int.(adjacency_matrix(g1)) + g2_adj = Int.(adjacency_matrix(g2)) + @test g1_adj == g2_adj + @test diag(g1_adj) == diag(g2_adj) == zeros(n) + end end From ec62227f836c24b1b39af7bf7e6c0d20791c2c73 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 14:59:33 +0100 Subject: [PATCH 09/46] Export new functions --- src/Graphs.jl | 2 ++ src/SimpleGraphs/SimpleGraphs.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Graphs.jl b/src/Graphs.jl index b754ccb6e..7998cee61 100644 --- a/src/Graphs.jl +++ b/src/Graphs.jl @@ -295,6 +295,8 @@ export kronecker, dorogovtsev_mendes, random_orientation_dag, + bernoulli_graph, + rho_correlated_bernoulli_graphs, # community modularity, diff --git a/src/SimpleGraphs/SimpleGraphs.jl b/src/SimpleGraphs/SimpleGraphs.jl index 1a75a24d9..a56bac0a7 100644 --- a/src/SimpleGraphs/SimpleGraphs.jl +++ b/src/SimpleGraphs/SimpleGraphs.jl @@ -74,6 +74,8 @@ export AbstractSimpleGraph, static_scale_free, kronecker, random_orientation_dag, + bernoulli_graph, + rho_correlated_bernoulli_graphs, # generators complete_graph, star_graph, From 923a665c18e5aef6034f61dde6378d9611a4ea1b Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 22 Nov 2022 15:05:40 +0100 Subject: [PATCH 10/46] Remove bracket --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 786bdb949..4cc782cb3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,7 +21,7 @@ const testdir = dirname(@__FILE__) end @testset verbose = true "Code formatting (JuliaFormatter.jl)" begin - @test format(Graphs; verbose=false, overwrite=false, ignore=["vf2.jl"]) # TODO: remove ignore kwarg once the file is formatted correctly + @test format(Graphs; verbose=false, overwrite=false, ignore="vf2.jl") # TODO: remove ignore kwarg once the file is formatted correctly end @testset verbose = true "Doctests (Documenter.jl)" begin From e57b04a3391d4a1835f4c4ab7de9a60c9c219b81 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Wed, 30 Nov 2022 11:32:23 +0100 Subject: [PATCH 11/46] Remove merged double test --- test/simplegraphs/generators/randgraphs.jl | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index d3a48b899..845f8fc10 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -385,26 +385,6 @@ @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 end - @testset "randbn" begin - for i in 1:10 - @test Graphs.SimpleGraphs.randbn(i, 0.0; rng=rng) == 0 - @test Graphs.SimpleGraphs.randbn(i, 1.0; rng=rng) == i - end - N = 30 - s1 = zeros(N) - s2 = zeros(N) - for i in 1:N - s1[i] = Graphs.SimpleGraphs.randbn(5, 0.3) - s2[i] = Graphs.SimpleGraphs.randbn(3, 0.7) - end - μ1 = mean(s1) - μ2 = mean(s2) - sv1 = std(s1) - sv2 = std(s2) - @test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail - @test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2 - end - @testset "bernoulli graphs" begin n = 50 Λ = rand(n, n) From fd249a981f689f5fdb468febd754f894e657a748 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Wed, 30 Nov 2022 11:45:48 +0100 Subject: [PATCH 12/46] Add test for non-isomorphism case --- test/runtests.jl | 2 +- test/simplegraphs/generators/randgraphs.jl | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4cc782cb3..414b3e9b2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,7 +11,7 @@ using Compat using DelimitedFiles using Base64 using Random -using Statistics: mean, std +using Statistics: mean, std, cor using StableRNGs const testdir = dirname(@__FILE__) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 845f8fc10..ff517b7b7 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -388,11 +388,17 @@ @testset "bernoulli graphs" begin n = 50 Λ = rand(n, n) - ρ = 1.0 + ρ = 1.0 # isomorphism case g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) g1_adj = Int.(adjacency_matrix(g1)) g2_adj = Int.(adjacency_matrix(g2)) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) + ρ2 = 0.4 + g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) + mean(cor( + reshape(Int.(adjacency_matrix(g3)), n * n, 1), + reshape(Int.(adjacency_matrix(g4)), n * n, 1), + )) ≈ ρ end end From 40c3dfa0bf62ae0d96626b34782241032a3d3a1d Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Wed, 30 Nov 2022 12:31:52 +0100 Subject: [PATCH 13/46] Remove test --- test/simplegraphs/generators/randgraphs.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index ff517b7b7..07ffebdcd 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -394,11 +394,5 @@ g2_adj = Int.(adjacency_matrix(g2)) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) - ρ2 = 0.4 - g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) - mean(cor( - reshape(Int.(adjacency_matrix(g3)), n * n, 1), - reshape(Int.(adjacency_matrix(g4)), n * n, 1), - )) ≈ ρ end end From ed768f7bcf7cf38228d82ff301adefefbeb10dcf Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Wed, 30 Nov 2022 13:06:35 +0100 Subject: [PATCH 14/46] Fix typo --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 0b9fb17b5..c4f81241d 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1426,7 +1426,7 @@ end """ bernoulli_graph(Λ) -Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exits with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. +Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ function bernoulli_graph( Λ::Matrix{Float64}; From 795df03f9150c4202a6fda75ea7c39a1c4386df3 Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:13:16 +0100 Subject: [PATCH 15/46] Add assert size Co-authored-by: Emanuele Natale --- src/SimpleGraphs/generators/randgraphs.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index c4f81241d..409cc79e6 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1433,6 +1433,7 @@ function bernoulli_graph( rng::Union{Nothing,AbstractRNG}=nothing, seed::Union{Nothing,Integer}=nothing, ) + @assert size(Λ)[1] == size(Λ)[2] "The probability matrix must be a square matrix!" n = size(Λ)[1] A = SimpleGraph(n) for j in 1:n From 1ad8c103e6eb76a3d6eb85f6f91e1d94cf7f8d18 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Mon, 5 Dec 2022 18:24:15 +0100 Subject: [PATCH 16/46] Fix docstrings --- src/SimpleGraphs/generators/randgraphs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 409cc79e6..5d0bbd676 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1426,7 +1426,7 @@ end """ bernoulli_graph(Λ) -Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ function bernoulli_graph( Λ::Matrix{Float64}; @@ -1449,7 +1449,7 @@ end """ rho_correlated_bernoulli_graphs(Λ,ρ) -Given the parametric symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ function rho_correlated_bernoulli_graphs( Λ::Matrix{Float64}, From 13ddd4ae474a93bb8f2bb00216dd59b327dd7336 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 6 Dec 2022 11:58:34 +0100 Subject: [PATCH 17/46] Add more tests --- test/simplegraphs/generators/randgraphs.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 07ffebdcd..8c2176193 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -394,5 +394,17 @@ g2_adj = Int.(adjacency_matrix(g2)) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) + ρ = 0.5 # non isomorphism case + g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) + g3_adj = Int.(adjacency_matrix(g3)) + g4_adj = Int.(adjacency_matrix(g4)) + @test g3_adj != g4_adj + @test diag(g3_adj) == diag(g4_adj) == zeros(n) + g5=bernoulli_graph(Λ,seed=1) # check seed + g6=bernoulli_graph(Λ,seed=1) + @test g5==g6 + for g in testgraphs(g5) + @test nv(g)== n + end end end From dab77a6fdff60f9726383ed3b3e2908569c819f9 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 6 Dec 2022 11:58:51 +0100 Subject: [PATCH 18/46] Fix seed and rng bernoulli --- src/SimpleGraphs/generators/randgraphs.jl | 32 ++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 5d0bbd676..75185f03a 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1435,10 +1435,11 @@ function bernoulli_graph( ) @assert size(Λ)[1] == size(Λ)[2] "The probability matrix must be a square matrix!" n = size(Λ)[1] + rng = rng_from_rng_or_seed(rng, seed) A = SimpleGraph(n) for j in 1:n for i in (j + 1):n - if Bool(randbn(1, Λ[i, j]; rng=rng, seed=seed)) + if Bool(randbn(1, Λ[i, j]; rng=rng)) add_edge!(A, i, j) end end @@ -1452,21 +1453,22 @@ rho_correlated_bernoulli_graphs(Λ,ρ) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ function rho_correlated_bernoulli_graphs( -Λ::Matrix{Float64}, -ρ::Float64; -rng::Union{Nothing,AbstractRNG}=nothing, -seed::Union{Nothing,Integer}=nothing, + Λ::Matrix{Float64}, + ρ::Float64; + rng::Union{Nothing,AbstractRNG}=nothing, + seed::Union{Nothing,Integer}=nothing, ) -n = size(Λ)[1] -B = SimpleGraph(n) -A = bernoulli_graph(Λ; rng=rng, seed=seed) -A_adj = Int.(adjacency_matrix(A)) -for j in 1:n - for i in (j + 1):n - if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * A_adj[i, j]; rng=rng, seed=seed)) - add_edge!(B, i, j) + n = size(Λ)[1] + rng = rng_from_rng_or_seed(rng, seed) + B = SimpleGraph(n) + A = bernoulli_graph(Λ; rng=rng, seed=seed) + A_adj = Int.(adjacency_matrix(A)) + for j in 1:n + for i in (j + 1):n + if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * A_adj[i, j]; rng=rng)) + add_edge!(B, i, j) + end end end -end -return A, B + return A, B end \ No newline at end of file From 283a335e38bcf7cefa4ad0b03da8d128dc429a88 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 6 Dec 2022 12:00:18 +0100 Subject: [PATCH 19/46] Add parenthesis, remove cor --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 414b3e9b2..786bdb949 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,7 +11,7 @@ using Compat using DelimitedFiles using Base64 using Random -using Statistics: mean, std, cor +using Statistics: mean, std using StableRNGs const testdir = dirname(@__FILE__) @@ -21,7 +21,7 @@ const testdir = dirname(@__FILE__) end @testset verbose = true "Code formatting (JuliaFormatter.jl)" begin - @test format(Graphs; verbose=false, overwrite=false, ignore="vf2.jl") # TODO: remove ignore kwarg once the file is formatted correctly + @test format(Graphs; verbose=false, overwrite=false, ignore=["vf2.jl"]) # TODO: remove ignore kwarg once the file is formatted correctly end @testset verbose = true "Doctests (Documenter.jl)" begin From 7d5f9078055b7219d6649126d8a27f5df5793a2f Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Thu, 8 Dec 2022 16:23:14 +0100 Subject: [PATCH 20/46] Remove double space Co-authored-by: Emanuele Natale --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 75185f03a..fee4e8e64 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1426,7 +1426,7 @@ end """ bernoulli_graph(Λ) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ function bernoulli_graph( Λ::Matrix{Float64}; From 6cec8cc0dfb3f4b774ec202499ba384fbec7edda Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Thu, 8 Dec 2022 16:23:39 +0100 Subject: [PATCH 21/46] Add space Co-authored-by: Emanuele Natale --- test/simplegraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 8c2176193..93b119fb3 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -404,7 +404,7 @@ g6=bernoulli_graph(Λ,seed=1) @test g5==g6 for g in testgraphs(g5) - @test nv(g)== n + @test nv(g) == n end end end From 7275adb68da979460bbae5a7fa8062f2c39aeffd Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Tue, 13 Dec 2022 11:20:01 +0100 Subject: [PATCH 22/46] Remove brackets in ignore formatter --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 786bdb949..4cc782cb3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,7 +21,7 @@ const testdir = dirname(@__FILE__) end @testset verbose = true "Code formatting (JuliaFormatter.jl)" begin - @test format(Graphs; verbose=false, overwrite=false, ignore=["vf2.jl"]) # TODO: remove ignore kwarg once the file is formatted correctly + @test format(Graphs; verbose=false, overwrite=false, ignore="vf2.jl") # TODO: remove ignore kwarg once the file is formatted correctly end @testset verbose = true "Doctests (Documenter.jl)" begin From 7e8599beaeedcaab850d6a201ccb65fafd919ec7 Mon Sep 17 00:00:00 2001 From: Aurora Rossi <65721467+aurorarossi@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:52:21 +0100 Subject: [PATCH 23/46] Change Lambda type in Abstract Matrix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Simon Schölly --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index fee4e8e64..7307763e5 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1429,7 +1429,7 @@ end Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ function bernoulli_graph( - Λ::Matrix{Float64}; + Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing, seed::Union{Nothing,Integer}=nothing, ) From 07ae8e74d5b95487d4acba3286fa4f690325c38c Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:22:30 +0100 Subject: [PATCH 24/46] Remove seed in bernoulli graph --- src/SimpleGraphs/generators/randgraphs.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 7307763e5..01db395ac 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1431,11 +1431,10 @@ Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Berno function bernoulli_graph( Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing, - seed::Union{Nothing,Integer}=nothing, ) @assert size(Λ)[1] == size(Λ)[2] "The probability matrix must be a square matrix!" n = size(Λ)[1] - rng = rng_from_rng_or_seed(rng, seed) + rng = rng_from_rng_or_seed(rng) A = SimpleGraph(n) for j in 1:n for i in (j + 1):n From 559e49f27aad499cc9a9f7cad263827644825be4 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:24:21 +0100 Subject: [PATCH 25/46] Remove seed in test bernoulli graph --- test/simplegraphs/generators/randgraphs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 93b119fb3..304f80394 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -400,8 +400,8 @@ g4_adj = Int.(adjacency_matrix(g4)) @test g3_adj != g4_adj @test diag(g3_adj) == diag(g4_adj) == zeros(n) - g5=bernoulli_graph(Λ,seed=1) # check seed - g6=bernoulli_graph(Λ,seed=1) + g5=bernoulli_graph(Λ,rng) # check seed + g6=bernoulli_graph(Λ,rng) @test g5==g6 for g in testgraphs(g5) @test nv(g) == n From 855eb7911a8904b09a1165173f6d6a288da0bf5e Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:24:45 +0100 Subject: [PATCH 26/46] Remove comment --- test/simplegraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 304f80394..67a4229d2 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -400,7 +400,7 @@ g4_adj = Int.(adjacency_matrix(g4)) @test g3_adj != g4_adj @test diag(g3_adj) == diag(g4_adj) == zeros(n) - g5=bernoulli_graph(Λ,rng) # check seed + g5=bernoulli_graph(Λ,rng) g6=bernoulli_graph(Λ,rng) @test g5==g6 for g in testgraphs(g5) From 2a84eacfc3f37305ee29aad623f8f1870240595c Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:25:41 +0100 Subject: [PATCH 27/46] Remove seed in rho correlated --- src/SimpleGraphs/generators/randgraphs.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 01db395ac..658986b87 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1455,12 +1455,11 @@ function rho_correlated_bernoulli_graphs( Λ::Matrix{Float64}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing, - seed::Union{Nothing,Integer}=nothing, ) n = size(Λ)[1] - rng = rng_from_rng_or_seed(rng, seed) + rng = rng_from_rng_or_seed(rng) B = SimpleGraph(n) - A = bernoulli_graph(Λ; rng=rng, seed=seed) + A = bernoulli_graph(Λ; rng=rng) A_adj = Int.(adjacency_matrix(A)) for j in 1:n for i in (j + 1):n From 7e669103dc840b9847979e5253288442e54f3d86 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:31:14 +0100 Subject: [PATCH 28/46] Remove rng function --- src/SimpleGraphs/generators/randgraphs.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 658986b87..676889d14 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1434,7 +1434,6 @@ function bernoulli_graph( ) @assert size(Λ)[1] == size(Λ)[2] "The probability matrix must be a square matrix!" n = size(Λ)[1] - rng = rng_from_rng_or_seed(rng) A = SimpleGraph(n) for j in 1:n for i in (j + 1):n @@ -1457,7 +1456,6 @@ function rho_correlated_bernoulli_graphs( rng::Union{Nothing,AbstractRNG}=nothing, ) n = size(Λ)[1] - rng = rng_from_rng_or_seed(rng) B = SimpleGraph(n) A = bernoulli_graph(Λ; rng=rng) A_adj = Int.(adjacency_matrix(A)) From 0a1f121a587d96ed6a5a1a151068d238a9501e28 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 11:54:18 +0100 Subject: [PATCH 29/46] Change in sparse matrix rho correlated --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 676889d14..c269b5839 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1451,7 +1451,7 @@ rho_correlated_bernoulli_graphs(Λ,ρ) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ function rho_correlated_bernoulli_graphs( - Λ::Matrix{Float64}, + Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing, ) From 22ed22728492f8e32e6e72f952440e380c5d89ef Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 12:31:24 +0100 Subject: [PATCH 30/46] Remove seed test --- test/simplegraphs/generators/randgraphs.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 67a4229d2..ee688c7ab 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -387,7 +387,7 @@ @testset "bernoulli graphs" begin n = 50 - Λ = rand(n, n) + Λ = sparse(rand(n, n)) ρ = 1.0 # isomorphism case g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) g1_adj = Int.(adjacency_matrix(g1)) @@ -400,9 +400,7 @@ g4_adj = Int.(adjacency_matrix(g4)) @test g3_adj != g4_adj @test diag(g3_adj) == diag(g4_adj) == zeros(n) - g5=bernoulli_graph(Λ,rng) - g6=bernoulli_graph(Λ,rng) - @test g5==g6 + g5=bernoulli_graph(Λ;rng) for g in testgraphs(g5) @test nv(g) == n end From 7146795a6ba1dc6d69e50d5d6aece55772400884 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 12:37:39 +0100 Subject: [PATCH 31/46] Remove assert --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index c269b5839..fa550bb01 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1432,7 +1432,7 @@ function bernoulli_graph( Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing, ) - @assert size(Λ)[1] == size(Λ)[2] "The probability matrix must be a square matrix!" + size(Λ)[1] != size(Λ)[2] && throw(ArgumentError("The probability matrix must be a square matrix")) n = size(Λ)[1] A = SimpleGraph(n) for j in 1:n From 758e95f7a457a3c42a76b1a95a735385387e08ac Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 12:38:00 +0100 Subject: [PATCH 32/46] Add check Lambda symmetric --- src/SimpleGraphs/generators/randgraphs.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index fa550bb01..28b8eb013 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1432,7 +1432,8 @@ function bernoulli_graph( Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing, ) - size(Λ)[1] != size(Λ)[2] && throw(ArgumentError("The probability matrix must be a square matrix")) + size(Λ)[1] != size(Λ)[2] && throw(ArgumentError("The probability matrix must be a square matrix")) + !issymmetric(Λ) && throw(ArgumentError("Λ must be symmetric")) n = size(Λ)[1] A = SimpleGraph(n) for j in 1:n From e958ffdd7f0f3274c8bf403bfe18b55bd3da645d Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 12:45:43 +0100 Subject: [PATCH 33/46] Add test_throws bernoulli graphs --- test/simplegraphs/generators/randgraphs.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index ee688c7ab..91acd33cb 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -388,6 +388,9 @@ @testset "bernoulli graphs" begin n = 50 Λ = sparse(rand(n, n)) + @test_throws ArgumentError("Λ must be symmetric") bernoulli_graph(Λ;rng) + @test_throws ArgumentError("The probability matrix must be a square matrix") bernoulli_graph(sparse(rand(2,3));rng) + Λ=Symmetric(Λ) ρ = 1.0 # isomorphism case g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) g1_adj = Int.(adjacency_matrix(g1)) From 0323cbde1ab8965eaf59b9236430f7904981fe80 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 12:49:14 +0100 Subject: [PATCH 34/46] Change name variables and formatting --- src/SimpleGraphs/generators/randgraphs.jl | 32 +++++++++++------------ 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 28b8eb013..762d1808c 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1429,21 +1429,21 @@ end Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ function bernoulli_graph( - Λ::AbstractMatrix{<:AbstractFloat}; - rng::Union{Nothing,AbstractRNG}=nothing, + Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing ) - size(Λ)[1] != size(Λ)[2] && throw(ArgumentError("The probability matrix must be a square matrix")) - !issymmetric(Λ) && throw(ArgumentError("Λ must be symmetric")) + size(Λ)[1] != size(Λ)[2] && + throw(ArgumentError("The probability matrix must be a square matrix")) + !issymmetric(Λ) && throw(ArgumentError("Λ must be symmetric")) n = size(Λ)[1] - A = SimpleGraph(n) + g = SimpleGraph(n) for j in 1:n for i in (j + 1):n - if Bool(randbn(1, Λ[i, j]; rng=rng)) - add_edge!(A, i, j) + if Bool(randbn(1, Λ[i, j]; rng)) + add_edge!(g, i, j) end end end - return A + return g end """ @@ -1452,20 +1452,18 @@ rho_correlated_bernoulli_graphs(Λ,ρ) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ function rho_correlated_bernoulli_graphs( - Λ::AbstractMatrix{<:AbstractFloat}, - ρ::Float64; - rng::Union{Nothing,AbstractRNG}=nothing, + Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing ) n = size(Λ)[1] - B = SimpleGraph(n) - A = bernoulli_graph(Λ; rng=rng) - A_adj = Int.(adjacency_matrix(A)) + g2 = SimpleGraph(n) + g1 = bernoulli_graph(Λ; rng) + g1_adj = Int.(adjacency_matrix(g1)) for j in 1:n for i in (j + 1):n - if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * A_adj[i, j]; rng=rng)) - add_edge!(B, i, j) + if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * g1_adj[i, j]; rng)) + add_edge!(g2, i, j) end end end - return A, B + return g1, g2 end \ No newline at end of file From 806f93ded76495f5b9dfa3c10cfe4caabf09686a Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 13:12:19 +0100 Subject: [PATCH 35/46] Simplify lines --- src/SimpleGraphs/generators/randgraphs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 762d1808c..a6bf276c7 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1438,7 +1438,7 @@ function bernoulli_graph( g = SimpleGraph(n) for j in 1:n for i in (j + 1):n - if Bool(randbn(1, Λ[i, j]; rng)) + if rand(rng) <= Λ[i, j] add_edge!(g, i, j) end end @@ -1460,7 +1460,7 @@ function rho_correlated_bernoulli_graphs( g1_adj = Int.(adjacency_matrix(g1)) for j in 1:n for i in (j + 1):n - if Bool(randbn(1, (1 - ρ) * Λ[i, j] + ρ * g1_adj[i, j]; rng)) + if rand(rng) <= ((1 - ρ) * Λ[i, j] + ρ * g1_adj[i, j]) add_edge!(g2, i, j) end end From 0043a165d95704e2d9cd8b1b63e555d552ec227d Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 13:32:36 +0100 Subject: [PATCH 36/46] Remove `Int.` --- test/simplegraphs/generators/randgraphs.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 91acd33cb..fbfff3e96 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -393,14 +393,14 @@ Λ=Symmetric(Λ) ρ = 1.0 # isomorphism case g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) - g1_adj = Int.(adjacency_matrix(g1)) - g2_adj = Int.(adjacency_matrix(g2)) + g1_adj = adjacency_matrix(g1) + g2_adj = adjacency_matrix(g2) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) ρ = 0.5 # non isomorphism case g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) - g3_adj = Int.(adjacency_matrix(g3)) - g4_adj = Int.(adjacency_matrix(g4)) + g3_adj = adjacency_matrix(g3) + g4_adj = adjacency_matrix(g4) @test g3_adj != g4_adj @test diag(g3_adj) == diag(g4_adj) == zeros(n) g5=bernoulli_graph(Λ;rng) From fa9e5c7c5c603cc03a58cd199180d86fdc9bbb42 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 13:35:57 +0100 Subject: [PATCH 37/46] Add rng in docstrings --- src/SimpleGraphs/generators/randgraphs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index a6bf276c7..60dae3f7b 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1424,7 +1424,7 @@ function random_orientation_dag( end """ - bernoulli_graph(Λ) + bernoulli_graph(Λ; rng = nothing) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. """ @@ -1447,7 +1447,7 @@ function bernoulli_graph( end """ -rho_correlated_bernoulli_graphs(Λ,ρ) +rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ From 04ed8a24ecf439d7a35706f58418288b56d0913b Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 13:38:19 +0100 Subject: [PATCH 38/46] Add tuple info in docstring --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 60dae3f7b..86e5d0bcf 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1449,7 +1449,7 @@ end """ rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return a Tuple with two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. """ function rho_correlated_bernoulli_graphs( Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing From b5025e38d3c8c1727188378335363c993fce1113 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 13:40:19 +0100 Subject: [PATCH 39/46] Remove heavy latex --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 86e5d0bcf..1d41f22e5 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1426,7 +1426,7 @@ end """ bernoulli_graph(Λ; rng = nothing) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\operatorname{Bernoulli}(\\Lambda[i,j])``. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\Lambda[i,j]``. """ function bernoulli_graph( Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing From 6b040364ee14f3264d13eafc8cef265eef731490 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 17:20:45 +0100 Subject: [PATCH 40/46] Add explanation in docstring --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 1d41f22e5..5cb841865 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1449,7 +1449,7 @@ end """ rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return a Tuple with two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. +Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return a Tuple with two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. It means that, calling ``A`` and ``B`` the adjacency matrix of the outputed graphs, for ``i,j`` such that ``i``!=``j`` the Pearson correlation coefficient for ``A_{i,j}`` and ``B_{i,j}`` is ``\\rho``. """ function rho_correlated_bernoulli_graphs( Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing From 2746f6ff3d8b9221bba4da4cfebbb8f992017c65 Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 17:26:38 +0100 Subject: [PATCH 41/46] Add error rho interval --- src/SimpleGraphs/generators/randgraphs.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 5cb841865..deeb0fc21 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1454,6 +1454,7 @@ Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real numb function rho_correlated_bernoulli_graphs( Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing ) + (p < 0 || p > 1) && throw(ArgumentError("ρ must be in [0,1]")) n = size(Λ)[1] g2 = SimpleGraph(n) g1 = bernoulli_graph(Λ; rng) From f07275721b3a5b0b171ec40b11af4b607b5b559f Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 18:01:56 +0100 Subject: [PATCH 42/46] Add test rho exception --- src/SimpleGraphs/generators/randgraphs.jl | 2 +- test/simplegraphs/generators/randgraphs.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index deeb0fc21..eea9966c5 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1454,7 +1454,7 @@ Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real numb function rho_correlated_bernoulli_graphs( Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing ) - (p < 0 || p > 1) && throw(ArgumentError("ρ must be in [0,1]")) + (ρ < 0.0 || ρ > 1.0) && throw(ArgumentError("ρ must be in [0,1]")) n = size(Λ)[1] g2 = SimpleGraph(n) g1 = bernoulli_graph(Λ; rng) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index fbfff3e96..b30ff9a4a 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -391,6 +391,7 @@ @test_throws ArgumentError("Λ must be symmetric") bernoulli_graph(Λ;rng) @test_throws ArgumentError("The probability matrix must be a square matrix") bernoulli_graph(sparse(rand(2,3));rng) Λ=Symmetric(Λ) + @test_throws ArgumentError("ρ must be in [0,1]") rho_correlated_bernoulli_graphs(Λ, -1.; rng) ρ = 1.0 # isomorphism case g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) g1_adj = adjacency_matrix(g1) From b9ca0778e9dbfc486468aa37e9528cff7f5c0cdd Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Fri, 16 Dec 2022 18:09:52 +0100 Subject: [PATCH 43/46] Format --- test/simplegraphs/generators/randgraphs.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index b30ff9a4a..5871fcb60 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -388,23 +388,27 @@ @testset "bernoulli graphs" begin n = 50 Λ = sparse(rand(n, n)) - @test_throws ArgumentError("Λ must be symmetric") bernoulli_graph(Λ;rng) - @test_throws ArgumentError("The probability matrix must be a square matrix") bernoulli_graph(sparse(rand(2,3));rng) - Λ=Symmetric(Λ) - @test_throws ArgumentError("ρ must be in [0,1]") rho_correlated_bernoulli_graphs(Λ, -1.; rng) + @test_throws ArgumentError("Λ must be symmetric") bernoulli_graph(Λ; rng) + @test_throws ArgumentError("The probability matrix must be a square matrix") bernoulli_graph( + sparse(rand(2, 3)); rng + ) + Λ = Symmetric(Λ) + @test_throws ArgumentError("ρ must be in [0,1]") rho_correlated_bernoulli_graphs( + Λ, -1.0; rng + ) ρ = 1.0 # isomorphism case - g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) + g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng) g1_adj = adjacency_matrix(g1) g2_adj = adjacency_matrix(g2) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) ρ = 0.5 # non isomorphism case - g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng=rng) + g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng) g3_adj = adjacency_matrix(g3) g4_adj = adjacency_matrix(g4) @test g3_adj != g4_adj @test diag(g3_adj) == diag(g4_adj) == zeros(n) - g5=bernoulli_graph(Λ;rng) + g5 = bernoulli_graph(Λ; rng) for g in testgraphs(g5) @test nv(g) == n end From 64fc9ec95a795cc626f3c85390bf6bd948bffa1f Mon Sep 17 00:00:00 2001 From: aurorarossi Date: Sun, 26 Mar 2023 20:36:01 +0200 Subject: [PATCH 44/46] Add eltype specification --- src/SimpleGraphs/generators/randgraphs.jl | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index eea9966c5..680b4bd95 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1424,18 +1424,20 @@ function random_orientation_dag( end """ - bernoulli_graph(Λ; rng = nothing) + bernoulli_graph(Λ; rng = nothing, nodes_type = Int64) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\Lambda[i,j]``. """ function bernoulli_graph( - Λ::AbstractMatrix{<:AbstractFloat}; rng::Union{Nothing,AbstractRNG}=nothing + Λ::AbstractMatrix{<:AbstractFloat}; + rng::Union{Nothing,AbstractRNG}=nothing, + nodes_type::Type=Int64, ) size(Λ)[1] != size(Λ)[2] && throw(ArgumentError("The probability matrix must be a square matrix")) !issymmetric(Λ) && throw(ArgumentError("Λ must be symmetric")) n = size(Λ)[1] - g = SimpleGraph(n) + g = SimpleGraph{nodes_type}(n) for j in 1:n for i in (j + 1):n if rand(rng) <= Λ[i, j] @@ -1447,17 +1449,20 @@ function bernoulli_graph( end """ -rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing) +rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing, nodes_type = Int64) Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return a Tuple with two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. It means that, calling ``A`` and ``B`` the adjacency matrix of the outputed graphs, for ``i,j`` such that ``i``!=``j`` the Pearson correlation coefficient for ``A_{i,j}`` and ``B_{i,j}`` is ``\\rho``. """ function rho_correlated_bernoulli_graphs( - Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing + Λ::AbstractMatrix{<:AbstractFloat}, + ρ::Float64; + rng::Union{Nothing,AbstractRNG}=nothing, + nodes_type::Type=Int64, ) (ρ < 0.0 || ρ > 1.0) && throw(ArgumentError("ρ must be in [0,1]")) n = size(Λ)[1] - g2 = SimpleGraph(n) - g1 = bernoulli_graph(Λ; rng) + g2 = SimpleGraph{nodes_type}(n) + g1 = bernoulli_graph(Λ; rng, nodes_type=eltype(g2)) g1_adj = Int.(adjacency_matrix(g1)) for j in 1:n for i in (j + 1):n From 698eb4674731b45628691e21c564e1737abb0543 Mon Sep 17 00:00:00 2001 From: Aurora Rossi Date: Wed, 14 Feb 2024 16:42:36 +0100 Subject: [PATCH 45/46] Rename function --- src/Graphs.jl | 2 +- src/SimpleGraphs/SimpleGraphs.jl | 2 +- test/simplegraphs/generators/randgraphs.jl | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Graphs.jl b/src/Graphs.jl index 7998cee61..0141537b5 100644 --- a/src/Graphs.jl +++ b/src/Graphs.jl @@ -296,7 +296,7 @@ export dorogovtsev_mendes, random_orientation_dag, bernoulli_graph, - rho_correlated_bernoulli_graphs, + correlated_bernoulli_graphs, # community modularity, diff --git a/src/SimpleGraphs/SimpleGraphs.jl b/src/SimpleGraphs/SimpleGraphs.jl index a56bac0a7..e68b8fd3c 100644 --- a/src/SimpleGraphs/SimpleGraphs.jl +++ b/src/SimpleGraphs/SimpleGraphs.jl @@ -75,7 +75,7 @@ export AbstractSimpleGraph, kronecker, random_orientation_dag, bernoulli_graph, - rho_correlated_bernoulli_graphs, + correlated_bernoulli_graphs, # generators complete_graph, star_graph, diff --git a/test/simplegraphs/generators/randgraphs.jl b/test/simplegraphs/generators/randgraphs.jl index 5871fcb60..00c10fede 100644 --- a/test/simplegraphs/generators/randgraphs.jl +++ b/test/simplegraphs/generators/randgraphs.jl @@ -393,17 +393,17 @@ sparse(rand(2, 3)); rng ) Λ = Symmetric(Λ) - @test_throws ArgumentError("ρ must be in [0,1]") rho_correlated_bernoulli_graphs( + @test_throws ArgumentError("ρ must be in [0,1]") correlated_bernoulli_graphs( Λ, -1.0; rng ) ρ = 1.0 # isomorphism case - g1, g2 = rho_correlated_bernoulli_graphs(Λ, ρ; rng) + g1, g2 = correlated_bernoulli_graphs(Λ, ρ; rng) g1_adj = adjacency_matrix(g1) g2_adj = adjacency_matrix(g2) @test g1_adj == g2_adj @test diag(g1_adj) == diag(g2_adj) == zeros(n) ρ = 0.5 # non isomorphism case - g3, g4 = rho_correlated_bernoulli_graphs(Λ, ρ; rng) + g3, g4 = correlated_bernoulli_graphs(Λ, ρ; rng) g3_adj = adjacency_matrix(g3) g4_adj = adjacency_matrix(g4) @test g3_adj != g4_adj From 39627f68adef1b12412d72709ac32f74cb3b91f2 Mon Sep 17 00:00:00 2001 From: Aurora Rossi Date: Wed, 14 Feb 2024 16:42:51 +0100 Subject: [PATCH 46/46] Rename function and remove latex docstrings --- src/SimpleGraphs/generators/randgraphs.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SimpleGraphs/generators/randgraphs.jl b/src/SimpleGraphs/generators/randgraphs.jl index 680b4bd95..c58d850c1 100644 --- a/src/SimpleGraphs/generators/randgraphs.jl +++ b/src/SimpleGraphs/generators/randgraphs.jl @@ -1426,7 +1426,7 @@ end """ bernoulli_graph(Λ; rng = nothing, nodes_type = Int64) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}``, return a Bernoulli graph with ``n`` vertices. Each edge ``(i,j)`` exists with probability ``\\Lambda[i,j]``. +Given the symmetric matrix `Λ ∈ [0,1]^{n × n}`, return a Bernoulli graph with `n` vertices. Each edge `(i,j)` exists with probability `Λ[i,j]`. """ function bernoulli_graph( Λ::AbstractMatrix{<:AbstractFloat}; @@ -1449,11 +1449,11 @@ function bernoulli_graph( end """ -rho_correlated_bernoulli_graphs(Λ, ρ; rng = nothing, nodes_type = Int64) +correlated_bernoulli_graphs(Λ, ρ; rng = nothing, nodes_type = Int64) -Given the symmetric matrix ``\\Lambda \\in [0,1]^{n \\times n}`` and a real number ``\\rho \\in [0,1]`` return a Tuple with two ``\\rho``-correlated Bernoulli graphs with ``n`` vertices. It means that, calling ``A`` and ``B`` the adjacency matrix of the outputed graphs, for ``i,j`` such that ``i``!=``j`` the Pearson correlation coefficient for ``A_{i,j}`` and ``B_{i,j}`` is ``\\rho``. +Given the symmetric matrix `Λ ∈ [0,1]^{n × n}` and a real number `ρ ∈ [0,1]` return a Tuple with two ρ - correlated Bernoulli graphs with `n` vertices. It means that, calling `A` and `B` the adjacency matrix of the outputed graphs, for `i,j` such that `i!=j` the Pearson correlation coefficient for `A_{i,j}` and `B_{i,j}` is `ρ`. """ -function rho_correlated_bernoulli_graphs( +function correlated_bernoulli_graphs( Λ::AbstractMatrix{<:AbstractFloat}, ρ::Float64; rng::Union{Nothing,AbstractRNG}=nothing,