generating a community graph in igraph

前端 未结 1 1285
野的像风
野的像风 2021-02-03 12:46

I have been searching for an answer to this question but could not find any mention, so I decided to post here. I am trying to see if igraph or any packages provide a simple way

相关标签:
1条回答
  • 2021-02-03 13:05

    You can simply use the contract.vertices() function. This contracts groups of vertices into a single vertex, essentially the same way you want it. E.g.

    library(igraph)
    
    ## create example graph
    g1 <- graph.full(5)
    V(g1)$name <- 1:5    
    g2 <- graph.full(5)
    V(g2)$name <- 6:10
    g3 <- graph.ring(5)
    V(g3)$name <- 11:15
    g <- g1 %du% g2 %du% g3 + edge('1', '6') + edge('1', '11')
    
    ## Community structure
    fc <- fastgreedy.community(g)
    
    ## Create community graph, edge weights are the number of edges
    cg <- contract.vertices(g, membership(fc))
    E(cg)$weight <- 1
    cg2 <- simplify(cg, remove.loops=FALSE)
    
    ## Plot the community graph
    plot(cg2, edge.label=E(cg2)$weight, margin=.5, layout=layout.circle)
    
    0 讨论(0)
提交回复
热议问题