1

i want to use NMI to compare my algorithm in community detection with other methods.so i am making some graphs with sample_sbm() which i define to give me 10 nodes ,and in block.sizes=c(3,3,4) part i define to have communities,first one has 3members,second 3,third 4 members. now i want a membership vector of them.it should be : 1 1 1 2 2 2 3 3 3 3

what is the best way to do it?i was thinking of taking 3 arguments c1,c2,c3 and then using them in block.sizes(),so i can use a for loop to build the membership vector.but looks a bit dirty.cause the number of communities should be arbitrary. i will be thankful if you suggest me something nicer

library(igraph)
p<-cbind( c(1, 0,0), c(0, 1,0) ,c(0,0,1))
g <- sample_sbm(10, pref.matrix=p, block.sizes=c(3,3,4) )


#comunity detection algorithm
wc <- cluster_walktrap(g)
modularity(wc)

a=membership(wc)

Soma
  • 329
  • 1
  • 8

1 Answers1

2

UPDATE following the original question-asker's comments:

I store the sizes of the blocks in a my_block_sizes vector. Then I use the rep.int function and the seq_along function to create the membership vector according to the sizes of the blocks.

library(NMI)
library(igraph)

my_block_sizes <- c(3,3,4)

# make a membership vector
membership_vector <- rep.int(seq_along(my_block_sizes), my_block_sizes)
membership_vector
[1] 1 1 1 2 2 2 3 3 3 3

p <- cbind(c(1,0,0), c(0,1,0), c(0,0,1))
g <- igraph::sample_sbm(10, pref.matrix=p, block.sizes=my_block_sizes)

# comunity detection algorithm
wc <- cluster_walktrap(g)
modularity(wc)

a <- membership(wc)

Original answer:

I'm not 100% sure this is what you're after, but based on the information you've provided, this may solve your problem.

I use the length of the wc object to determine the number of communities detected by the community detection algorithm, and the rep.int function to repeat each community number according to the size of the blocks, which I store in advance in the my_block_sizes object.

library(NMI)
library(igraph)

my_block_sizes <- c(3,3,4)

p <- cbind(c(1,0,0), c(0,1,0), c(0,0,1))
g <- igraph::sample_sbm(10, pref.matrix=p, block.sizes=my_block_sizes)


#comunity detection algorithm
wc <- cluster_walktrap(g)
modularity(wc)

a <- membership(wc)

# make a membership vector
membership_vector <- rep.int(1:length(wc), my_block_sizes)
membership_vector
[1] 1 1 1 2 2 2 3 3 3 3
meenaparam
  • 1,949
  • 2
  • 17
  • 29
  • 1
    yes yes it works.but there is only one point.i should have membership_vector for my graph before using algorithms ! but luckily its equal to number of nodes which it is first argument of sample_sbm()function.so i can save it as my_n and then use your method, thank you – Soma Jun 21 '19 at 16:28
  • Glad that this answer helped you. Please do accept this answer if it solves your problem. Otherwise I'm happy to help further. I'm afraid I don't quite understand what determines the `membership_vector`. It sounds like the length of the vector is equal to the first argument of the `sample_sbm` function, but what determines how many communities/integer membership values there are? For example, you have three membershp values: 1, 2, 3 - how is that determined? If you can tell me where those numbers come from, I can update my solution to help you further. – meenaparam Jun 23 '19 at 16:00
  • 1
    yes it is what i need was that rep.int () `function.rep.int(seq_along(my_block_sizes), my_block_sizes)` .this code work so please replace it cause i am going to accept your answer.andlength of my_block_sizes is the number of my communities which it is 3 here.so i just want my membership_vector shows 3 first nodes are in one community(number1),3other are in one community (number2)and 4th other are in same community (number3). – Soma Jun 24 '19 at 19:33