generate_lfr#
- graph_tool.generation.generate_lfr(N, mu, average_degree=None, max_degree=None, tau1=2.5, tau2=1.5, min_degree=None, min_community=None, max_community=None, max_iter=100)[git][source]#
Generate a random graph with planted communities according to the benchmark of Lancichinetti, Fortunato, and Radicchi (LFR).
- Parameters:
- N
int Number of vertices.
- mu
float Mixing parameter: the fraction of the edges of each vertex that go to other communities.
- average_degree
float(optional, default:None) Target average degree. It is used to set
min_degreeif the latter is not given; one of the two must be given.- max_degree
int(optional, default:None) Maximum degree. If not given,
N - 1is used.- tau1
float(optional, default:2.5) Exponent of the power-law degree distribution.
- tau2
float(optional, default:1.5) Exponent of the power-law distribution of community sizes.
- min_degree
int(optional, default:None) Minimum degree.
- min_community
int(optional, default:None) Minimum community size. If not given, the minimum degree is used.
- max_community
int(optional, default:None) Maximum community size. If not given, the maximum degree is used.
- max_iter
int(optional, default:100) Maximum number of attempts in each randomized step (sampling of the community sizes, pairing of the external half-edges), multiplied by the number of vertices in the assignment of the communities and by the number of edges in the removal of parallel edges and self-loops.
- N
- Returns:
- g
Graph The generated undirected simple graph.
- b
VertexPropertyMap The planted community of each vertex.
- g
See also
generate_planted_partitionPlanted partition model
generate_sbmStochastic block model
generate_relaxed_cavemanRelaxed caveman graph
graph_tool.inference.minimize_blockmodel_dlCommunity detection
Notes
The LFR benchmark [lancichinetti-benchmark-2008] is a planted partition model in which both the degrees and the community sizes follow power laws, as in many empirical networks, and the fraction of edges of each vertex that go to other communities is fixed. It is widely used to evaluate community detection methods.
The graph is generated as follows:
The degrees \(k_i\) are sampled from the discrete power law \(P(k)\propto k^{-\tau_1}\) in
[min_degree, max_degree], wheremin_degreeis chosen so that the expected average degree is closest toaverage_degree, if the latter is given. If their sum is odd, the degree of a random vertex is increased by one (or decreased, if it is already equal tomax_degree).The community sizes are sampled from \(P(s)\propto s^{-\tau_2}\) in
[min_community, max_community]until their sum reaches or exceeds \(N\), and the last size is then reduced so that the sum is exactly \(N\); the whole sequence is sampled again if this last size falls belowmin_community.Each vertex \(i\) has the internal degree \(\lfloor(1-\mu)k_i\rceil\), and is assigned to a random community large enough to hold it, displacing a random member if the community is full, until every vertex is assigned [lancichinetti-benchmark-2008]. If the sum of the internal degrees of a community is odd, one of them is decreased by one (and the external degree increased), and while they are not graphical (see
is_graphical()), the two largest are decreased by one.The internal edges of each community form a random simple graph with the internal degrees, obtained from the Havel-Hakimi realization (see
realize_degree_sequence()) by ten random double edge swaps per edge, which preserve the degrees.The external half-edges, \(k_i\) minus the internal degree for each vertex \(i\), are paired uniformly at random, and every pair inside a single community is then swapped with a random pair until all pairs connect different communities. The parallel edges among them are removed in the same way, by swapping the endpoints of pairs of external edges, which preserves all degrees and keeps every edge between different communities.
Hence, the degrees of the vertices are exactly those sampled, and so are their internal and external degrees, up to the corrections in step 3, which affect few vertices. A stochastic block model with similar properties is obtained with
generate_sbm(), where, unlike here, the fraction of edges of each vertex inside its community fluctuates.References
[lancichinetti-benchmark-2008] (1,2)Andrea Lancichinetti, Santo Fortunato and Filippo Radicchi, “Benchmark graphs for testing community detection algorithms”, Phys. Rev. E 78, 046110 (2008), DOI: 10.1103/PhysRevE.78.046110 [sci-hub, @tor], arXiv: 0805.4770
Examples
>>> g, b = gt.generate_lfr(1000, 0.2, average_degree=10, max_degree=50) >>> print(g.num_vertices(), len(set(b.a))) 1000 81 >>> state = gt.minimize_blockmodel_dl(g) >>> print(round(gt.mutual_information(b, state.get_blocks(), norm=True), 1)) 0.9 >>> gt.graph_draw(g, vertex_fill_color=b, output="lfr.svg") <...>
Graph generated by the LFR benchmark with \(N=1000\) and \(\mu=0.2\). The vertex colors indicate the planted communities.#