igraph Reference Manual

For using the igraph C library

Chapter 22. Detecting Community Structure

1. Common functions related to community structure

1.1. igraph_modularity — Calculate the modularity of a graph with respect to some vertex types

int igraph_modularity(const igraph_t *graph, 
		      const igraph_vector_t *membership,
		      igraph_real_t *modularity,
			  const igraph_vector_t *weights);

The modularity of a graph with respect to some division (or vertex types) measures how good the division is, or how separated are the different vertex types from each other. It is defined as Q=1/(2m) * sum((Aij - ki*kj / (2m)) delta(ci,cj), i, j), here `m' is the number of edges, `Aij' is the element of the `A' adjacency matrix in row `i' and column `j', `ki' is the degree of `i', `kj' is the degree of `j', `ci' is the type (or component) of `i', `cj' that of `j', the sum goes over all `i' and `j' pairs of vertices, and `delta(x,y)' is one if x=y and zero otherwise.

Modularity on weighted graphs is also meaningful. When taking edge weights into account, `Aij' becomes the weight of the corresponding edge (or 0 if there is no edge), `ki' is the total weight of edges incident on vertex `i', `kj' is the total weight of edges incident on vertex `j' and `m' is the total weight of all edges.

See also Clauset, A.; Newman, M. E. J.; Moore, C. Finding community structure in very large networks, Physical Review E, 2004, 70, 066111.

Arguments: 

graph:

The input graph.

membership:

Numeric vector which gives the type of each vertex, ie. the component to which it belongs. It does not have to be consecutive, i.e. empty communities are allowed.

modularity:

Pointer to a real number, the result will be stored here.

weights:

Weight vector or NULL if no weights are specified.

Returns: 

Error code.

Time complexity: O(|V|+|E|), the number of vertices plus the number of edges.

1.2. igraph_community_optimal_modularity — Calculate the community structure with the highest modularity value

int igraph_community_optimal_modularity(const igraph_t *graph,
					igraph_real_t *modularity,
					igraph_vector_t *membership,
					const igraph_vector_t *weights);

This function calculates the optimal community structure for a graph, in terms of maximal modularity score.

The calculation is done by transforming the modularity maximization into an integer programming problem, and then calling the GLPK library to solve that. Please see Ulrik Brandes et al.: On Modularity Clustering, IEEE Transactions on Knowledge and Data Engineering 20(2):172-188, 2008.

Note that modularity optimization is an NP-complete problem, and all known algorithms for it have exponential time complexity. This means that you probably don't want to run this function on larger graphs. Graphs with up to fifty vertices should be fine, graphs with a couple of hundred vertices might be possible.

Arguments: 

graph:

The input graph. It is always treated as undirected.

modularity:

Pointer to a real number, or a null pointer. If it is not a null pointer, then a optimal modularity value is returned here.

membership:

Pointer to a vector, or a null pointer. If not a null pointer, then the membership vector of the optimal community structure is stored here.

weights:

Vector giving the weights of the edges. If it is NULL then each edge is supposed to have the same weight.

Returns: 

Error code.

See also: 

igraph_modularity(), igraph_community_fastgreedy() for an algorithm that finds a local optimum in a greedy way.

Time complexity: exponential in the number of vertices.

Example 22.1.  File examples/simple/igraph_community_optimal_modularity.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2010-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

void prepare_weights_vector(igraph_vector_t* weights, const igraph_t* graph) {
  int i, n = igraph_ecount(graph);
  igraph_vector_resize(weights, n);
  for (i = 0; i < n; i++) {
    VECTOR(*weights)[i] = i % 5;
  }
}

int main() {
  igraph_t graph;

  igraph_vector_t v;
  igraph_real_t edges[] = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
			    0,10, 0,11, 0,12, 0,13, 0,17, 0,19, 0,21, 0,31,
			    1, 2, 1, 3, 1, 7, 1,13, 1,17, 1,19, 1,21, 1,30,
			    2, 3, 2, 7, 2,27, 2,28, 2,32, 2, 9, 2, 8, 2,13,
			    3, 7, 3,12, 3,13, 4, 6, 4,10, 5, 6, 5,10, 5,16,
			    6,16, 8,30, 8,32, 8,33, 9,33,13,33,14,32,14,33,
			    15,32,15,33,18,32,18,33,19,33,20,32,20,33,
			    22,32,22,33,23,25,23,27,23,32,23,33,23,29,
			    24,25,24,27,24,31,25,31,26,29,26,33,27,33,
			    28,31,28,33,29,32,29,33,30,32,30,33,31,32,31,33,
			    32,33
  };

  igraph_vector_t membership;
  igraph_vector_t weights;
  igraph_real_t modularity;
  igraph_bool_t simple;
  int retval;

  igraph_vector_view(&v, edges, sizeof(edges)/sizeof(double));
  igraph_create(&graph, &v, 0, IGRAPH_UNDIRECTED);

  igraph_vector_init(&weights, 0);

  igraph_is_simple(&graph, &simple);
  if (!simple) { return 1; }

  igraph_vector_init(&membership, 0);

  igraph_set_error_handler(&igraph_error_handler_printignore);

  /* Zachary karate club, unweighted */
  retval = igraph_community_optimal_modularity(&graph, &modularity,
					       &membership, 0);
  if (retval == IGRAPH_UNIMPLEMENTED) { return 77; }
  if (fabs(modularity - 0.4197896) > 0.0000001) { return 2; }
  /* Zachary karate club, weighted */
  prepare_weights_vector(&weights, &graph);
  igraph_community_optimal_modularity(&graph, &modularity,
				      &membership, &weights);
  if (fabs(modularity - 0.5115767) > 0.0000001) { return 4; }
  igraph_destroy(&graph);

  /* simple graph with loop edges, unweighted */
  igraph_small(&graph, 6, IGRAPH_UNDIRECTED,
          0,1,1,2,2,3,3,4,4,5,5,0,0,0,2,2,-1);
  igraph_community_optimal_modularity(&graph, &modularity,
				      &membership, 0);
  if (fabs(modularity - 0.28125) > 0.00001) { return 3; }
  /* simple graph with loop edges, weighted */
  prepare_weights_vector(&weights, &graph);
  igraph_community_optimal_modularity(&graph, &modularity,
				      &membership, &weights);
  if (fabs(modularity - 0.36686) > 0.00001) { return 5; }
  igraph_destroy(&graph);

  igraph_vector_destroy(&membership);
  igraph_vector_destroy(&weights);

  return 0;
}


1.3. igraph_community_to_membership — Create membership vector from community structure dendrogram

int igraph_community_to_membership(const igraph_matrix_t *merges,
				   igraph_integer_t nodes,
				   igraph_integer_t steps,
				   igraph_vector_t *membership,
				   igraph_vector_t *csize);

This function creates a membership vector from a community structure dendrogram. A membership vector contains for each vertex the id of its graph component, the graph components are numbered from zero, see the same argument of igraph_clusters() for an example of a membership vector.

Many community detection algorithms return with a merges matrix, igraph_community_walktrap() and igraph_community_edge_betweenness() are two examples. The matrix contains the merge operations performed while mapping the hierarchical structure of a network. If the matrix has n-1 rows, where n is the number of vertices in the graph, then it contains the hierarchical structure of the whole network and it is called a dendrogram.

This function performs steps merge operations as prescribed by the merges matrix and returns the current state of the network.

If merges is not a complete dendrogram, it is possible to take steps steps if steps is not bigger than the number lines in merges.

Arguments: 

merges:

The two-column matrix containing the merge operations. See igraph_community_walktrap() for the detailed syntax.

nodes:

The number of leaf nodes in the dendrogram

steps:

Integer constant, the number of steps to take.

membership:

Pointer to an initialized vector, the membership results will be stored here, if not NULL. The vector will be resized as needed.

csize:

Pointer to an initialized vector, or NULL. If not NULL then the sizes of the components will be stored here, the vector will be resized as needed.

See also: 

Time complexity: O(|V|), the number of vertices in the graph.

1.4. igraph_reindex_membership — Makes the IDs in a membership vector continuous

int igraph_reindex_membership(igraph_vector_t *membership,
                              igraph_vector_t *new_to_old);

This function reindexes component IDs in a membership vector in a way that the new IDs start from zero and go up to C-1, where C is the number of unique component IDs in the original vector. This function was contributed by Tom Gregorovic.

Arguments: 

membership:

Numeric vector which gives the type of each vertex, ie. the component to which it belongs. The vector will be altered in-place.

new_to_old:

Pointer to a vector which will contain the old component ID for each new one, or NULL, in which case it is not returned. The vector will be resized as needed.

Time complexity: should be O(n log n) for n elements.

1.5. igraph_compare_communities — Compares community structures using various metrics

int igraph_compare_communities(const igraph_vector_t *comm1,
    const igraph_vector_t *comm2, igraph_real_t* result,
    igraph_community_comparison_t method);

This function assesses the distance between two community structures using the variation of information (VI) metric of Meila (2003), the normalized mutual information (NMI) of Danon et al (2005), the split-join distance of van Dongen (2000), the Rand index of Rand (1971) or the adjusted Rand index of Hubert and Arabie (1985).

References:

Meila M: Comparing clusterings by the variation of information. In: Schölkopf B, Warmuth MK (eds.). Learning Theory and Kernel Machines: 16th Annual Conference on Computational Learning Theory and 7th Kernel Workshop, COLT/Kernel 2003, Washington, DC, USA. Lecture Notes in Computer Science, vol. 2777, Springer, 2003. ISBN: 978-3-540-40720-1.

Danon L, Diaz-Guilera A, Duch J, Arenas A: Comparing community structure identification. J Stat Mech P09008, 2005.

van Dongen S: Performance criteria for graph clustering and Markov cluster experiments. Technical Report INS-R0012, National Research Institute for Mathematics and Computer Science in the Netherlands, Amsterdam, May 2000.

Rand WM: Objective criteria for the evaluation of clustering methods. J Am Stat Assoc 66(336):846-850, 1971.

Hubert L and Arabie P: Comparing partitions. Journal of Classification 2:193-218, 1985.

Arguments: 

comm1:

the membership vector of the first community structure

comm2:

the membership vector of the second community structure

result:

the result is stored here.

method:

the comparison method to use. IGRAPH_COMMCMP_VI selects the variation of information (VI) metric of Meila (2003), IGRAPH_COMMCMP_NMI selects the normalized mutual information measure proposed by Danon et al (2005), IGRAPH_COMMCMP_SPLIT_JOIN selects the split-join distance of van Dongen (2000), IGRAPH_COMMCMP_RAND selects the unadjusted Rand index (1971) and IGRAPH_COMMCMP_ADJUSTED_RAND selects the adjusted Rand index.

Returns: 

Error code.

Time complexity: O(n log(n)).

1.6. igraph_split_join_distance — Calculates the split-join distance of two community structures

int igraph_split_join_distance(const igraph_vector_t *comm1,
    const igraph_vector_t *comm2, igraph_integer_t *distance12,
    igraph_integer_t *distance21);

The split-join distance between partitions A and B is the sum of the projection distance of A from B and the projection distance of B from A. The projection distance is an asymmetric measure and it is defined as follows:

First, each set in partition A is evaluated against all sets in partition B. For each set in partition A, the best matching set in partition B is found and the overlap size is calculated. (Matching is quantified by the size of the overlap between the two sets). Then, the maximal overlap sizes for each set in A are summed together and subtracted from the number of elements in A.

The split-join distance will be returned in two arguments, distance12 will contain the projection distance of the first partition from the second, while distance21 will be the projection distance of the second partition from the first. This makes it easier to detect whether a partition is a subpartition of the other, since in this case, the corresponding distance will be zero.

Reference:

van Dongen S: Performance criteria for graph clustering and Markov cluster experiments. Technical Report INS-R0012, National Research Institute for Mathematics and Computer Science in the Netherlands, Amsterdam, May 2000.

Arguments: 

comm1:

the membership vector of the first community structure

comm2:

the membership vector of the second community structure

distance12:

pointer to an igraph_integer_t, the projection distance of the first community structure from the second one will be returned here.

distance21:

pointer to an igraph_integer_t, the projection distance of the second community structure from the first one will be returned here.

Returns: 

Error code.

\see igraph_compare_communities() with the IGRAPH_COMMCMP_SPLIT_JOIN method if you are not interested in the individual distances but only the sum of them. Time complexity: O(n log(n)).

2. Community structure based on statistical mechanics

2.1. igraph_community_spinglass — Community detection based on statistical mechanics

int igraph_community_spinglass(const igraph_t *graph,
			       const igraph_vector_t *weights,
			       igraph_real_t *modularity,
			       igraph_real_t *temperature,
			       igraph_vector_t *membership, 
			       igraph_vector_t *csize, 
			       igraph_integer_t spins,
			       igraph_bool_t parupdate,
			       igraph_real_t starttemp,
			       igraph_real_t stoptemp,
			       igraph_real_t coolfact,
			       igraph_spincomm_update_t update_rule,
			       igraph_real_t gamma,
			       /* the rest is for the NegSpin implementation */
			       igraph_spinglass_implementation_t implementation,
/* 			       igraph_matrix_t *adhesion, */
/* 			       igraph_matrix_t *normalised_adhesion, */
/* 			       igraph_real_t *polarization, */
			       igraph_real_t gamma_minus);

This function implements the community structure detection algorithm proposed by Joerg Reichardt and Stefan Bornholdt. The algorithm is described in their paper: Statistical Mechanics of Community Detection, http://arxiv.org/abs/cond-mat/0603718 .

From version 0.6 igraph also supports an extension to the algorithm that allows negative edge weights. This is described in V.A. Traag and Jeroen Bruggeman: Community detection in networks with positive and negative links, http://arxiv.org/abs/0811.2329 .

Arguments: 

graph:

The input graph, it may be directed but the direction of the edge is not used in the algorithm.

weights:

The vector giving the edge weights, it may be NULL, in which case all edges are weighted equally. Edge weights should be positive, altough this is not tested.

modularity:

Pointer to a real number, if not NULL then the modularity score of the solution will be stored here. This is the gereralized modularity that simplifies to the one defined in M. E. J. Newman and M. Girvan, Phys. Rev. E 69, 026113 (2004), if the gamma parameter is one.

temperature:

Pointer to a real number, if not NULL then the temperature at the end of the algorithm will be stored here.

membership:

Pointer to an initialized vector or NULL. If not NULL then the result of the clustering will be stored here, for each vertex the number of its cluster is given, the first cluster is numbered zero. The vector will be resized as needed.

csize:

Pointer to an initialized vector or NULL. If not NULL then the sizes of the clusters will stored here in cluster number order. The vector will be resized as needed.

spins:

Integer giving the number of spins, ie. the maximum number of clusters. Usually it is not a program to give a high number here, the default was 25 in the original code. Even if the number of spins is high the number of clusters in the result might small.

parupdate:

A logical constant, whether to update all spins in parallel. The default for this argument was FALSE (ie. 0) in the original code. It is not implemented in the IGRAPH_SPINCOMM_INP_NEG implementation.

starttemp:

Real number, the temperature at the start. The value of this argument was 1.0 in the original code.

stoptemp:

Real number, the algorithm stops at this temperature. The default was 0.01 in the original code.

coolfact:

Real number, the coolinf factor for the simulated annealing. The default was 0.99 in the original code.

update_rule:

The type of the update rule. Possible values: IGRAPH_SPINCOMM_UPDATE_SIMPLE and IGRAPH_SPINCOMM_UPDATE_CONFIG. Basically this parameter defined the null model based on which the actual clustering is done. If this is IGRAPH_SPINCOMM_UPDATE_SIMPLE then the random graph (ie. G(n,p)), if it is IGRAPH_SPINCOMM_UPDATE then the configuration model is used. The configuration means that the baseline for the clustering is a random graph with the same degree distribution as the input graph.

gamma:

Real number. The gamma parameter of the algorithm. This defined the weight of the missing and existing links in the quality function for the clustering. The default value in the original code was 1.0, which is equal weight to missing and existing edges. Smaller values make the existing links contibute more to the energy function which is minimized in the algorithm. Bigger values make the missing links more important. (If my understanding is correct.)

implementation:

Constant, chooses between the two implementations of the spin-glass algorithm that are included in igraph. IGRAPH_SPINCOMM_IMP_ORIG selects the original implementation, this is faster, IGRAPH_SPINCOMM_INP_NEG selects a new implementation by Vincent Traag that allows negative edge weights.

gamma_minus:

Real number. Parameter for the IGRAPH_SPINCOMM_IMP_NEG implementation. This specifies the balance between the importance of present and non-present negative weighted edges in a community. Smaller values of gamma_minus lead to communities with lesser negative intra-connectivity. If this argument is set to zero, the algorithm reduces to a graph coloring algorithm, using the number of spins as the number of colors.

Returns: 

Error code.

See also: 

igraph_community_spinglass_single() for calculating the community of a single vertex.

Time complexity: TODO.

Example 22.2.  File examples/simple/spinglass.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard st, Cambridge MA, 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

int main() {
  igraph_t g;
  igraph_real_t  modularity, temperature;
  igraph_vector_t membership, csize;
  /* long int i; */
  igraph_real_t cohesion, adhesion;
  igraph_integer_t inner_links;
  igraph_integer_t outer_links;
  
  igraph_small(&g, 5, IGRAPH_UNDIRECTED, 
	       0,1,0,2,0,3,0,4, 1,2,1,3,1,4, 2,3,2,4, 3,4,
	       5,6,5,7,5,8,5,9, 6,7,6,8,6,9, 7,8,7,9, 8,9, 0,5, -1);
  igraph_vector_init(&membership, 0);
  igraph_vector_init(&csize, 0);
  igraph_community_spinglass(&g, 
			     0, /* no weights */
			     &modularity,
			     &temperature,
			     &membership,
			     &csize,
			     2,	   /* no of spins */
			     0,    /* parallel update */
			     1.0,  /* start temperature */
			     0.01, /* stop temperature */
			     0.99, /* cooling factor */
			     IGRAPH_SPINCOMM_UPDATE_CONFIG,
			     1.0, /* gamma */
			     IGRAPH_SPINCOMM_IMP_ORIG,
			     /*gamma-=*/ 0);

/*   printf("Modularity:  %f\n", modularity); */
/*   printf("Temperature: %f\n", temperature); */
/*   printf("Cluster sizes: "); */
/*   for (i=0; i<igraph_vector_size(&csize); i++) { */
/*     printf("%li ", (long int)VECTOR(csize)[i]); */
/*   } */
/*   printf("\n"); */
/*   printf("Membership: "); */
/*   for (i=0; i<igraph_vector_size(&membership); i++) { */
/*     printf("%li ", (long int)VECTOR(membership)[i]); */
/*   } */
/*   printf("\n"); */

  if (igraph_vector_size(&csize) != 2) {
    igraph_vector_destroy(&membership);
    igraph_vector_destroy(&csize);  
    return 77;
  }
  if (VECTOR(csize)[0] != 5) {
    igraph_vector_destroy(&membership);
    igraph_vector_destroy(&csize);  
    return 77;
  }

  /* Try to call this as well, we don't check the results currently.... */
  
  igraph_community_spinglass_single(&g, 
				    /*weights=  */ 0,
				    /*vertex=   */ 0,
				    /*community=*/ &membership,
				    /*cohesion= */ &cohesion,
				    /*adhesion= */ &adhesion,
				    /*inner_links= */ &inner_links,
				    /*outer_links= */ &outer_links,
				    /*spins=       */ 2,
				    /*update_rule= */ IGRAPH_SPINCOMM_UPDATE_CONFIG,
				    /*gamma=       */ 1.0);
  
  igraph_destroy(&g);
  igraph_vector_destroy(&membership);
  igraph_vector_destroy(&csize);  
  
  return 0;
}


2.2. igraph_community_spinglass_single — Community of a single node based on statistical mechanics

int igraph_community_spinglass_single(const igraph_t *graph,
				      const igraph_vector_t *weights,
				      igraph_integer_t vertex,
				      igraph_vector_t *community,
				      igraph_real_t *cohesion,
				      igraph_real_t *adhesion,
				      igraph_integer_t *inner_links,
				      igraph_integer_t *outer_links,
				      igraph_integer_t spins,
				      igraph_spincomm_update_t update_rule,
				      igraph_real_t gamma);

This function implements the community structure detection algorithm proposed by Joerg Reichardt and Stefan Bornholdt. It is described in their paper: Statistical Mechanics of Community Detection, http://arxiv.org/abs/cond-mat/0603718 .

This function calculates the community of a single vertex without calculating all the communities in the graph.

Arguments: 

graph:

The input graph, it may be directed but the direction of the edges is not used in the algorithm.

weights:

Pointer to a vector with the weights of the edges. Alternatively NULL can be supplied to have the same weight for every edge.

vertex:

The vertex id of the vertex of which ths community is calculated.

community:

Pointer to an initialized vector, the result, the ids of the vertices in the community of the input vertex will be stored here. The vector will be resized as needed.

cohesion:

Pointer to a real variable, if not NULL the cohesion index of the community will be stored here.

adhesion:

Pointer to a real variable, if not NULL the adhesion index of the community will be stored here.

inner_links:

Pointer to an integer, if not NULL the number of edges within the community is stored here.

outer_links:

Pointer to an integer, if not NULL the number of edges between the community and the rest of the graph will be stored here.

spins:

The number of spins to use, this can be higher than the actual number of clusters in the network, in which case some clusters will contain zero vertices.

update_rule:

The type of the update rule. Possible values: IGRAPH_SPINCOMM_UPDATE_SIMPLE and IGRAPH_SPINCOMM_UPDATE_CONFIG. Basically this parameter defined the null model based on which the actual clustering is done. If this is IGRAPH_SPINCOMM_UPDATE_SIMPLE then the random graph (ie. G(n,p)), if it is IGRAPH_SPINCOMM_UPDATE then the configuration model is used. The configuration means that the baseline for the clustering is a random graph with the same degree distribution as the input graph.

gamma:

Real number. The gamma parameter of the algorithm. This defined the weight of the missing and existing links in the quality function for the clustering. The default value in the original code was 1.0, which is equal weight to missing and existing edges. Smaller values make the existing links contibute more to the energy function which is minimized in the algorithm. Bigger values make the missing links more important. (If my understanding is correct.)

Returns: 

Error code.

See also: 

igraph_community_spinglass() for the traditional version of the algorithm.

Time complexity: TODO.

3. Community structure based on eigenvectors of matrices

The function documented in these section implements the leading eigenvector method developed by Mark Newman and published in MEJ Newman: Finding community structure using the eigenvectors of matrices, Phys Rev E 74:036104 (2006).

The heart of the method is the definition of the modularity matrix, B, which is B=A-P, A being the adjacency matrix of the (undirected) network, and P contains the probability that certain edges are present according to the configuration model In other words, a Pij element of P is the probability that there is an edge between vertices i and j in a random network in which the degrees of all vertices are the same as in the input graph.

The leading eigenvector method works by calculating the eigenvector of the modularity matrix for the largest positive eigenvalue and then separating vertices into two community based on the sign of the corresponding element in the eigenvector. If all elements in the eigenvector are of the same sign that means that the network has no underlying community structure. Check Newman's paper to understand why this is a good method for detecting community structure.

The leading eigenvector community structure detection method is implemented in igraph_community_leading_eigenvector(). After the initial split, the following splits are done in a way to optimize modularity regarding to the original network.

Example 22.3.  File examples/simple/igraph_community_leading_eigenvector.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

int print_vector(const igraph_vector_t *v) {
  long int i, n=igraph_vector_size(v);
  for (i=0; i<n; i++) {
    printf("%.2g", (double)VECTOR(*v)[i]);
    if (i!=n-1) { printf(" "); }
  }
  printf("\n");
  return 0;
}

int print_matrix(const igraph_matrix_t *m) {
  long int i, j, nrow=igraph_matrix_nrow(m), ncol=igraph_matrix_ncol(m);
  for (i=0; i<nrow; i++) {
    for (j=0; j<ncol; j++) {
      printf("%.2g", (double)MATRIX(*m, i, j));
      if (j!=ncol-1) { printf(" "); }
    }
    printf("\n");
  }
  return 0;
}

int main() {

  igraph_t g;
  igraph_matrix_t merges;
  igraph_vector_t membership;
  igraph_vector_t x;
  igraph_arpack_options_t options;
  
  /* Zachary Karate club */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);  
 
  igraph_matrix_init(&merges, 0, 0);
  igraph_vector_init(&membership, 0);
  igraph_vector_init(&x, 0);
  igraph_arpack_options_init(&options);

  igraph_community_leading_eigenvector(&g, /*weights=*/ 0, &merges, 
				       &membership, 1, 
				       &options, /*modularity=*/ 0, 
				       /*start=*/ 0, /*eigenvalues=*/ 0, 
				       /*eigenvectors=*/ 0, /*history=*/ 0,
				       /*callback=*/ 0, 
				       /*callback_extra=*/ 0);

  print_matrix(&merges);
  print_vector(&membership);

  printf("\n");

  /* Make all the steps */
  igraph_community_leading_eigenvector(&g, /*weights=*/ 0, &merges, 
				       &membership, igraph_vcount(&g),
				       &options, /*modularity=*/ 0, 
				       /*start=*/ 0, /*eigenvalues=*/ 0,
				       /*eigenvectors=*/ 0, /*history=*/ 0,
				       /*callback=*/ 0, 
				       /*callback_extra=*/ 0);

  print_matrix(&merges);
  print_vector(&membership);

  igraph_vector_destroy(&x);
  igraph_vector_destroy(&membership);
  igraph_matrix_destroy(&merges);
  igraph_destroy(&g);
  
  return 0;
}


3.1. igraph_community_leading_eigenvector — Leading eigenvector community finding (proper version).

int igraph_community_leading_eigenvector(const igraph_t *graph,
        const igraph_vector_t *weights,
	igraph_matrix_t *merges,
	igraph_vector_t *membership,
	igraph_integer_t steps,
	igraph_arpack_options_t *options, 
	igraph_real_t *modularity,
	igraph_bool_t start,
	igraph_vector_t *eigenvalues,
	igraph_vector_ptr_t *eigenvectors,
	igraph_vector_t *history,
        igraph_community_leading_eigenvector_callback_t *callback,
        void *callback_extra);

Newman's leading eigenvector method for detecting community structure. This is the proper implementation of the recursive, divisive algorithm: each split is done by maximizing the modularity regarding the original network, see MEJ Newman: Finding community structure in networks using the eigenvectors of matrices, Phys Rev E 74:036104 (2006).

Arguments: 

graph:

The undirected input graph.

weights:

The weights of the edges, or a null pointer for unweighted graphs.

merges:

The result of the algorithm, a matrix containing the information about the splits performed. The matrix is built in the opposite way however, it is like the result of an agglomerative algorithm. If at the end of the algorithm (after steps steps was done) there are p communities, then these are numbered from zero to p-1. The first line of the matrix contains the first merge (which is in reality the last split) of two communities into community p, the merge in the second line forms community p+1, etc. The matrix should be initialized before calling and will be resized as needed. This argument is ignored of it is NULL.

membership:

The membership of the vertices after all the splits were performed will be stored here. The vector must be initialized before calling and will be resized as needed. This argument is ignored if it is NULL. This argument can also be used to supply a starting configuration for the community finding, in the format of a membership vector. In this case the start argument must be set to 1.

steps:

The maximum number of steps to perform. It might happen that some component (or the whole network) has no underlying community structure and no further steps can be done. If you want as many steps as possible then supply the number of vertices in the network here.

options:

The options for ARPACK. n is always overwritten. ncv is set to at least 4.

modularity:

If not a null pointer, then it must be a pointer to a real number and the modularity score of the final division is stored here.

start:

Boolean, whether to use the community structure given in the membership argument as a starting point.

eigenvalues:

Pointer to an initialized vector or a null pointer. If not a null pointer, then the eigenvalues calculated along the community structure detection are stored here. The non-positive eigenvalues, that do not result a split, are stored as well.

eigenvectors:

If not a null pointer, then the eigenvectors that are calculated in each step of the algorithm, are stored here, in a pointer vector. Each eigenvector is stored in an igraph_vector_t object. The user is responsible of deallocating the memory that belongs to the individual vectors, by calling first igraph_vector_destroy(), and then free() on them.

history:

Pointer to an initialized vector or a null pointer. If not a null pointer, then a trace of the algorithm is stored here, encoded numerically. The various operations:

IGRAPH_LEVC_HIST_START_FULL

Start the algorithm from an initial state where each connected component is a separate community.

IGRAPH_LEVC_HIST_START_GIVEN

Start the algorithm from a given community structure. The next value in the vector contains the initial number of communities.

IGRAPH_LEVC_HIST_SPLIT

Split a community into two communities. The id of the splitted community is given in the next element of the history vector. The id of the first new community is the same as the id of the splitted community. The id of the second community equals to the number of communities before the split.

IGRAPH_LEVC_HIST_FAILED

Tried to split a community, but it was not worth it, as it does not result in a bigger modularity value. The id of the community is given in the next element of the vector.

callback:

A null pointer or a function of type igraph_community_leading_eigenvector_callback_t. If given, this callback function is called after each eigenvector/eigenvalue calculation. If the callback returns a non-zero value, then the community finding algorithm stops. See the arguments passed to the callback at the documentation of igraph_community_leading_eigenvector_callback_t.

callback_extra:

Extra argument to pass to the callback function.

Returns: 

Error code.

See also: 

igraph_community_walktrap() and igraph_community_spinglass() for other community structure detection methods.

Time complexity: O(|E|+|V|^2*steps), |V| is the number of vertices, |E| the number of edges, steps the number of splits performed.

3.2. igraph_community_leading_eigenvector_callback_t — Callback for the leading eigenvector community finding method.

typedef int igraph_community_leading_eigenvector_callback_t(
	const igraph_vector_t *membership,
	long int comm, 
	igraph_real_t eigenvalue,
	const igraph_vector_t *eigenvector,
	igraph_arpack_function_t *arpack_multiplier,
        void *arpack_extra,
        void *extra);

The leading eigenvector community finding implementation in igraph is able to call a callback function, after each eigenvalue calculation. This callback function must be of igraph_community_leading_eigenvector_callback_t type. The following arguments are passed to the callback:

Arguments: 

membership:

The actual membership vector, before recording the potential change implied by the newly found eigenvalue.

comm:

The id of the community that the algorithm tried to split in the last iteration. The community ids are indexed from zero here!

eigenvalue:

The eigenvalue the algorithm has just found.

eigenvector:

The eigenvector corresponding to the eigenvalue the algorithm just found.

arpack_multiplier:

A function that was passed to igraph_arpack_rssolve() to solve the last eigenproblem.

arpack_extra:

The extra argument that was passed to the ARPACK solver.

extra:

Extra argument that as passed to igraph_community_leading_eigenvector().

See also: 

3.3. igraph_le_community_to_membership — Vertex membership from the leading eigenvector community structure

int igraph_le_community_to_membership(const igraph_matrix_t *merges,
				      igraph_integer_t steps,
				      igraph_vector_t *membership,
				      igraph_vector_t *csize);

This function creates a membership vector from the result of igraph_community_leading_eigenvector(), It takes membership and performs steps merges, according to the supplied merges matrix.

Arguments: 

merges:

The matrix defining the merges to make. This is usually from the output of the leading eigenvector community structure detection routines.

steps:

The number of steps to make according to merges.

membership:

Initially the starting membership vector, on output the resulting membership vector, after performing steps merges.

csize:

Optionally the sizes of the communities is stored here, if this is not a null pointer, but an initialized vector.

Returns: 

Error code.

Time complexity: O(|V|), the number of vertices.

4. Walktrap: community structure based on random walks

4.1. igraph_community_walktrap — This function is the implementation of the Walktrap community

int igraph_community_walktrap(const igraph_t *graph, 
			      const igraph_vector_t *weights,
			      int steps,
			      igraph_matrix_t *merges,
			      igraph_vector_t *modularity, 
			      igraph_vector_t *membership);

finding algorithm, see Pascal Pons, Matthieu Latapy: Computing communities in large networks using random walks, http://arxiv.org/abs/physics/0512106

Currently the original C++ implementation is used in igraph, see http://www-rp.lip6.fr/~latapy/PP/walktrap.html I'm grateful to Matthieu Latapy and Pascal Pons for providing this source code.

In contrast to the original implementation, isolated vertices are allowed in the graph and they are assumed to have a single incident loop edge with weight 1.

Arguments: 

graph:

The input graph, edge directions are ignored.

weights:

Numeric vector giving the weights of the edges. If it is a NULL pointer then all edges will have equal weights. The weights are expected to be positive.

steps:

Integer constant, the length of the random walks.

merges:

Pointer to a matrix, the merges performed by the algorithm will be stored here (if not NULL). Each merge is a row in a two-column matrix and contains the ids of the merged clusters. Clusters are numbered from zero and cluster numbers smaller than the number of nodes in the network belong to the individual vertices as singleton clusters. In each step a new cluster is created from two other clusters and its id will be one larger than the largest cluster id so far. This means that before the first merge we have n clusters (the number of vertices in the graph) numbered from zero to n-1. The first merge creates cluster n, the second cluster n+1, etc.

modularity:

Pointer to a vector. If not NULL then the modularity score of the current clustering is stored here after each merge operation.

membership:

Pointer to a vector. If not a NULL pointer, then the membership vector corresponding to the maximal modularity score is stored here. If it is not a NULL pointer, then neither modularity nor merges may be NULL.

Returns: 

Error code.

See also: 

Time complexity: O(|E||V|^2) in the worst case, O(|V|^2 log|V|) typically, |V| is the number of vertices, |E| is the number of edges.

Example 22.4.  File examples/simple/walktrap.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge MA, 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

int main() {
  igraph_t g;
  igraph_matrix_t merges;
  igraph_vector_t modularity;
  long int no_of_nodes;
  long int i;
  
  igraph_small(&g, 5, IGRAPH_UNDIRECTED, 
	       0,1,0,2,0,3,0,4, 1,2,1,3,1,4, 2,3,2,4, 3,4,
	       5,6,5,7,5,8,5,9, 6,7,6,8,6,9, 7,8,7,9, 8,9, 0,5, -1);
  igraph_vector_init(&modularity, 0);
  igraph_matrix_init(&merges, 0, 0);
  
  igraph_community_walktrap(&g, 0 /* no weights */,
			    4 /* steps */,
			    &merges, &modularity, 
			    /* membership=*/ 0);
  
  no_of_nodes=igraph_vcount(&g);
  printf("Merges:\n");
  for (i=0; i<igraph_matrix_nrow(&merges); i++) {
    printf("%2.1li + %2.li -> %2.li (modularity %4.2f)\n", 
	   (long int)MATRIX(merges, i, 0), 
	   (long int)MATRIX(merges, i, 1), 
	   no_of_nodes+i,
	   VECTOR(modularity)[i]);
  }
  
  igraph_destroy(&g);

  /* isolated vertices */
  igraph_small(&g, 5, IGRAPH_UNDIRECTED, -1);
  if (igraph_community_walktrap(&g, 0 /* no weights */, 4 /* steps */, &merges,
      &modularity, /* membership = */ 0)) {
    return 1;
  }
  if (igraph_vector_min(&modularity) != 0 || igraph_vector_max(&modularity) != 0) {
    return 2;
  }
  igraph_destroy(&g);

  igraph_matrix_destroy(&merges);
  igraph_vector_destroy(&modularity);
  return 0;
}


5. Edge betweenness based community detection

5.1. igraph_community_edge_betweenness — Community finding based on edge betweenness

int igraph_community_edge_betweenness(const igraph_t *graph, 
				      igraph_vector_t *result,
				      igraph_vector_t *edge_betweenness,
				      igraph_matrix_t *merges,
				      igraph_vector_t *bridges,
				      igraph_vector_t *modularity,
				      igraph_vector_t *membership,
				      igraph_bool_t directed,
				      const igraph_vector_t *weights);

Community structure detection based on the betweenness of the edges in the network. The algorithm was invented by M. Girvan and M. Newman, see: M. Girvan and M. E. J. Newman: Community structure in social and biological networks, Proc. Nat. Acad. Sci. USA 99, 7821-7826 (2002).

The idea is that the betweenness of the edges connecting two communities is typically high, as many of the shortest paths between nodes in separate communities go through them. So we gradually remove the edge with highest betweenness from the network, and recalculate edge betweenness after every removal. This way sooner or later the network falls off to two components, then after a while one of these components falls off to two smaller components, etc. until all edges are removed. This is a divisive hierarchical approach, the result is a dendrogram.

Arguments: 

graph:

The input graph.

result:

Pointer to an initialized vector, the result will be stored here, the ids of the removed edges in the order of their removal. It will be resized as needed. It may be NULL if the edge IDs are not needed by the caller.

edge_betweenness:

Pointer to an initialized vector or NULL. In the former case the edge betweenness of the removed edge is stored here. The vector will be resized as needed.

merges:

Pointer to an initialized matrix or NULL. If not NULL then merges performed by the algorithm are stored here. Even if this is a divisive algorithm, we can replay it backwards and note which two clusters were merged. Clusters are numbered from zero, see the merges argument of igraph_community_walktrap() for details. The matrix will be resized as needed.

bridges:

Pointer to an initialized vector of NULL. If not NULL then all edge removals which separated the network into more components are marked here.

modularity:

If not a null pointer, then the modularity values of the different divisions are stored here, in the order corresponding to the merge matrix. The modularity values will take weights into account if weights is not null.

membership:

If not a null pointer, then the membership vector, corresponding to the highest modularity value, is stored here.

directed:

Logical constant, whether to calculate directed betweenness (ie. directed paths) for directed graphs. It is ignored for undirected graphs.

weights:

An optional vector containing edge weights. If null, the unweighted edge betweenness scores will be calculated and used. If not null, the weighted edge betweenness scores will be calculated and used.

Returns: 

Error code.

See also: 

Time complexity: O(|V||E|^2), as the betweenness calculation requires O(|V||E|) and we do it |E|-1 times.

Example 22.5.  File examples/simple/igraph_community_edge_betweenness.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

int igraph_vector_between(const igraph_vector_t* v, const igraph_vector_t* lo,
		const igraph_vector_t* hi) {
	return igraph_vector_all_le(lo, v) && igraph_vector_all_ge(hi, v);
}

void test_unweighted() {
  igraph_t g;
  igraph_vector_t edges, eb;
  long int i;
  long int no_of_edges;

  /* Zachary Karate club */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);  
  
  igraph_vector_init(&edges, 0);
  igraph_vector_init(&eb, 0);
  igraph_community_edge_betweenness(&g, &edges, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    /*weights=*/ 0);
  
  no_of_edges=igraph_ecount(&g);
  for (i=0; i<no_of_edges; i++) {
    printf("%li ", (long int)VECTOR(edges)[i]);
  }
  printf("\n");
  
  for (i=0; i<no_of_edges; i++) {
    printf("%.2f ", VECTOR(eb)[i]);
  }
  printf("\n");

  /* Try it once again without storage space for edges */
  igraph_community_edge_betweenness(&g, 0, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    /*weights=*/ 0);
  for (i=0; i<no_of_edges; i++) {
    printf("%.2f ", VECTOR(eb)[i]);
  }
  printf("\n");

  igraph_vector_destroy(&eb);
  igraph_vector_destroy(&edges);
  igraph_destroy(&g);
}

#define EPS 1e-4

void test_weighted() {
  igraph_t g;
  igraph_vector_t edges, eb, weights;
  igraph_real_t weights_array[] = { 4, 1, 3, 2, 5, 8, 6, 7 };

  igraph_real_t edges_array1[] = { 2, 3, 0, 1, 4, 7, 5, 6 };
  igraph_real_t edges_array2[] = { 2, 3, 6, 5, 0, 1, 4, 7 };
  igraph_real_t eb_array1_lo[] = { 4, 5, 3+1/3.0-EPS, 4, 2.5, 4, 1, 1 };
  igraph_real_t eb_array1_hi[] = { 4, 5, 3+1/3.0+EPS, 4, 2.5, 4, 1, 1 };
  igraph_real_t eb_array2_lo[] = { 4, 5, 3+1/3.0-EPS, 6, 1.5, 2, 1, 1 };
  igraph_real_t eb_array2_hi[] = { 4, 5, 3+1/3.0+EPS, 6, 1.5, 2, 1, 1 };

  igraph_vector_t edges_sol1, edges_sol2, eb_sol1_lo, eb_sol1_hi, eb_sol2_lo, eb_sol2_hi;

  igraph_vector_view(&edges_sol1, edges_array1, 
		     sizeof(edges_array1)/sizeof(double));
  igraph_vector_view(&edges_sol2, edges_array2, 
		     sizeof(edges_array2)/sizeof(double));
  igraph_vector_view(&eb_sol1_lo, eb_array1_lo, sizeof(eb_array1_lo)/sizeof(double));
  igraph_vector_view(&eb_sol2_lo, eb_array2_lo, sizeof(eb_array2_lo)/sizeof(double));
  igraph_vector_view(&eb_sol1_hi, eb_array1_hi, sizeof(eb_array1_hi)/sizeof(double));
  igraph_vector_view(&eb_sol2_hi, eb_array2_hi, sizeof(eb_array2_hi)/sizeof(double));

  /* Small graph as follows: A--B--C--A, A--D--E--A, B--D, C--E */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
      0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 1, 3, 2, 4, 3, 4, -1);
  igraph_vector_view(&weights, weights_array, igraph_ecount(&g));

  igraph_vector_init(&edges, 0);
  igraph_vector_init(&eb, 0);
  igraph_community_edge_betweenness(&g, &edges, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    &weights);
  
  if (!igraph_vector_all_e(&edges_sol1, &edges) && 
      !igraph_vector_all_e(&edges_sol2, &edges)) {
    printf("Error, edges vector was: \n");
	igraph_vector_print(&edges);
    exit(2);
  }
  if (!igraph_vector_between(&eb, &eb_sol1_lo, &eb_sol1_hi) &&
      !igraph_vector_between(&eb, &eb_sol2_lo, &eb_sol2_hi)) {
    printf("Error, eb vector was: \n");
	igraph_vector_print(&eb);
    exit(2);
  }

  /* Try it once again without storage space for edges */
  igraph_community_edge_betweenness(&g, 0, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    &weights);

  if (!igraph_vector_between(&eb, &eb_sol1_lo, &eb_sol1_hi) &&
      !igraph_vector_between(&eb, &eb_sol2_lo, &eb_sol2_hi)) {
    printf("Error, eb vector was: \n");
	igraph_vector_print(&eb);
    exit(2);
  }

  igraph_vector_destroy(&eb);
  igraph_vector_destroy(&edges);
  igraph_destroy(&g);
}

int main() {
  test_unweighted();
  test_weighted();
  return 0;
}


5.2. igraph_community_eb_get_merges — Calculating the merges, ie. the dendrogram for an edge betweenness community structure

int igraph_community_eb_get_merges(const igraph_t *graph, 
				   const igraph_vector_t *edges,
                                   const igraph_vector_t *weights,
				   igraph_matrix_t *res,
				   igraph_vector_t *bridges, 
				   igraph_vector_t *modularity, 
				   igraph_vector_t *membership);

This function is handy if you have a sequence of edge which are gradually removed from the network and you would like to know how the network falls apart into separate components. The edge sequence may come from the igraph_community_edge_betweenness() function, but this is not necessary. Note that igraph_community_edge_betweenness can also calculate the dendrogram, via its merges argument.

Arguments: 

graph:

The input graph.

edges:

Vector containing the edges to be removed from the network, all edges are expected to appear exactly once in the vector.

weights:

An optional vector containing edge weights. If null, the unweighted modularity scores will be calculated. If not null, the weighted modularity scores will be calculated. Ignored if both modularity and membership are nulls.

res:

Pointer to an initialized matrix, if not NULL then the dendrogram will be stored here, in the same form as for the igraph_community_walktrap() function: the matrix has two columns and each line is a merge given by the ids of the merged components. The component ids are number from zero and component ids smaller than the number of vertices in the graph belong to individual vertices. The non-trivial components containing at least two vertices are numbered from n, n is the number of vertices in the graph. So if the first line contains a and b that means that components a and b are merged into component n, the second line creates component n+1, etc. The matrix will be resized as needed.

bridges:

Pointer to an initialized vector or NULL. If not null then the index of the edge removals which split the network will be stored here. The vector will be resized as needed.

modularity:

If not a null pointer, then the modularity values for the different divisions, corresponding to the merges matrix, will be stored here.

membership:

If not a null pointer, then the membership vector for the best division (in terms of modularity) will be stored here.

Returns: 

Error code.

See also: 

Time complexity: O(|E|+|V|log|V|), |V| is the number of vertices, |E| is the number of edges.

6. Community structure based on the optimization of modularity

6.1. igraph_community_fastgreedy — Finding community structure by greedy optimization of modularity

int igraph_community_fastgreedy(const igraph_t *graph,
				const igraph_vector_t *weights,
				igraph_matrix_t *merges, 
				igraph_vector_t *modularity, 
				igraph_vector_t *membership);

This function implements the fast greedy modularity optimization algorithm for finding community structure, see A Clauset, MEJ Newman, C Moore: Finding community structure in very large networks, http://www.arxiv.org/abs/cond-mat/0408187 for the details.

Some improvements proposed in K Wakita, T Tsurumi: Finding community structure in mega-scale social networks, http://www.arxiv.org/abs/cs.CY/0702048v1 have also been implemented.

Arguments: 

graph:

The input graph. It must be a graph without multiple edges. This is checked and an error message is given for graphs with multiple edges.

weights:

Potentially a numeric vector containing edge weights. Supply a null pointer here for unweighted graphs. The weights are expected to be non-negative.

merges:

Pointer to an initialized matrix or NULL, the result of the computation is stored here. The matrix has two columns and each merge corresponds to one merge, the ids of the two merged components are stored. The component ids are numbered from zero and the first n components are the individual vertices, n is the number of vertices in the graph. Component n is created in the first merge, component n+1 in the second merge, etc. The matrix will be resized as needed. If this argument is NULL then it is ignored completely.

modularity:

Pointer to an initialized vector or NULL pointer, in the former case the modularity scores along the stages of the computation are recorded here. The vector will be resized as needed.

membership:

Pointer to a vector. If not a null pointer, then the membership vector corresponding to the best split (in terms of modularity) is stored here.

Returns: 

Error code.

See also: 

igraph_community_walktrap(), igraph_community_edge_betweenness() for other community detection algorithms, igraph_community_to_membership() to convert the dendrogram to a membership vector.

Time complexity: O(|E||V|log|V|) in the worst case, O(|E|+|V|log^2|V|) typically, |V| is the number of vertices, |E| is the number of edges.

Example 22.6.  File examples/simple/igraph_community_fastgreedy.c

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

void show_results(igraph_t *g, igraph_vector_t *mod, igraph_matrix_t *merges, FILE* f) {
  long int i;
  igraph_vector_t membership;

  igraph_vector_init(&membership, 0);

  i=igraph_vector_which_max(mod);
  fprintf(f, "Modularity:  %f\n", VECTOR(*mod)[i]);
  igraph_community_to_membership(merges, igraph_vcount(g), i, &membership, 0);
  printf("Membership: ");
  for (i=0; i<igraph_vector_size(&membership); i++) {
    printf("%li ", (long int)VECTOR(membership)[i]);
  }
  printf("\n");

  igraph_vector_destroy(&membership);
}

int main() {
  igraph_t g;
  igraph_vector_t modularity, weights;
  igraph_matrix_t merges;

  igraph_vector_init(&modularity,0);
  igraph_matrix_init(&merges,0,0);
  igraph_vector_init(&weights,0);

  /* Simple unweighted graph */
  igraph_small(&g, 10, IGRAPH_UNDIRECTED, 
	       0,1,0,2,0,3,0,4, 1,2,1,3,1,4, 2,3,2,4, 3,4,
	       5,6,5,7,5,8,5,9, 6,7,6,8,6,9, 7,8,7,9, 8,9,
           0,5, -1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 

  /* Same simple graph, with uniform edge weights */
  igraph_vector_resize(&weights, igraph_ecount(&g));
  igraph_vector_fill(&weights, 2);
  igraph_community_fastgreedy(&g, &weights, &merges, &modularity, 
			      /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 
  igraph_destroy(&g);

  /* Simple nonuniform weighted graph, with and without weights */
  igraph_small(&g, 6, IGRAPH_UNDIRECTED, 
	       0,1,1,2,2,3,2,4,2,5,3,4,3,5,4,5, -1);
  igraph_vector_resize(&weights, 8);
  igraph_vector_fill(&weights, 1);
  VECTOR(weights)[0] = 10; VECTOR(weights)[1] = 10;
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 
  igraph_community_fastgreedy(&g, &weights, &merges, &modularity, 
			      /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 
  igraph_destroy(&g);

  /* Zachary Karate club */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, 
			      /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 
  igraph_destroy(&g);
   
  /* Simple disconnected graph with isolates */
  igraph_small(&g, 9, IGRAPH_UNDIRECTED,
	       0,  1,  0,  2,  0,  3,  1,  2,  1,  3,  2,  3,
	       4,  5,  4,  6,  4,  7,  5,  6,  5,  7,  6,  7,
	       -1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout); 
  igraph_destroy(&g);

  /* Disjoint union of two rings */
  igraph_small(&g, 20, IGRAPH_UNDIRECTED,
           0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,0,9,
	   10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,10,19,-1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout);
  igraph_destroy(&g);

  /* Completely empty graph */
  igraph_small(&g, 10, IGRAPH_UNDIRECTED, -1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout);
  igraph_destroy(&g);

  /* Ring graph with loop edges */
  igraph_small(&g, 6, IGRAPH_UNDIRECTED,
          0,1,1,2,2,3,3,4,4,5,5,0,0,0,2,2,-1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout);
  igraph_destroy(&g);

  /* Regression test -- graph with two vertices and two edges */
  igraph_small(&g, 2, IGRAPH_UNDIRECTED, 0,0,1,1,-1);
  igraph_community_fastgreedy(&g, 0, &merges, &modularity, /*membership=*/ 0);
  show_results(&g, &modularity, &merges, stdout);
  igraph_destroy(&g);

  igraph_vector_destroy(&modularity);
  igraph_vector_destroy(&weights);
  igraph_matrix_destroy(&merges);

  return 0;
}


6.2. igraph_community_multilevel — Finding community structure by multi-level optimization of modularity

int igraph_community_multilevel(const igraph_t *graph,
  const igraph_vector_t *weights, igraph_vector_t *membership,
  igraph_matrix_t *memberships, igraph_vector_t *modularity);

This function implements the multi-level modularity optimization algorithm for finding community structure, see VD Blondel, J-L Guillaume, R Lambiotte and E Lefebvre: Fast unfolding of community hierarchies in large networks, J Stat Mech P10008 (2008) for the details (preprint: http://arxiv.org/abs/arXiv:0803.0476). It is based on the modularity measure and a hierarchical approach. Initially, each vertex is assigned to a community on its own. In every step, vertices are re-assigned to communities in a local, greedy way: each vertex is moved to the community with which it achieves the highest contribution to modularity. When no vertices can be reassigned, each community is considered a vertex on its own, and the process starts again with the merged communities. The process stops when there is only a single vertex left or when the modularity cannot be increased any more in a step. This function was contributed by Tom Gregorovic.

Arguments: 

graph:

The input graph. It must be an undirected graph.

weights:

Numeric vector containing edge weights. If NULL, every edge has equal weight. The weights are expected to be non-negative.

membership:

The membership vector, the result is returned here. For each vertex it gives the ID of its community. The vector must be initialized and it will be resized accordingly.

memberships:

Numeric matrix that will contain the membership vector after each level, if not NULL. It must be initialized and it will be resized accordingly.

modularity:

Numeric vector that will contain the modularity score after each level, if not NULL. It must be initialized and it will be resized accordingly.

Returns: 

Error code.

Time complexity: in average near linear on sparse graphs.

Example 22.7.  File examples/simple/igraph_community_multilevel.c

/* -*- mode: C -*-  */
/* vim:set ts=2 sts=2 sw=2 et: */
/* 
   IGraph library.
   Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

void show_results(igraph_t *g, igraph_vector_t *membership, igraph_matrix_t *memberships, igraph_vector_t *modularity, FILE* f) {
  long int i, j, no_of_nodes = igraph_vcount(g);

  j=igraph_vector_which_max(modularity);
  for (i=0; i<igraph_vector_size(membership); i++) {
    if (VECTOR(*membership)[i] != MATRIX(*memberships, j, i)) {
      fprintf(f, "WARNING: best membership vector element %li does not match the best one in the membership matrix\n", i);
    }
  }

  fprintf(f, "Modularities:\n");
  igraph_vector_print(modularity);

  for (i=0; i < igraph_matrix_nrow(memberships); i++) {
    for (j=0; j < no_of_nodes; j++) {
      fprintf(f, "%ld ", (long int)MATRIX(*memberships, i, j));
    }
    fprintf(f, "\n");
  }

  fprintf(f, "\n");
}

int main() {
  igraph_t g;
  igraph_vector_t modularity, membership, edges;
  igraph_matrix_t memberships;
  int i, j, k;

  igraph_vector_init(&modularity,0);
  igraph_vector_init(&membership,0);
  igraph_matrix_init(&memberships,0,0);

  /* Unweighted test graph from the paper of Blondel et al */
  igraph_small(&g, 16, IGRAPH_UNDIRECTED,
      0, 2, 0, 3, 0, 4, 0, 5,
      1, 2, 1, 4, 1, 7,
      2, 4, 2, 5, 2, 6,
      3, 7,
      4, 10,
      5, 7, 5, 11,
      6, 7, 6, 11,
      8, 9, 8, 10, 8, 11, 8, 14, 8, 15,
      9, 12, 9, 14,
      10, 11, 10, 12, 10, 13, 10, 14,
      11, 13,
      -1);
  igraph_community_multilevel(&g, 0, &membership, &memberships, &modularity);
  show_results(&g, &membership, &memberships, &modularity, stdout);
  igraph_destroy(&g);

  /* Ring of 30 cliques */
  igraph_vector_init(&edges,0);
  for (i = 0; i < 30; i++) {
    for (j = 0; j < 5; j++) {
      for (k = j+1; k < 5; k++) {
        igraph_vector_push_back(&edges, i*5+j);
        igraph_vector_push_back(&edges, i*5+k);
      }
    }
  }
  for (i = 0; i < 30; i++) {
    igraph_vector_push_back(&edges, i*5 % 150);
    igraph_vector_push_back(&edges, (i*5+6) % 150);
  }
  igraph_create(&g, &edges, 150, 0);
  igraph_community_multilevel(&g, 0, &membership, &memberships, &modularity);
  show_results(&g, &membership, &memberships, &modularity, stdout);
  igraph_destroy(&g);

  igraph_vector_destroy(&modularity);
  igraph_vector_destroy(&membership);
  igraph_vector_destroy(&edges);
  igraph_matrix_destroy(&memberships);

#ifdef __APPLE__
  return 0;
#else
  return 77;
#endif
}


7. Label propagation

7.1. igraph_community_label_propagation — Community detection based on label propagation

int igraph_community_label_propagation(const igraph_t *graph,
                                       igraph_vector_t *membership,
                                       const igraph_vector_t *weights,
                                       const igraph_vector_t *initial,
                                       igraph_vector_bool_t *fixed, 
				       igraph_real_t *modularity);

This function implements the community detection method described in: Raghavan, U.N. and Albert, R. and Kumara, S.: Near linear time algorithm to detect community structures in large-scale networks. Phys Rev E 76, 036106. (2007). This version extends the original method by the ability to take edge weights into consideration and also by allowing some labels to be fixed.

Weights are taken into account as follows: when the new label of node i is determined, the algorithm iterates over all edges incident on node i and calculate the total weight of edges leading to other nodes with label 0, 1, 2, ..., k-1 (where k is the number of possible labels). The new label of node i will then be the label whose edges (among the ones incident on node i) have the highest total weight.

Arguments: 

graph:

The input graph, should be undirected to make sense.

membership:

The membership vector, the result is returned here. For each vertex it gives the ID of its community (label).

weights:

The weight vector, it should contain a positive weight for all the edges.

initial:

The initial state. If NULL, every vertex will have a different label at the beginning. Otherwise it must be a vector with an entry for each vertex. Non-negative values denote different labels, negative entries denote vertices without labels.

fixed:

Boolean vector denoting which labels are fixed. Of course this makes sense only if you provided an initial state, otherwise this element will be ignored. Also note that vertices without labels cannot be fixed.

modularity:

If not a null pointer, then it must be a pointer to a real number. The modularity score of the detected community structure is stored here.

Returns: 

Error code.

Time complexity: O(m+n)

Example 22.8.  File examples/simple/igraph_community_label_propagation.c

/* -*- mode: C -*-  */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>

int main() {
  igraph_t g;
  igraph_vector_t membership, weights, initial;
  igraph_vector_bool_t fixed;
  long int i;

  /* Zachary Karate club -- this is just a quick smoke test */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
               0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
               0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
               0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
               0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
               1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
               2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
               2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
               4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
               6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
               13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
               18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
               22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
               23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
               25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
               28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
               31, 32, 31, 33, 32, 33,
               -1);  
  
  igraph_vector_init(&membership, 0);
  igraph_community_label_propagation(&g, &membership, 0, 0, 0, 
				     /*modularity=*/ 0);
  if (igraph_vector_max(&membership) > 3) {
    printf("Resulting graph had more than four clusters:\n");
    for (i=0; i<igraph_vcount(&g); i++)
      printf("%li ", (long)VECTOR(membership)[i]);
    printf("\n");
    return 1;
  }

  igraph_destroy(&g);
  
  /* Simple star graph to test weights */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
               0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
               2,  3,  2,  4,  3,  4,  3,  5,  4,  5,  -1);
  igraph_vector_init_int_end(&weights, -1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1);
  igraph_vector_init_int_end(&initial, -1, 0, 0, 1, 1, 1, 1, -1);
  igraph_vector_bool_init(&fixed, 6);
  VECTOR(fixed)[3] = 1;
  VECTOR(fixed)[4] = 1;
  VECTOR(fixed)[5] = 1;
  igraph_community_label_propagation(&g, &membership, &weights,
				     &initial, &fixed, /*modularity=*/ 0);
  for (i=0; i<igraph_vcount(&g); i++)
    if (VECTOR(membership)[i] != (i < 2 ? 0 : 1)) return 3;
  igraph_community_label_propagation(&g, &membership, 0,
				     &initial, &fixed, /*modularity=*/ 0);
  for (i=0; i<igraph_vcount(&g); i++)
    if (VECTOR(membership)[i] != 0) return 4;

  /* Check whether it works with no fixed vertices at all
   * while an initial configuration is given -- see bug
   * #570902 in Launchpad. This is a simple smoke test only. */
  igraph_community_label_propagation(&g, &membership, &weights,
				     &initial, 0, /*modularity=*/ 0);

  igraph_vector_bool_destroy(&fixed);
  igraph_vector_destroy(&weights);
  igraph_vector_destroy(&initial);
  igraph_destroy(&g);

  igraph_vector_destroy(&membership);

  return 0;
}


8. The InfoMAP algorithm

8.1. igraph_community_infomap — Find community structure that minimizes the expected

int igraph_community_infomap(const igraph_t * graph,
                             const igraph_vector_t *e_weights,
                             const igraph_vector_t *v_weights,
                             int nb_trials,
                             igraph_vector_t *membership,
                             igraph_real_t *codelength);

description length of a random walker trajectory. Implementation of the InfoMap community detection algorithm.of Martin Rosvall and Carl T. Bergstrom. See : Visualization of the math and the map generator: www.mapequation.org [2] The original paper: M. Rosvall and C. T. Bergstrom, Maps of information flow reveal community structure in complex networks, PNAS 105, 1118 (2008) [http://dx.doi.org/10.1073/pnas.0706851105 , http://arxiv.org/abs/0707.0609 ] [3] A more detailed paper: M. Rosvall, D. Axelsson, and C. T. Bergstrom, The map equation, Eur. Phys. J. Special Topics 178, 13 (2009). [http://dx.doi.org/10.1140/epjst/e2010-01179-1 , http://arxiv.org/abs/0906.1405 ]

The original C++ implementation of Martin Rosvall is used, see http://www.tp.umu.se/~rosvall/downloads/infomap_undir.tgz . Intergation in igraph has be done by Emmanuel Navarro (who is grateful to * Martin Rosvall and Carl T. Bergstrom for providing this source code.)

Note that the graph must not contain isolated vertices.

If you want to specify a random seed (as in original implementation) you can use igraph_rng_seed().

Arguments: 

graph:

The input graph.

e_weights:

Numeric vector giving the weights of the edges. If it is a NULL pointer then all edges will have equal weights. The weights are expected to be positive.

v_weights:

Numeric vector giving the weights of the vertices. If it is a NULL pointer then all vertices will have equal weights. The weights are expected to be positive.

nb_trials:

The number of attempts to partition the network (can be any integer value equal or larger than 1).

membership:

Pointer to a vector. The membership vector is stored here.

codelength:

Pointer to a real. If not NULL the code length of the partition is stored here.

Returns: 

Error code.

See also: 

Time complexity: TODO.