Modularity calculation for weighted graphs in igraph

I used the fastgreedy algorithm in igraph for my community detection in a weighted, undirected graph. Afterwards I wanted to have a look at the modularity and I got different values for different methods and I am wondering why. I included a short example, which demonstrates my problem:

library(igraph) d<-matrix(c(1, 0.2, 0.3, 0.9, 0.9, 0.2, 1, 0.6, 0.4, 0.5, 0.3, 0.6, 1, 0.1, 0.8, 0.9, 0.4, 0.1, 1, 0.5, 0.9, 0.5, 0.8, 0.5, 1), byrow=T, nrow=5) g<-graph.adjacency(d, weighted=T, mode="lower",diag=FALSE, add.colnames=NA) fc<-fastgreedy.community(g) fc$modularity[3] #[1] -0.05011095 modularity(g,membership=cutat(fc,steps=2),weights=get.adjacency(g,attr="weight")) #[1] 0.07193047

I would expect both of the values to be identical and if I try the same with an unweighted graph, I get the same values.

d2<-round(d,digits=0) g2<- graph.adjacency(d2, weighted=NULL, mode="lower",diag=FALSE, add.colnames=NA) fc2<-fastgreedy.community(g2) plot(fc2,g2) fc2$modularity[3] #[1] 0.15625 modularity(g2,membership=cutat(fc2,steps=2)) #[1] 0.15625

Another user had a similar problem, but I have the current version of igraph, so that should not be the problem. Can someone explain to me why there is a difference or is there a problem with my code I don't see?


The line


is wrong. If you want to pass the weights of edges to modularity(), do it with E(g)$weight:

modularity(g, membership = cutat(fc, steps = 2), weights = E(g)$weight) # [1] -0.05011095


  • Hadoop Maven Dependency Error
  • Order of columns and rows with ggplot2 tile
  • Bellman-Ford Distance Vector Algorithm With Arbitrarily Many Nodes
  • R: Find the Variance of all Non-Zero Elements in Each Row
  • head and tail doesn't take negative number as argument for data.table?
  • Plotly - Create Pie Chart with continuous color scale
  • Can't get JSON data from Rails API
  • Non alpha character arrowlabels on a diagram
  • Reduce size of self-loops in igraph
  • How to do row-wise subtraction and replace a specific number with zero?
  • Spark dataframe to nested map
  • Convert adjacency matrix to a csv file
  • Insert Pandas dataframe into Cassandra Table
  • c# linear regression given 2 sets of data
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • Transpose table then set and rename index
  • iOS - MKOverlayView custom view rect fills works, but line draws do not
  • css calendar - td background diagonal split - two colors
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • How to change the default configuration files used in bootstrapping of reactJs through npm
  • Not able to display correct data in table -AngularJS
  • Selectively hide background elements when overlayed with transparent div
  • Cannot find “Grammar.txt” in python-sphinx
  • Dynamically load css stylesheet and wait for it to load
  • Calculating ratio of reciprocated ties for each node in igraph
  • quiver not drawing arrows just lots of blue, matlab
  • configure: error: no acceptable C compiler found in $PATH
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Mysterious problem with floating point in LISP - time axis generation
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How do I use the BLAS library provided by MATLAB?
  • Why winpcap requires both .lib and .dll to run?
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal