R: igraph, community detection, edge.betweenness method, count/list members of each community?

后端 未结 2 759
Happy的楠姐
Happy的楠姐 2021-02-02 02:38

I\'ve a relatively large graph with Vertices: 524 Edges: 1125, of real world transactions. The edges are directed and have a weight (inclusion is optional). I\'m trying investig

2条回答
  •  执念已碎
    2021-02-02 03:07

    In regard to "how to control the number of communities" in OPs question, I use the cut_at function on the communities to cut the resulting hierarchical structure into a desired number of groups. I hope someone can confirm that I am doing something sane. Namely, consider the following:

    #Generate graph
    adj.mat<- matrix(,nrow=200, ncol=200) #empty matrix
    set.seed(2) 
    
    ##populate adjacency matrix
    for(i in 1:200){adj.mat[i,sample(rep(1:200), runif(1,1,100))]<-1}
    adj.mat[which(is.na(adj.mat))] <-0
    
    for(i in 1:200){
      adj.mat[i,i]<-0
    }
    
    G<-graph.adjacency(adj.mat, mode='undirected')
    plot(G, vertex.label=NA)
    
    ##Find clusters
    walktrap.comms<- cluster_walktrap(G, steps=10)
    max(walktrap.comms$membership) #43
    
      [1]  6 34 13  1 19 19  3  9 20 29 12 26  9 28  9  9  2 14 13 14 27  9 33 17 22 23 23 10 17 31  9 21  2  1
     [35] 33 23  3 26 22 29  4 16 24 22 25 31 23 23 13 30 35 27 25 15  6 14  9  2 16  7 23  4 18 10 10 22 27 27
     [69] 23 31 27 32 36  8 23  6 23 14 19 22 19 37 27  6 27 22  9 14  4 22 14 32 33 27 26 14 21 27 22 12 20  7
    [103] 14 26 38 39 26  3 14 23 22 14 40  9  5 19 29 31 26 26  2 19  6  9  1  9 23  4 14 11  9 22 23 41 10 27
    [137] 22 18 26 14  8 15 27 10  5 33 21 28 23 22 13  1 22 24 14 18  8  2 18  1 27 12 22 34 13 27  3  5 27 25
    [171]  1 27 13 34  8 10 13  5 17 17 25  6 19 42 31 13 30 32 15 30  5 11  9 25  6 33 18 33 43 10
    

    Now, note that there are 43 groups but we want coarser cuts hence, examine the dendrogram:

    plot(as.hclust(walktrap.comms), label=F)
    

    And cut based on it. I arbitrarily chose 6 cuts but nevertheless, you now have coarser clusters

    cut_at(walktrap.comms, no=6)
    
      [1] 4 2 5 4 5 5 3 5 3 4 3 5 5 3 5 5 3 1 5 1 1 5 1 6 1 1 1 4 6 5 5 2 3 4 1 1 3 5 1 4 6 6 3 1 5 5 1 1 5 4 3 1
     [53] 5 2 4 1 5 3 6 3 1 6 6 4 4 1 1 1 1 5 1 4 3 3 1 4 1 1 5 1 5 2 1 4 1 1 5 1 6 1 1 4 1 1 5 1 2 1 1 3 3 3 1 5
    [105] 3 3 5 3 1 1 1 1 3 5 2 5 4 5 5 5 3 5 4 5 4 5 1 6 1 3 5 1 1 1 4 1 1 6 5 1 3 2 1 4 2 1 2 3 1 1 5 4 1 3 1 6
    [157] 3 3 6 4 1 3 1 2 5 1 3 2 1 5 4 1 5 2 3 4 5 2 6 6 5 4 5 3 5 5 4 4 2 4 2 3 5 5 4 1 6 1 2 4
    

提交回复
热议问题