Question
Asked 25th Aug, 2015

Can anyone provide a short example of how the modularity is being calculated in networks?

I have read the documents available on google and also gone through the Wikipedia's definition and formula about calculating the modularity for a particular network. I am using r studio for implementation.For example if my network is like following.Then the value of modularity being calculated in r studio as an output for this network is 0.08. How the answer 0.08 is being achieved that i am not getting.Can any one explain the mathematical formulation of modularity for networks.How the formula that is given in Wikipedia is being applied in this case that i am not getting. I hope my question make sense. Thanks in advance.
--R Script--
library(igraph)
g <- graph_from_literal( 1--2, 4--5, 3--1, 5--2, 4--3 )
g
membership <- c(1,2,1,2,1)
modularity(g, membership)

Most recent answer

Mohammad R. Radi
University Of Kufa
Thanks a lot Matthew .. it was very helpfull

Popular answers (1)

Matthew Michalska-Smith
University of Minnesota Twin Cities
While the above address your first question (explaining the mathematical formulation of modularity), I don't think any sufficiently address the second (how the formula is being applied in this case), so I will work through your example below:
> g <- graph_from_literal( 1--2, 4--5, 3--1, 5--2, 4--3 )
This produces an igraph object which can be visualized through a matrix by running:
> as_adj(g)# 5 x 5 sparse Matrix of class "dgCMatrix"
# 1 2 4 5 3
# 1 . 1 . . 1
# 2 1 . . 1 .
# 4 . . . 1 1
# 5 . 1 1 . .
# 3 1 . 1 . .
What is important to note here is that igraph does not sort the matrix by row-name (even though you have named your nodes with integers).  Thus, when you assign the membership:
> membership <- c(1,2,1,2,1)
You are stating that nodes 1, 4, and 3 are in module 1 and nodes 2 and 5 are in module 2.
> modularity(g, membership)
Calculates Q as defined by Newman (2006) (and referenced by both Christopher and Daniel above):
Q = 1 / 2m * sum( (Aij - ki*kj / (2m) ) delta(ci,cj), i, j)
m is the number of links = 5
Aij is the element (i, j) of the adjacency matrix depicted above
ki, kj are the degrees of nodes i, j (always 2 in your example, note that all row/column sums of the adjacency matrix are 2)
and delta(ci,cj) is the Kronecker delta, which is 1 if nodes i and j are in the same module and 0 otherwise. In this way, Q only cares about links that fall within modules and ignores those which link separate modules to each other (except as a normalizing factor).
To calculate Q, just go through each element of the adjacency matrix. Note that if it is not within a module (that is, the nodes defined by the row and the column have different membership values), then that element of the sum will be multiplied by 0, so you can save some time by only considering links within the modules. For example, the first row of the adjacency matrix yields:
Q = 1 / (2 * 5) ( (0 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 1 -> absent, same membership
                           (1 - 2 * 2 / (2 * 5) ) * 0 +     # node 1 to 2 -> present, different membership
                           (0 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 4 -> absent, same membership
                           (0 - 2 * 2 / (2 * 5) ) * 0 +     # node 1 to 5 -> present, different membership
                           (1 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 3 -> present, same membership
                           ...
Continuing for the rest of the matrix eventually simplifies to:
Q = 1 / 10 ( 7 * (0 - 2 / 5) + 6 * (1 - 2 / 5) ) = 4 / 50 = 0.08
9 Recommendations

All Answers (5)

Christopher G. Watson
University of Texas Health Science Center at Houston
It should be using Newman's method. See Newman, 2006, PNAS: Modularity and community structure in networks.
1 Recommendation
Daniel M. Holstein
Louisiana State University
I'll give this a shot. Newman has quite a few papers on this. The algorithm is actually given in the help (?modularity):
Q=1/(2m) * sum( (Aij-ki*kj/(2m) ) delta(ci,cj),i,j)     (see the help)
BUT, your example is not a good one, because there is very little modularity. Plot your graph to see.
Here's another example (from the igraph manual):
karate <- graph.famous("Zachary")
wc <- walktrap.community(karate) # This is looking for structure in your graph
modularity(wc) # The modularity score of that structure
membership(wc) # The membership of each vertex to groups found 
plot(wc, karate) # Showing the network and the groups.
Now, use a different, random membership:
new.membership <- round(runif(34,1,5)) # Random vertex membership to 4 groups
modularity(karate, new.membership) # Compare this value to the modularity found in the community analysis above.
I hope that helps a little... I don't fully understand modularity score, myself. What is your goal?
Cheers,
Dan
Matthew Michalska-Smith
University of Minnesota Twin Cities
While the above address your first question (explaining the mathematical formulation of modularity), I don't think any sufficiently address the second (how the formula is being applied in this case), so I will work through your example below:
> g <- graph_from_literal( 1--2, 4--5, 3--1, 5--2, 4--3 )
This produces an igraph object which can be visualized through a matrix by running:
> as_adj(g)# 5 x 5 sparse Matrix of class "dgCMatrix"
# 1 2 4 5 3
# 1 . 1 . . 1
# 2 1 . . 1 .
# 4 . . . 1 1
# 5 . 1 1 . .
# 3 1 . 1 . .
What is important to note here is that igraph does not sort the matrix by row-name (even though you have named your nodes with integers).  Thus, when you assign the membership:
> membership <- c(1,2,1,2,1)
You are stating that nodes 1, 4, and 3 are in module 1 and nodes 2 and 5 are in module 2.
> modularity(g, membership)
Calculates Q as defined by Newman (2006) (and referenced by both Christopher and Daniel above):
Q = 1 / 2m * sum( (Aij - ki*kj / (2m) ) delta(ci,cj), i, j)
m is the number of links = 5
Aij is the element (i, j) of the adjacency matrix depicted above
ki, kj are the degrees of nodes i, j (always 2 in your example, note that all row/column sums of the adjacency matrix are 2)
and delta(ci,cj) is the Kronecker delta, which is 1 if nodes i and j are in the same module and 0 otherwise. In this way, Q only cares about links that fall within modules and ignores those which link separate modules to each other (except as a normalizing factor).
To calculate Q, just go through each element of the adjacency matrix. Note that if it is not within a module (that is, the nodes defined by the row and the column have different membership values), then that element of the sum will be multiplied by 0, so you can save some time by only considering links within the modules. For example, the first row of the adjacency matrix yields:
Q = 1 / (2 * 5) ( (0 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 1 -> absent, same membership
                           (1 - 2 * 2 / (2 * 5) ) * 0 +     # node 1 to 2 -> present, different membership
                           (0 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 4 -> absent, same membership
                           (0 - 2 * 2 / (2 * 5) ) * 0 +     # node 1 to 5 -> present, different membership
                           (1 - 2 * 2 / (2 * 5) ) * 1 +     # node 1 to 3 -> present, same membership
                           ...
Continuing for the rest of the matrix eventually simplifies to:
Q = 1 / 10 ( 7 * (0 - 2 / 5) + 6 * (1 - 2 / 5) ) = 4 / 50 = 0.08
9 Recommendations
Mohammad R. Radi
University Of Kufa
Thanks a lot Matthew .. it was very helpfull

Similar questions and discussions

Related Publications

Article
Neural networks are quantitative models which learn to associate input and output patterns adaptively with the use of learning algorithms. We expose four main concepts from linear algebra which are essential for analyzing these models: 1) the projection of a vector, 2) the eigen and singular value decomposition, 3) the gradient vector and Hessian m...
Article
Full-text available
Motifs are patterns of subgraphs of complex networks. We studied the impact of such patterns of connectivity on the level of correlated, or synchronized, spiking activity among pairs of cells in a recurrent network model of integrate and fire neurons. For a range of network architectures, we find that the pairwise correlation coefficients, averaged...
Got a technical question?
Get high-quality answers from experts.