在 igraph R 中删除顶点之间的边

4

我有一个无权无向图(A),它有10个顶点和10条边。

> A
IGRAPH UNW- 10 10 -- 
+ attr: name (v/c), weight (e/n)

我想按照顶点对的定义从该图中删除一些边,例如,我想删除以下边缘:
V4 -- V5
V3 -- V7
V3 -- V6

这些边缘存储在名为“edges”的数据框中。我想一次性删除这些边缘。我尝试了以下方法:
> delete.edges(A,t(edges))

但是这样做没有效果,并且返回错误:
Error in as.igraph.es(graph, edges) : Invalid edge names
In addition: Warning message:
In as.igraph.es(graph, edges) : NAs introduced by coercion

为什么添加边的等价命令可以工作,而这个命令却不能工作?

add.edges(A,t(edges))

请问有没有一种命令可以一次性从图A中删除这些边?谢谢。
3个回答

6

可能最简单的方法是使用图作为邻接矩阵:

library(igraph)
g <- graph.ring(10)
V(g)$name <- letters[1:10]
str(g)
# IGRAPH UN-- 10 10 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
# + edges (vertex names):
#  [1] a--b b--c c--d d--e e--f f--g g--h h--i i--j a--j


g[ from=c("a","b","c"), to=c("b","c","d") ] <- 0
str(g)
# IGRAPH UN-- 10 7 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
# + edges (vertex names):
# [1] d--e e--f f--g g--h h--i i--j a--j

请查看http://igraph.org/r/doc/graph.structure.html了解更多相关的IT技术信息。

0

Delete_edges接受一系列边缘ID。通过它们的关联顶点删除边缘需要额外转换为边缘ID。

ei <- get.edge.ids(g, c(3,6, 3,7, 4,5))

g  <- graph(c(3,6, 3,7, 4,5), 10, directed=FALSE) +
      path(1,2,5,6,7,8,9,10)

ei <- get.edge.ids(g, c(3,6, 3,7, 4,5))
g2 <- delete_edges(g, ei)

边缘也可以通过符号名称进行删除。
g3 <- set_vertex_attr( g
                     , name="name"
                     , value=strsplit(paste0("V", seq_len(10)), " " )
                     )
g4 <- delete_edges( g3
                  , get.edge.ids(g3, c("V3","V6", "V3","V7", "V4","V5"))
                  )

0
手册建议使用E从图中提取要删除的边,或使用edges从它们的名称构建它们(我们不知道您的edges数据帧包含什么)。
library(igraph)

# Sample graph
g <- graph.ring(10)
plot(g)

# Edges to remove, as a data.frame
e <- data.frame( 
  from = 1:3,
  to   = 2:4
)

# Convert the data.frame to edges
e <- apply(e, 1, paste, collapse="|")
e <- edges(e)

# Remove the edges and plot the resulting graph.
plot( g - e )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接