[英]How to find edge ids of some vertices in igraph?
我在 R 的igraph
中遇到get.edge.ids()
function 的問題,我需要將奇數個頂點傳遞給它並獲取它們之間的edgeIDs
,但不幸的是它只能獲得成對的頂點示例代碼來生成有向圖:
Graph <- erdos.renyi.game(20, 100 , directed=TRUE, loops=FALSE)
如何調用 get.edge.ids:
get.edge.ids(Graph, c("1", "2", "3))
我希望在這些頂點之間獲得所有可能的邊 ID,但它不起作用。 為此,我開發了 function,但速度不夠快。 這是 function:
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)])
}
有什么辦法可以更快地做到這一點?
您可以使用%--%
運算符按頂點索引查詢邊,然后使用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
這與您的自定義 function 中的 output 匹配:
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
由代表 package (v0.3.0) 於 2020 年 4 月 10 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.