Collapsing graph by clusters in igraph

ε祈祈猫儿з 提交于 2019-12-10 03:34:07

问题


I want to collapse a graph into its respective communities/clusters. Let me illustrate this with the following toy example:

set.seed(123)

#toy graph
g <- barabasi.game(10) %>%
  as.undirected()

#identify communities 
c_g <- fastgreedy.community(g) 

There are three communities, as seen in the following graph.

I want to reduce the collapse the vertices so that vertices in the resulting graph correspond to the membership of the previous vertices. See the graph.

I'm new to the igraph package and I'm not familiar with the best way of dealing with igraph objects.


回答1:


You could try contract:

library(igraph)
set.seed(123)
g <- barabasi.game(10) %>% as.undirected()
c_g <- fastgreedy.community(g) 
V(g)$name <- letters[1:vcount(g)]

g2 <- contract(g, membership(c_g), vertex.attr.comb=toString)

par(mfrow=c(1,2))
plot(g, vertex.color=membership(c_g))
plot(simplify(g2), vertex.color=1:vcount(g2))



来源:https://stackoverflow.com/questions/35000554/collapsing-graph-by-clusters-in-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!