R / igraph : getting vertex neighbors list within a depth-first-search callback causes a R to close. Any suggestion?

StackOverflow https://stackoverflow.com/questions/21332819

Question

I'm trying to write a script in R to identify the elements (edges and vertices) belonging to the cycles of the graph.

I'm using a callback function within the graph.dfs (igraph R package). I don't want to modify the graph, just want to build a new a list of visited vertices (and update it) each time the dfs algorithm visits a vertex. The problem is that R program closes if I run the following code. It might be doing something wrong - any suggestion/ help?

#create a simple graph with 4 nodes and 1 cycle
gIncidenceMatrix <- matrix (c(0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0), nrow=4, ncol=4, byrow=T)

g <- graph.adjacency(gIncidenceMatrix, mode = "undirected")

f.cycleDetection <- function(g, data, extra) {

vId <- data[1]
nVertices <- neighbors(g, vId+1, mode = 1)        #vId + 1 is to avoid having index 0 (any suggestion?)

FALSE
}

graph.dfs(g, root=1, neimode = "all", unreachable = TRUE, order = FALSE, order.out = FALSE, father = FALSE, dist = FALSE, in.callback = f.cycleDetection, out.callback = NULL, extra = NULL, rho = parent.frame())

I found the following thread (R / igraph : any call to get/set vertex attribute within a depth-first-search callback causes a segfault) but it looks its answer is focused in changing properties of the graph elements within the callback function, which is somewhat different than what I'm trying to do.

Was it helpful?

Solution

Actually, it is worse than what is in the referred question. It seems that you cannot even call an igraph function from the callback. The solution is to query all neighbors before the DFS, using get.adjlist and then using them in the DFS.

I have created a bug report for this: https://github.com/igraph/igraph/issues/571

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top