请考虑以下图表
library(igraph)
g <- erdos.renyi.game(100, 2/100)
E(g)$weight <- sample(1:10, ecount(g), replace=TRUE)
我感兴趣的是通过添加所有缺失边来“完成”图形(因此每对顶点将通过边连接),但确保新边指定为E(g)$ weight = 0。 / p>
有可能吗?
答案 0 :(得分:0)
这应该有效。
olde = E(g) # saving edges
g[V(g), V(g)] <- TRUE # adding all possible edges
E(g)$weight <- 0 # all weights is 0
E(g)[olde]$weight <- olde$weight # old weights is equal to old weights
g <- simplify(g) # removing loops
根据评论,我想建议更强大的答案,这是基于边缘的额外属性, 请参阅下文并发表评论。
ids = E(g) # saving old ids
E(g)$oldids = ids # assigning to specific edge as extra attribute
olde = E(g) # saving edges
g[V(g), V(g)] <- TRUE # adding all possible edges
E(g)$weight <- 0 # all weights is 0
# Now it is more robust, because it matches oldids with the ids of old graph
E(g)[match(ids, oldids)]$weight <- olde$weight[ids] # old weights is equal to old weights