Question

I want to find 1) the modularity of a network and find 2) the identities of the nodes in each community.

I think this is the way to get modularity:

g <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
g <- add.edges(g, c(1,6, 1,11, 6, 11))
ebc <- edge.betweenness.community(g)

sizes(ebc)
#Community sizes
#1 2 3 
#5 5 5 

modularity(g,membership(ebc))
#[1] 0.5757576

but on this link by Gabor I get this code:

memberships <- list()
G <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
G <- add.edges(G, c(1,6, 1,11, 6, 11))

### edge.betweenness.community
ebc <- edge.betweenness.community(G)
mods <- sapply(0:ecount(G), function(i) {
  g2 <- delete.edges(G, ebc$removed.edges[seq(length=i)])
  cl <- clusters(g2)$membership
  modularity(G, cl)
})

g2 <- delete.edges(G, ebc$removed.edges[1:(which.max(mods)-1)])
memberships$`Edge betweenness` <- clusters(g2)$membership

This seems to be doing the same thing that I am, I think that the delete.edges and clusters is about splitting the modules into separate components by deleting the connecting edges and then getting the IDs of nodes in each component.

Although there are a few questions I have:

  1. In @Gabor Csardil 's code why does the modularity call use clusters(G)$membership and not ebc like I did in my example? What is the difference? (I would have thought that would overestimate actual modularity?) There also seems to be an alternate using community.to.membership

  2. The mods code returns this error which I do not understand which is partly why I cannot explore g2 and cl to see more of what is happening:

Error in delete.edges(G, ebc$removed.edges[seq(length = i)]) : At iterators.c:1809 : Cannot create iterator, invalid edge id, Invalid vertex id

Was it helpful?

Solution

The general answer is that the code in the wiki is outdated. It works with igraph version 0.5.x, but not with 0.6.x. More specifically:

  1. In igraph version 0.5.x there was no direct way to get the membership vector, in version 0.6.x you can just say membership(ebc).

  2. In version 0.5.x vertex ids start with zero, in version 0.6.x they start with one, so you don't meed to subtract one in the delete.edges() line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top