本文介绍了如何完成将0分配为新边的权重属性的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下图表
library(igraph)
g <- erdos.renyi.game(100, 2/100)
E(g)$weight <- sample(1:10, ecount(g), replace=TRUE)
我有兴趣通过添加所有缺失的边来完成"图形(因此,每对顶点将通过一条边连接),但要确保为新边分配了E(g)$ weight = 0. /p>
有可能吗?
解决方案
这应该有效.
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
Please consider the following graph
library(igraph)
g <- erdos.renyi.game(100, 2/100)
E(g)$weight <- sample(1:10, ecount(g), replace=TRUE)
I am interested in "completing" the graph by adding all missing edges (as a result each pair of vertices will be connected by an edge) but making sure the new edges are assign E(g)$weight = 0.
Is it possible?
解决方案
This should work.
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
According to the comments, I would like to suggest more robust answer that is based on the extra attribute of edges,please see below and give comments.
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
这篇关于如何完成将0分配为新边的权重属性的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!