Benchmarks for testing community detection algorithms on directed and weighted graphs with overlapping communities

Andrea Lancichinetti, Santo Fortunato

I Introduction

Complex systems are characterized by a division in subsystems, which in turn contain other subsystems in a hierarchical fashion. Herbert A. Simon, already in 1962, pointed out that such hierarchical organization plays a crucial role both in the generation and in the evolution of complex systems simon62. Many complex systems can be described as graphs, or networks, where the elementary parts of a system and their mutual interactions are nodes and links, respectively Newman:2003; vitorep. In a network, the subsystems appear as subgraphs with a high density of internal links, which are loosely connected to each other. These subgraphs are called communities and occur in a wide variety of networked systems Girvan:2002; miareview. Communities reveal how a network is internally organized, and indicate the presence of special relationships between the nodes, that may not be easily accessible from direct empirical tests. Communities may be groups of related individuals in social networks Girvan:2002; Lusseau:2005, sets of Web pages dealing with the same topic Flake:2002, biochemical pathways in metabolic networks Guimera:2005; palla, etc.

Most research on community detection focuses on the simplest case of undirected and unweighted graphs, as the problem is already very hard. However, links of networks from the real world are often directed and carry weights, and both features are essential to understand their function leicht08; barrat04. Moreover, in real graphs communities are sometimes overlapping palla, i. e. they share vertices. This aspect, frequent in certain types of systems, like social networks, has received some attention in the last years baumes05; zhang07; nepusz08; lancichinetti09. Finding communities in networks with directed and weighted edges and possibly overlapping communities is highly non-trivial. Many techniques working on undirected graphs, for instance, cannot be extended to include link direction. This implies the need of new approaches to the problem. In any case, once a method is designed, it is important to test it against reliable benchmarks. Since the new benchmark of Ref. lancichinetti08 is defined for undirected and unweighted graphs, we extend it here to the directed and weighted cases. For any type of benchmark, we will include the possibility to have overlapping communities. Sawardecker et al. have recently proposed a different benchmark with overlapping communities where the probability that two nodes are linked grows with the number of communities both nodes belong to sawardecker09.

Our algorithms to create the benchmark graphs have a computational complexity which grows linearly with the number of links and reduce considerably the fluctuations of specific realizations of the graphs, so that they come as close as possible to the type of structure described by the input parameters. We use our benchmark to make some testing of modularity optimization newman04, which is well defined in the case of directed and weighted networks arenas07c.

In Section II we describe the algorithms to create the new benchmarks. Tests are presented in Section III. Conclusions are summarized in Section IV.

II The benchmark

We start by presenting the algorithm to build the benchmark for undirected graphs with overlaps between communities. Then we extend it to the case of weighted and directed graphs.

The aim of this section is to describe the algorithm to generate undirected and unweighted benchmark graphs, where each node is allowed to have memberships in more communities. The algorithm consists of the following steps:

We first assign the number νi\nu_{i} of memberships of node ii, i.e. the number of communities the node belongs to. Of course, if each node has only one membership, we recover the benchmark of Ref. lancichinetti08; in general we can assign the number of memberships according to a certain distribution. Next, we assign the degrees {ki}\{k_{i}\} by drawing NN random numbers from a power law distribution power with exponent τ1\tau_{1}. We also introduce the topological mixing parameter μt\mu_{t}: ki(in)=(1−μt)kik_{i}^{(in)}=(1-\mu_{t})k_{i} is the internal degree of the node ii, i. e. the number of neighbors of node ii which have at least one membership in common with ii. In this way, the internal degree is a fixed fraction of the total degree for all the nodes. Of course, it is straightforward to generalize the algorithm to implement a different rule (one can introduce a non linear functional dependence, individual mixing parameters, etc.).

The community sizes {sξ}\{s_{\xi}\} are assigned by drawing random numbers from another power law with exponent τ2\tau_{2}. Naturally, the sum of the community sizes must equal the sum of the node memberships, i. e. ∑ξsξ=∑iνi\sum_{\xi}s_{\xi}=\sum_{i}\nu_{i}. Furthermore smax=max⁡{sξ}⩽Ns_{max}=\max\{s_{\xi}\}\leqslant N and νmax=max⁡{νi}⩽nc\nu_{max}=\max\{\nu_{i}\}\leqslant n_{c}, where NN is the number of nodes and ncn_{c} the number of communities. At this point, we have to decide which communities each node should be included into. This is equivalent to generating a bipartite network where the two classes are the ncn_{c} communities and the NN nodes; each community ξ\xi has sξs_{\xi} links, whereas each node has as many links as its memberships νi\nu_{i} (Fig. 1).

The network can be easily generated with the configuration model molloy95. To build the graph, it is important to take into account the constraint

where the sum is relative to the communities including node ii. This condition means that each node cannot have an internal degree larger than the highest possible number of nodes it can be connected to within the communities it stays in. We perform a rewiring process for the bipartite network until the constraint is satisfied. For some choices of the input parameters, it could happen that, after some iterations, the constraint is still unsatisfied. In this case one can change the sizes of the communities, by merging some of them, for instance. It turns out that this is not necessary in most situations and that, when it is, the perturbations introduced in the community size distributions are not too large. In general, it is convenient to start with a distribution of community sizes such that smin⩾kmin(in)s_{min}\geqslant k_{min}^{(in)} and smax⩾kmax(in)s_{max}\geqslant k_{max}^{(in)}.

So far we assigned an internal degree to each node but it has not been specified how many links should be distributed among the communities of the node. Again, one can follow several recipes; we chose the simple equipartition ki(ξ)=ki(in)/νik_{i}(\xi)=k_{i}^{(in)}/\nu_{i}, where ki(ξ)k_{i}(\xi) is the number of links which ii shares in community ξ\xi, provided that ii holds membership in ξ\xi. Some adjustments may be necessary to assure

Before generating the whole network, we start generating ncn_{c} subgraphs, one for each community. In fact, our definition of community ξ\xi is nothing but a random subgraph of sξs_{\xi} nodes with degree sequence {ki(ξ)}\{k_{i}(\xi)\}, which can be built via the configuration model, with a rewiring procedure to avoid multiple links. Note that Eq. 2 is necessary to generate the configuration model, but in general not sufficient. For one thing, we need ∑iki(ξ)\sum_{i}k_{i}(\xi) to be even. This might cause a change in the degree sequence, which is generally not appreciable. Once each subgraph is built, we obtain a graph divided in components. Note that because of the overlapping nodes, some components may be connected to each other, and in principle the whole graph might be connected. Furthermore, if two nodes belong simultaneously to the same two (or more) communities, the procedure may set more than one link between the nodes. A rewiring strategy similar to that described below suffices to avoid this problem.

The last step of the algorithm consists in adding the links external to the communities. To do this, let us consider the degree sequence {ki(ext)}\{k_{i}^{(ext)}\}, where simply ki(ext)=ki−ki(in)=μtkik_{i}^{(ext)}=k_{i}-k_{i}^{(in)}=\mu_{t}k_{i}. We want to insert randomly these links in our already built network without changing the internal degree sequences. In order to do so, we build a new network G(ext)\mathcal{G}^{(ext)} of NN nodes with degree sequence {ki(ext)}\{k_{i}^{(ext)}\}, and we perform a rewiring process each time we encounter a link between two nodes which have at least one membership in common (Fig. 2), since we are supposed to join only nodes of different communities at this stage.

