我在RI中的get.edge.ids()
中的igraph
函数存在问题,需要向其传递奇数个顶点并在它们之间获取edgeIDs
,但不幸的是,它仅获得成对顶点的示例代码生成有向图:
Graph <- erdos.renyi.game(20, 100 , directed=TRUE, loops=FALSE)
如何调用get.edge.ids:
get.edge.ids(Graph, c("1", "2", "3))
我希望在这些顶点之间获得所有可能的边缘ID,但是它不起作用。我为此目的开发了一个函数,但是它不够快。这是函数:
insideOfCommEdgeIDs <- function(graph, vertices)
{
out <- matrix()
condition <- matrix()
if (length(vertices) < 2) {return(NULL)}
for (i in vertices)
{
for (j in vertices)
{
condition <- are_adjacent(graph,i,j)
ifelse(condition,
out <- rbind(out, get.edge.ids(graph, c(i, j), directed=TRUE)),
next)
}
}
return(out[!is.na(out)])
}
有什么办法可以更快地做到这一点?
答案 0 :(得分:2)
您可以使用%--%
运算符通过顶点索引查询边缘,然后使用as_ids()
获取边缘索引。
请注意,我使用的是igraph
版本1.2.4.2,所以我使用的是sample_gnm()
而不是erdos.renyi.game()
。
library(igraph)
set.seed(1491)
Graph <- sample_gnm(20, 100 , directed = TRUE, loops = FALSE)
as_ids(E(Graph)[c(1, 2, 3) %--% c(1, 2, 3)])
#> [1] 6 12
这与您的自定义函数的输出相匹配:
insideOfCommEdgeIDs <- function(graph,vertices)
{
out <- matrix()
condition <- matrix()
if(length(vertices) < 2) {return(NULL)}
for(i in vertices)
{
for (j in vertices)
{
condition <- are_adjacent(graph,i,j)
ifelse(condition,out <- rbind(out,get.edge.ids(graph,c(i,j),directed = TRUE)),next)
}
}
return(out[!is.na(out)])
}
insideOfCommEdgeIDs(Graph, c(1, 2, 3))
#> [1] 6 12
由reprex package(v0.3.0)于2020-04-10创建