# How to generate networks of a certain density?

How can you generate small world or scale-free networks with a certain density, say .3?

In the case of random networks it seems easy because an edge is drawn based on a certain probability. However, this doesn't seem to be so straightforward for the small world and scale-free architectures. I was thinking of starting with a dense network and randomly remove edges until a certain density is met. But I think this is not correct, because in the case of small world and scale-free architectures the edges are drawn iteratively using rewiring probability and preferential attachment.

I am trying to do this in R, using igraph and any hints would be extremely helpful.

For small-world networks, a possible approach is to start with a lattice with the desired density and, then, to rewire some edges to obtain a small-world network (very much in the spirit of the Watts and Strogatz model).

For example, if you want a network with a density of approximately $0.3$, you could create a lattice with this density and rewire some percentage of the edges:

library(igraph)

l <- make_lattice(100, nei = 17)
# creates a 1 dimensional lattice with 100 edges
# and with each vertice linked to its neighbours up two a distance of 17 in the graph

edge_density(l) # This is approximately 0.31

r <- rewire(l, each_edge(prob = 0.01, loops = FALSE, multiple = FALSE))
# This rewires 1% of the edges in the network.