For using the igraph C library
igraph_modularity
— Calculate the modularity of a graph with respect to some vertex typesigraph_community_optimal_modularity
— Calculate the community structure with the highest modularity valueigraph_community_to_membership
— Create membership vector from community structure dendrogramigraph_reindex_membership
— Makes the IDs in a membership vector continuousigraph_compare_communities
— Compares community structures using various metricsigraph_split_join_distance
— Calculates the splitjoin distance of two community structures
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:

The input graph. 

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. 

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

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.
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):172188, 2008.
Note that modularity optimization is an NPcomplete 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:

The input graph. It is always treated as undirected. 

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

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. 

Vector giving the weights of the edges. If it is

Returns:
Error code. 
See also:

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) 20102012 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 021101301 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; }
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 n1
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:

The twocolumn matrix containing the merge
operations. See 

The number of leaf nodes in the dendrogram 

Integer constant, the number of steps to take. 

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

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.
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 C1, where C is the number of unique component IDs in the original vector. This function was contributed by Tom Gregorovic.
Arguments:

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

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.
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 splitjoin 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: 9783540407201.
Danon L, DiazGuilera 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 INSR0012, 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):846850, 1971.
Hubert L and Arabie P: Comparing partitions. Journal of Classification 2:193218, 1985.
Arguments:

the membership vector of the first community structure 

the membership vector of the second community structure 

the result is stored here. 

the comparison method to use. 
Returns:
Error code. 
Time complexity: O(n log(n)).
int igraph_split_join_distance(const igraph_vector_t *comm1, const igraph_vector_t *comm2, igraph_integer_t *distance12, igraph_integer_t *distance21);
The splitjoin 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 splitjoin 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 INSR0012, National Research Institute for Mathematics and Computer Science in the Netherlands, Amsterdam, May 2000.
Arguments:

the membership vector of the first community structure 

the membership vector of the second community structure 

pointer to an 

pointer to an 
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)).
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/condmat/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:

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

The vector giving the edge weights, it may be 

Pointer to a real number, if not 

Pointer to a real number, if not 

Pointer to an initialized vector or 

Pointer to an initialized vector or 

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. 

A logical constant, whether to update all spins in
parallel. The default for this argument was 

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

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

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

The type of the update rule. Possible values: 

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

Constant, chooses between the two
implementations of the spinglass algorithm that are included
in igraph. 

Real number. Parameter for the 
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) 20062012 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 021101301 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; }
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/condmat/0603718 .
This function calculates the community of a single vertex without calculating all the communities in the graph.
Arguments:

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

Pointer to a vector with the weights of the edges.
Alternatively 

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

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. 

Pointer to a real variable, if not 

Pointer to a real variable, if not 

Pointer to an integer, if not 

Pointer to an integer, if not 

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. 

The type of the update rule. Possible values: 

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.
igraph_community_leading_eigenvector
— Leading eigenvector community finding (proper version).igraph_community_leading_eigenvector_callback_t
— Callback for the leading eigenvector community finding method.igraph_le_community_to_membership
— Vertex membership from the leading eigenvector community structureThe 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=AP, 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) 20072012 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 021101301 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!=n1) { 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!=ncol1) { 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; }
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:

The undirected input graph. 


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


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



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 


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. 


The options for ARPACK. 


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. 


Boolean, whether to use the community structure given
in the 


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 nonpositive eigenvalues, that do not result a split, are stored as well. 


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



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:



A null pointer or a function of type 


Extra argument to pass to the callback function. 
Returns:
Error code. 
See also:

Time complexity: O(E+V^2*steps), V is the number of vertices, E the number of edges, “steps” the number of splits performed.
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:

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

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

The eigenvalue the algorithm has just found. 

The eigenvector corresponding to the eigenvalue the algorithm just found. 

A function that was passed to 

The extra argument that was passed to the ARPACK solver. 

Extra argument that as passed to 
See also:
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:

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

The number of steps to make according to 

Initially the starting membership vector,
on output the resulting membership vector, after performing 

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.
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://wwwrp.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:

The input graph, edge directions are ignored. 

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. 

Integer constant, the length of the random walks. 

Pointer to a matrix, the merges performed by the
algorithm will be stored here (if not NULL). Each merge is a
row in a twocolumn 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 

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

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

Returns:
Error code. 
See also:
Time complexity: O(EV^2) in the worst case, O(V^2 logV) 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) 20072012 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 021101301 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; }
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, 78217826 (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:

The input graph. 

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. 

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. 

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 

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

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 

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

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

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(VE^2), as the betweenness calculation requires O(VE) and we do it E1 times.
Example 22.5. File examples/simple/igraph_community_edge_betweenness.c
/* * mode: C * */ /* IGraph library. Copyright (C) 20072012 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 021101301 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 1e4 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.0EPS, 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.0EPS, 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: ABCA, ADEA, BD, CE */ 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; }
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:

The input graph. 

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

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


Pointer to an initialized matrix, if not NULL then the
dendrogram will be stored here, in the same form as for the 

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. 

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

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+VlogV), V is the number of vertices, E is the number of edges.
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/condmat/0408187 for the details.
Some improvements proposed in K Wakita, T Tsurumi: Finding community structure in megascale social networks, http://www.arxiv.org/abs/cs.CY/0702048v1 have also been implemented.
Arguments:

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. 

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

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 

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. 

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:

Time complexity: O(EVlogV) in the worst case, O(E+Vlog^2V) 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) 20062012 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 021101301 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; }
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 multilevel modularity optimization algorithm for finding community structure, see VD Blondel, JL 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 reassigned 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:

The input graph. It must be an undirected graph. 

Numeric vector containing edge weights. If 

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. 

Numeric matrix that will contain the membership
vector after each level, if not 

Numeric vector that will contain the modularity score
after each level, if not 
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) 20062012 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 021101301 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 }
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 largescale 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, ..., k1 (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:

The input graph, should be undirected to make sense. 

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

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

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. Nonnegative values denote different labels, negative entries denote vertices without labels. 

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. 

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) 20072012 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 021101301 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; }
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/e2010011791 , 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:

The input graph. 

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. 

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. 

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

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

Pointer to a real. If not NULL the code length of the partition is stored here. 
Returns:
Error code. 
See also:
Time complexity: TODO.
← Chapter 21. Vertex separators  Chapter 23. Graphlets → 