Let us assume that AA and BB are in the same community and that they are linked in G(ext)\mathcal{G}^{(ext)}; we pick a node CC which does not share any membership with AA, and we look for a neighbor of CC (call it DD) which is not neighbor of BB. Next, we replace the links A−BA-B and C−DC-D with the new links A−CA-C and B−DB-D. This rewiring procedure can decrease the number of internal links of G(ext)\mathcal{G}^{(ext)} or leaving it unchanged (this happens only when BB and DD have one membership in common) but it cannot increase it. This means that after a few sweeps over all the nodes we reach a steady state where the number of internal links is very close to zero (if no node has ki∼Nk_{i}\sim N, the internal links of G(ext)\mathcal{G}^{(ext)} are just a few and one sweep is sufficient). Fig. 3 shows how the number of internal links decreases during the rewiring procedure. Finally, we have to superimpose G(ext)\mathcal{G}^{(ext)} on the previous one.

In our previous work about benchmarking lancichinetti08, we discussed the dispersion of the internal degree around the fixed value ki(in)k_{i}^{(in)}. In this case, if the number of internal links of G(ext)\mathcal{G}^{(ext)} goes to zero, the only reason not to have a perfectly sharp function for the distribution of the mixing parameters of the nodes in specific realizations of the new benchmark is a round-off problem, i.e. the problem of rounding integer numbers.

Other benchmarks, like that by Girvan and Newman, are based on a similar definition of communities, expressed in terms of different probabilities for internal and external links. One may wonder what is the connection between our benchmark and the others. It is not difficult to compute an approximation of how the probability of having a link between two nodes in the same community depends on the mixing parameter μt\mu_{t}.

In the configuration model, the probability to have a connection between nodes ii and jj with kik_{i} and kjk_{j} links respectively is approximately pij=kikj2mp_{ij}=\frac{k_{i}k_{j}}{2m}, provided that ki≪2mk_{i}\ll 2m and kj≪2mk_{j}\ll 2m. If the approximation holds, our prescription to assign ki(ξ)k_{i}(\xi) allows us to compute the probability that ii and jj get a link in the community ξ\xi:

where 2mξ=∑iki(ξ)2m_{\xi}=\sum_{i}k_{i}(\xi) is the number of internal links in the community (we recall that νi\nu_{i} is the number of memberships of node ii). If ii and jj share a number νij\nu_{ij} of memberships and all the respective pij(ξ)p_{ij}(\xi) are small, the probability that they get a link somewhere can be approximated with the sum over all the common communities. The final result is

where ⟨12mξ⟩ξ=1/νij∑ξ1/2mξ\langle\frac{1}{2m_{\xi}}\rangle_{\xi}=1/\nu_{ij}\sum_{\xi}1/2m_{\xi}, and ξ\xi runs only over the common memberships of the nodes.

On the other hand, if ii and jj do not share any membership, the probability to have a link between them is:

where 2m(ext)=∑iki(ext)=μt∑iki2m^{(ext)}=\sum_{i}k_{i}^{(ext)}=\mu_{t}\sum_{i}k_{i} is the number of external links in the network. The equation holds only if the rewiring process does not affect too much the probabilities, i.e. if the communities are small compared to the size of the network.

These results are based on some assumptions which are likely to be not exactly, but only approximately valid. Anyway, carrying out the right calculation is far from trivial and surely beyond the scope of this paper.

We conclude this section with a remark about the complexity of the algorithm. The configuration model takes a time growing linearly with the number of links mm of the network. If the rewiring procedure takes only a few iterations, like it happens in most instances, the complexity of the algorithm is O(m)O(m) (Fig. 4).

II.2 Weighted networks

In order to build a weighted network, we first generate an unweighted network with a given topological mixing parameter μt\mu_{t} and then we assign a positive real number to each link.

To do this we need to specify two other parameters, β\beta and μw\mu_{w}. The parameter β\beta is used to assign a strength sis_{i} to each node, si=kiβs_{i}=k_{i}^{\beta}; such power law relation between the strength and the degree of a node is frequently observed in real weighted networks barrat04. The parameter μw\mu_{w} is used to assign the internal strength si(in)=(1−μw)  sis_{i}^{(in)}=(1-\mu_{w})\,\,s_{i}, which is defined as the sum of the weights of the links between node ii and all its neighbors having at least one membership in common with ii. The problem is equivalent to finding an assignment of mm positive numbers {wij}\{w_{ij}\} such to minimize the following function:

