简体   繁体   中英

How to find edge ids of some vertices in igraph?

I have an issue with get.edge.ids() function in igraph in R I need to pass odd number of vertices to it and get the edgeIDs between them but unfortunately it only gets pairwise vertices sample code to generate a directed graph:

Graph <- erdos.renyi.game(20, 100 , directed=TRUE, loops=FALSE)

how do I call get.edge.ids:

get.edge.ids(Graph, c("1", "2", "3)) 

I expect to get all possible edges IDs between these vertices but it doesn't work. I developed a function for this purpose but it is not fast enough. Here is the 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)])
}

Is there any way to to this faster?

You can use the %--% operator to query edges by vertex indices and then use as_ids() to get the edge index.

Please note, I'm using igraph version 1.2.4.2, so I'm using sample_gnm() rather than 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

This matches the output from your custom 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)])
}

insideOfCommEdgeIDs(Graph, c(1, 2, 3))
#> [1]  6 12

Created on 2020-04-10 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM