Igraph R:两个组合图的一些顶点的约束计算NA

时间:2016-08-29 05:17:18

标签: r graph igraph

使用R和igraph,我有两个已合并的图表,然后运行constraint()。约束适用于g1,但当我添加g2时,constraint()会为NA添加的新顶点和这些新顶点旁边的顶点返回g2

以下是复制我的问题的示例代码。我从二分图的投影中获得g1,因为这反映了我的数据处理。

我的问题可能是两个图表联合的问题,但无论哪种方式我都尝试(g3 <- g1 + g2g3 <- graph.union(g1, g2)),约束计算会带来NA s。

set.seed(42)
g <- sample_bipartite(100, 10, type = c("gnp"), p=.03, directed = FALSE) 
gproj <- bipartite_projection(g, types=NULL, multiplicity = TRUE)
g1 <- gproj[[1]]
V(g1)$name <- 1:vcount(g1) #this gets their actual vertex id to show as label
V(g1)$name
components <- decompose.graph(g1)
largest <- which.max(sapply(components, vcount))
largest #this tells me which component is largest
lc <- components[[largest]]
lc
plot(lc)
cg1 <- constraint(lc)
cg1 #constraint for all connected vertices is calculated

#Create g2, some vertices in g1, some are new
rel <- data.frame( rel1 =
                     c(95), rel2 =
                     c(2000), stringsAsFactors = F) #create edgelist of g2, one vertex in lc, one vertex new
g2  <- graph.data.frame(rel, directed=FALSE)

#combine graphs and calculate constraint on combined graph
#g1 is used instead of lc because relationships in g2 may connect previously isolated vertices/components
g3 <- g1 + g2 
components1 <- decompose.graph(g3)
largest1 <- which.max(sapply(components1, vcount))
largest1 #this tells me the first component is largest
lc1 <- components1[[largest1]]
plot(lc1)
cg3 <- constraint(lc1)
cg3 #now constraint vertices close to 2000 is 'NA'

有关进一步的信息,其他igraph测量,如特征向量中心,学位和bonpower不会遇到这个问题。

1 个答案:

答案 0 :(得分:0)

通过一些额外的调查,我找到了一个解决方案。问题是g1有权重,而g2没有。删除权重后,使用此代码(并使用g1a向下行)constraint计算所有顶点。

g1a <- remove.edge.attribute(g1, "weight")

甚至比为这个玩具示例工作更好,这个解决方案适用于我的完整,更大的数据集。