Here sis_{i} and si(in,ext)s_{i}^{(in,ext)} indicate the strengths which we would like to assign, i.e. si=kiβs_{i}=k_{i}^{\beta}, si(in)=(1−μw)  sis_{i}^{(in)}=(1-\mu_{w})\,\,s_{i}, si(out)=μw  sis_{i}^{(out)}=\mu_{w}\,\,s_{i}; {ρi∗}\{\rho_{i}^{*}\} are the total, internal and external strengths of node ii defined through its link weights, i.e. ρi=∑jwij\rho_{i}=\sum_{j}w_{ij}, ρi(in)=∑jwij  κ(i,j)\rho_{i}^{(in)}=\sum_{j}w_{ij}\,\,\kappa(i,j), ρi(out)=∑jwij  (1−κ(i,j))\rho_{i}^{(out)}=\sum_{j}w_{ij}\,\,(1-\kappa(i,j)), where the function κ(i,j)=1\kappa(i,j)=1 if nodes ii and jj share at least one membership, and κ(i,j)=0\kappa(i,j)=0 otherwise.

We have to arrange things so that sis_{i} and si(in,ext)s_{i}^{(in,ext)} are consistent with the {ρi∗}\{\rho_{i}^{*}\}. For that we need a fast algorithm to minimize Var({wij})\text{Var}(\{w_{ij}\}). We found that the greedy algorithm described below can do this job well enough for the cases of our interest.

At the beginning wij=0w_{ij}=0, ∀i,j\forall i,j, so all the {ρi∗}\{\rho_{i}^{*}\} are zero.

We take node ii and increase the weight of each of its links by an amount ui=si−ρikiu_{i}=\frac{s_{i}-\rho_{i}}{k_{i}}, where ρi\rho_{i} indicates the sum of the links’ weights resulting from the previous step, i. e. before we increment them. In this way, since initially {ρi∗}=0\{\rho_{i}^{*}\}=0, the weights of the links of ii after the first step take the (equal for all) value siki\frac{s_{i}}{k_{i}}, and ρi=si\rho_{i}=s_{i} by construction, condition that is maintained along the whole procedure. We update {ρi∗}\{\rho_{i}^{*}\} for the node ii and its neighbors.

Still for node ii we increase all the weights wijw_{ij} by an amount si(in)−ρi(in)ki(in)\frac{s_{i}^{(in)}-\rho_{i}^{(in)}}{k_{i}^{(in)}} if κ(i,j)=1\kappa(i,j)=1 and by an amount −si(in)−ρi(in)ki(ext)-\frac{s_{i}^{(in)}-\rho_{i}^{(in)}}{k_{i}^{(ext)}} if κ(i,j)=0\kappa(i,j)=0. Again we update {ρi∗}\{\rho_{i}^{*}\} for the node ii and its neighbors. These two steps assure to set the contribute of node ii in Var({wij})\text{Var}(\{w_{ij}\}) to zero.

We repeat steps (2) and (3) for all the nodes. Two remarks are in order. First, we want each weight wij>0w_{ij}>0; so we update the weights only if this condition is fulfilled. Second, the contribute of the neighbors of node ii in Var({wij})\text{Var}(\{w_{ij}\}) will change and, of course, it can increase or decrease. For this reason, we need to iterate the procedure several times until a steady state is reached, or until we reach a certain value. With our procedure the value of Var({wij})\text{Var}(\{w_{ij}\}) decreases at least exponentially with the number of iterations, consisting in sweeps over all network links. (Fig. 5).

