我正在创建一个圆形格子图并考虑其相应的完整图。我将整个图中不在晶格图中的边缘称为“非晶格”边缘。现在,我想从晶格中选择要删除的许多边,然后再从非晶格边中添加一些随机选择的边,以创建一个新图。这是一个小示例,您将看到它分解的位置(最后一行)。基本上,我在边缘列表是序列时遇到麻烦。另外,如果我不必指定所有更好的节点就可以做到这一点-最终,我将拥有大型图形!这是我当前的代码:
n <- 5 #number of nodes
k <- 1 #number of neighbors for lattice connections
g <- make_lattice(length = n, dim = 1, nei = k, circular = TRUE) #lattice
V(g)$name <- letters[1:n] #name nodes
lat_e <- E(g) #lattice edges
g1 <- g #make a copy
g1[V(g1), V(g1)] <- TRUE #add all possible edges
g1 <- simplify(g1) #remove loops
newe <- E(g1)
non_lat_e <- difference(newe, olde) #non lattice edges
n_switch <- 2 #want to change 2 lattice edges to
non_lattice edges
e_rem <- sample(1:length(lat_e),n_switch)
e_add <- sample(1:length(non_lat_e), n_switch)
g <- delete_edges(g, lat_e[e_rem]) #delete lattice edges
g <- add.edges(g, non_lat_e[e_add]) #add non lattice edges. ERROR
答案 0 :(得分:0)
在将边线添加到新图形时,您不能从一个图形中引用节点。您需要按名称提供成对的矢量,以便添加新的边缘。像这样
non_lattice_edges <- ends(g1, non_lat_e[e_add]) %>%
t() %>%
as.vector()
g <- add.edges(g, non_lattice_edges)
如果从不删除矢量,则不需要添加顶点名称,因为顶点索引将保持一致。这是您的代码,但没有命名的顶点:
n <- 5 #number of nodes
k <- 1 #number of neighbors for lattice connections
g <- make_lattice(length = n, dim = 1, nei = k, circular = TRUE) #lattice
lat_e <- E(g) #lattice edges
g1 <- g #make a copy
g1[V(g1), V(g1)] <- TRUE #add all possible edges
g1 <- simplify(g1) #remove loops
newe <- E(g1)
non_lat_e <- difference(newe, lat_e) #non lattice edges
n_switch <- 2
e_rem <- sample(1:length(lat_e),n_switch)
e_add <- sample(1:length(non_lat_e), n_switch)
g <- delete_edges(g, lat_e[e_rem]) #delete lattice edges
non_lattice_edges <- ends(g1, non_lat_e[e_add]) %>%
t() %>%
as.vector()
g <- add.edges(g, non_lattice_edges)