generate_lfr

Contents

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:
Nint

Number of vertices.

mufloat

Mixing parameter: the fraction of the edges of each vertex that go to other communities.

average_degreefloat (optional, default: None)

Target average degree. It is used to set min_degree if the latter is not given; one of the two must be given.

max_degreeint (optional, default: None)

Maximum degree. If not given, N - 1 is used.

tau1float (optional, default: 2.5)

Exponent of the power-law degree distribution.

tau2float (optional, default: 1.5)

Exponent of the power-law distribution of community sizes.

min_degreeint (optional, default: None)

Minimum degree.

min_communityint (optional, default: None)

Minimum community size. If not given, the minimum degree is used.

max_communityint (optional, default: None)

Maximum community size. If not given, the maximum degree is used.

max_iterint (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.

Returns:
gGraph

The generated undirected simple graph.

bVertexPropertyMap

The planted community of each vertex.

See also

generate_planted_partition

Planted partition model

generate_sbm

Stochastic block model

generate_relaxed_caveman

Relaxed caveman graph

graph_tool.inference.minimize_blockmodel_dl

Community 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:

  1. The degrees \(k_i\) are sampled from the discrete power law \(P(k)\propto k^{-\tau_1}\) in [min_degree, max_degree], where min_degree is chosen so that the expected average degree is closest to average_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 to max_degree).

  2. 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 below min_community.

  3. 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.

  4. 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.

  5. 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")
<...>
../_images/lfr.svg

Graph generated by the LFR benchmark with \(N=1000\) and \(\mu=0.2\). The vertex colors indicate the planted communities.#