For the distribution of the weights wijw_{ij}, we expect the averages ⟨wi(int)⟩=1/ki(in)∑jwijκ(i,j)=si(in)/ki(in)\langle w_{i}^{(int)}\rangle=1/k_{i}^{(in)}\sum_{j}w_{ij}\kappa(i,j)=s_{i}^{(in)}/k_{i}^{(in)} and ⟨wi(ext)⟩=si(ext)/ki(ext)\langle w_{i}^{(ext)}\rangle=s_{i}^{(ext)}/k_{i}^{(ext)}. Note that these expressions can be related to the mixing parameters in a simple way (Fig. 6):

Since Var({wij})\text{Var}(\{w_{ij}\}) decreases exponentially, the number of iterations needed to reach convergence has a slow dependence on the size of the network so it does not contribute much to the total complexity, which remains O(m)O(m) (Fig. 7).

II.3 Directed networks

It is quite straightforward to generalize the previous algorithms to generate directed networks. Now, we have an indegree sequence {yi}\{y_{i}\} and an outdegree sequence {zi}\{z_{i}\} but we can still go through all the steps of the construction of the benchmark for undirected networks with just some slight modifications. In the following, we list what to change in each point of the corresponding list in Section II.1.

We decided to sample the indegree sequence from a power law and the outdegree sequence from a δ\delta-distribution (with the obvious constraint ∑iyi=∑izi\sum_{i}y_{i}=\sum_{i}z_{i}). We need to define the internal in- and outdegrees yi(ξ)y_{i}(\xi) and zi(ξ)z_{i}(\xi) with respect to every community ξ\xi, which can be done by introducing two mixing parameters. For simplicity one can set them equal.

It is necessary that Eq. 2 holds for both {yi}\{y_{i}\} and {zi}\{z_{i}\}.

We need to use the configuration model for directed networks, and the condition that ∑iki(ξ)\sum_{i}k_{i}(\xi) should be even is replaced by ∑iyi(ξ)=∑izi(ξ)\sum_{i}y_{i}(\xi)=\sum_{i}z_{i}(\xi); because of this condition it might be necessary to change yi(ξ)y_{i}(\xi) and/or zi(ξ)z_{i}(\xi). We decided to modify only zi(ξ)z_{i}(\xi), whenever necessary.

The rewiring procedure can be done by preserving both distributions of indegree and outdegree, for instance, by adopting the following scheme: before rewiring, AA points to BB and DD to CC; after rewiring, AA points to CC and DD to BB.

In order to generate directed and weighted networks, we use the following relation between the strength sis_{i} of a node and its in- and outdegree: si=(yi+zi)βs_{i}=(y_{i}+z_{i})^{\beta}. Given a node ii, one considers all its neighbors, regardless of the link directions (note that ii may have the same neighbor counted twice if the link runs in both directions). Otherwise, the procedure to insert weights is equivalent.

In directed networks, the directedness of the links may reflect some interesting structural information that is not present in the corresponding undirected version of the graph. For instance there could be flows, represented by many links with the same direction running from one subgraph to another: such subgraphs might correspond to important classifications of the nodes. Our directed benchmark is based on the balance between the numbers of internal and external links, and it does not seem suitable to generate graphs with flows. However, this is not true: flows can be generated by introducing proper constraints on the number of incoming and outgoing links of the communities.

Suppose we want to generate a network with two communities only, where the nodes of community 11 point to nodes of community 22 but not vice versa and there are a few random connections among nodes in the same community. We could use our algorithm in this way: first we build separately the two subgraphs; then we set yi(ext)≃0y_{i}^{(ext)}\simeq 0 for nodes in the community 11 and zi(ext)≃0z_{i}^{(ext)}\simeq 0 for nodes in community 22 and build G(ext)\mathcal{G}^{(ext)}. If there are more communities, one first builds as many subgraphs as necessary and then links them according to the desired flow patterns.

