用igraph中的二分图进行社区检测

时间:2014-05-08 20:39:34

标签: igraph bipartite

我有1000个椎体的二分列表(帖子,单词类别),并希望使用快速和贪婪的算法进行社区检测,但我不确定是否必须在二分图或二分投影上运行它。 / p>

我的二分列表如下:

   post word
1   66  2
2   312 1
3   432 7
4   433 7
5   434 1
6   435 5
7   436 1
8   437 4

当我在没有投影的情况下运行它时,我在第二步中遇到问题:

### Load bipartie list and create graph ###
bipartite_list <- read.csv("bipartite_list_tnf.csv", header = TRUE, sep = ";")
bipartite_graph <- graph.incidence(bipartite_list)
g<-bipartite_graph
fc <- fastgreedy.community(g) ## communities / clusters
set.seed(123)
l <- layout.fruchterman.reingold(g, niter=1000, coolexp=0.5) ## layout
membership(fc)
# 2. checking who is in each cluster
cl <- data.frame(name = fc$post, cluster = fc$membership, stringsAsFactors=F)
cl <- cl[order(cl$cluster),]
cl[cl$cluster==1,]

# 3. preparing data for plot
d <- data.frame(l); names(d) <- c("x", "y")
d$cluster <- factor(fc$membership)

# 4. plot with only nodes, colored by cluster
p <- ggplot(d, aes(x=x, y=y, color=cluster))
pq <- p + geom_point()
pq

也许我必须对投影进行社区检测?但后来我总是失败因为投影不是图形对象:

bipartite_graph <- graph.incidence(bipartite_list)
#projection (both directions)
projection_word_post <- bipartite.projection(bipartite_graph)
fc <- fastgreedy.community(projection_word_post)
Fehler in fastgreedy.community(projection_word_post) : Not a graph object

我很乐意帮忙!

1 个答案:

答案 0 :(得分:1)

当您在没有投影的情况下运行时,问题出在:

bipartite_graph <- graph.incidence(bipartite_list)

在应用graph.incidence()函数之前,您需要重新塑造'bipartite_list'。使用以下命令

tab <- table(bipartite_list)

其余步骤相同

g <- graph.incidence(tab,mode=c("all"))
fc <- fastgreedy.community(g)