删除igraph R中顶点之间的边

时间:2014-06-20 09:24:26

标签: r igraph edges

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

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

我想从顶点对定义的图中删除一堆边,例如,我想删除以下边:

V4 -- V5
V3 -- V7
V3 -- V6

这些边存储在称为“边”的数据框中。我想一次删除这些边缘。我试过了:

> 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(边缘))

如何在一个命令中从图A中删除这些边?感谢

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

手册建议使用E从图表中提取要删除的边缘, 或edges从他们的名字构建它们(我们不知道您的edges data.frame包含的内容。)

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 )