Methods based on mixture models newman07; ramasco08 may detect this kind of structures. Methods based on a balance between internal and external links, like (directed) modularity optimization may have problems. For example (Fig. 8), consider a network with three communities AA, BB, CC, with 1010 nodes in each community, each node with 33 in-links and 33 out-links on average; nodes in AA point to 22 nodes in BB, nodes in BB point to 22 nodes in CC, and nodes in CC point to 22 nodes in AA; each node points to 11 node in its own community. The modularity of this partition is Q=0Q=0, therefore the optimization would give a different partition, as the maximum modularity for a graph is usually positive.

III Tests

Here we present some tests of community detection methods on our benchmark graphs.

We focused on two techniques: modularity optimization, because it is one of very few methods that can be extended to the cases of directed and weighted graphs arenas07c; the Clique Percolation Method (CPM) by Palla et al. palla, a popular method to find community structure with overlapping communities. The optimization of modularity was carried out by using simulated annealing Guimera:2005.

To measure the similarity between the built-in modular structure of the benchmark and the one delivered by the algorithm we adopt the normalized mutual information, a measure borrowed from information theory Danon:2005. We stress that other choices for the similarity measure are possible (for a survey, see meila) and that we use the normalized mutual information for two main reasons: 1) it is regularly used in papers about community detection, so one has a clear idea of the performance of the algorithms by looking at the results, compared to similar plots; 2) it has been recently extended to the case of overlapping communities lancichinetti09, whereas most other measures have no such extension.

Fig. 9 shows the result for the directed (unweighted) benchmark graphs, without overlapping communities. The plot shows a very similar pattern as that observed in the undirected case lancichinetti08.

For the weighted benchmark (still without overlapping communities) we can tune two parameters, μt\mu_{t} and μw\mu_{w}. Fig. 10 refers to networks where we set μt=μw\mu_{t}=\mu_{w}, while in Fig. 11 we set μt=0.5\mu_{t}=0.5. Since, for μw<0.5\mu_{w}<0.5, μt\mu_{t} is smaller for the networks of Fig. 10 than for those in Fig. 11, we would expect to see better performances of modularity optimization in Fig. 10 in the range 0≤μw<0.50\leq\mu_{w}<0.5. Instead, we get the opposite result. The reason is that the links between communities carry on average more weight when μt<μw\mu_{t}<\mu_{w} than when μt=μw\mu_{t}=\mu_{w}, and this enhances the chance that mergers between small communities occur, leading to higher values of modularity FB. Because of such mergers, the partition found by the method can be quite different from the planted partition of the benchmark.

In Figs. 12 and 13 we show the results of tests performed with the CPM on our benchmarks with overlapping communities. In this case, the mixing parameter μt\mu_{t} is fixed and one varies the fraction of overlapping nodes between communities. We have run the CPM for different types of kk-cliques (kk indicates the number of nodes of the clique), with k=3,4,5,6k=3,4,5,6. In general we notice that triangles (k=3k=3) yield the worst performance, whereas 44- and 55-cliques give better results. In the two top diagrams community sizes range between smin=10s_{min}=10 and smax=50s_{max}=50, whereas in the bottom diagrams the range goes from smin=20s_{min}=20 and smax=100s_{max}=100. By comparing the diagrams in the top with those in the bottom we see that the algorithm performs better when communities are (on average) smaller. The networks used to produce Fig. 12 consist of 10001000 nodes, whereas those of Fig. 13 consist of 50005000 nodes. From the comparison of Fig. 12 with Fig. 13 we see that the algorithm performs better on networks of larger size.

IV Summary

In this paper we have introduced new benchmark graphs to test community detection methods on directed and weighted networks. The new graphs are suitable extensions of the benchmark we have recently introduced in Ref. lancichinetti08, in that they account for the fat-tailed distributions of node degree and community size that are observed in real networks. Furthermore we have equipped all our new benchmark graphs with the option of having overlapping communities, an important feature of community structure in real networks. With this work we have provided researchers working on the problem of detecting communities in graphs with a complete set of tools to make stringent objective tests of their algorithms, something which is sorely needed in this field. We have developed and carefully tested a software package for the generation of each class of benchmark graphs, all of which can be freely downloaded software.

References