Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
adding Dorogovtsev Mendes random graph generator (#1032)
Browse files Browse the repository at this point in the history
* adding Dorogovtsev Mendes random graph generator
* add docs for dorogovtsev_mendes
* replace fadjlist call with outneighbors
* add docs to describe accessing g[1:t]
  • Loading branch information
mcognetta authored and jpfairbanks committed Oct 5, 2018
1 parent 8928190 commit d9787a2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/LightGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ loadgraph, loadgraphs, savegraph, LGFormat,
erdos_renyi, expected_degree_graph, watts_strogatz, random_regular_graph, random_regular_digraph,
random_configuration_model, random_tournament_digraph, StochasticBlockModel, make_edgestream,
nearbipartiteSBM, blockcounts, blockfractions, stochastic_block_model, barabasi_albert,
barabasi_albert!, static_fitness_model, static_scale_free, kronecker,
barabasi_albert!, static_fitness_model, static_scale_free, kronecker, dorogovtsev_mendes,

#community
modularity, core_periphery_deg,
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleGraphs/SimpleGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export AbstractSimpleGraph, AbstractSimpleEdge,
erdos_renyi, expected_degree_graph, watts_strogatz, random_regular_graph,
random_regular_digraph, random_configuration_model, random_tournament_digraph,
StochasticBlockModel, make_edgestream, nearbipartiteSBM, blockcounts,
blockfractions, stochastic_block_model, barabasi_albert,
blockfractions, stochastic_block_model, barabasi_albert, dorogovtsev_mendes,
barabasi_albert!, static_fitness_model, static_scale_free, kronecker,
#generators
CompleteGraph, StarGraph, PathGraph, WheelGraph, CycleGraph,
Expand Down
42 changes: 42 additions & 0 deletions src/SimpleGraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1004,3 +1004,45 @@ function kronecker(SCALE, edgefactor, A=0.57, B=0.19, C=0.19)
end
return g
end

"""
dorogovtsev_mendes(n)
Generate a random `n` vertex graph by the Dorogovtsev-Mendes method (with `n \\ge 3`).
The Dorogovtsev-Mendes process begins with a triangle graph and inserts `n-3` additional vertices.
Each time a vertex is added, a random edge is selected and the new vertex is connected to the two
endpoints of the chosen edge. This creates graphs with a many triangles and a high local clustering coefficient.
It is often useful to track the evolution of the graph as vertices are added, you can access the graph from
the `t`th stage of this algorithm by accessing the first `t` vertices with `g[1:t]`.
### References
- http://graphstream-project.org/doc/Generators/Dorogovtsev-Mendes-generator/
- https://arxiv.org/pdf/cond-mat/0106144.pdf#page=24
"""
function dorogovtsev_mendes(n::Integer; seed::Int=-1)
n < 3 && throw(DomainError("n=$n must be at least 3"))
rng = getRNG(seed)
g = CycleGraph(3)

for iteration in 1:(n-3)
chosenedge = rand(rng, 1:(2*ne(g))) # undirected so each edge is listed twice in adjlist
u, v = -1, -1
for i in 1:nv(g)
edgelist = outneighbors(g, i)
if chosenedge > length(edgelist)
chosenedge -= length(edgelist)
else
u = i
v = edgelist[chosenedge]
break
end
end

add_vertex!(g)
add_edge!(g, nv(g), u)
add_edge!(g, nv(g), v)
end
return g
end
11 changes: 11 additions & 0 deletions test/simplegraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,15 @@
kg = @inferred kronecker(5, 5)
@test nv(kg) == 32
@test is_directed(kg)

g = @inferred(dorogovtsev_mendes(10))
@test nv(g) == 10 && ne(g) == 17
g = dorogovtsev_mendes(11)
@test nv(g) == 11 && ne(g) == 19
@test δ(g) == 2
g = dorogovtsev_mendes(3)
@test nv(g) == 3 && ne(g) == 3
# testing domain errors
@test_throws DomainError dorogovtsev_mendes(2)
@test_throws DomainError dorogovtsev_mendes(-1)
end

0 comments on commit d9787a2

Please sign in to comment.