igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] A query about finding singleton cycles in a graph


From: Simon Tavare
Subject: [igraph] A query about finding singleton cycles in a graph
Date: Thu, 2 Jan 2020 11:13:39 -0800

In response to a query on Stack Overflow on March 10 2019 by Ankit, the following  function, FindCycles, was given:

## More efficient version
FindCycles = function(g) {
  Cycles = NULL
  for(v1 in V(g)) {
    if(degree(g, v1, mode="in") == 0) { next }
    GoodNeighbors = neighbors(g, v1, mode="out")
    GoodNeighbors = GoodNeighbors[GoodNeighbors > v1]
    for(v2 in GoodNeighbors) {
      TempCyc = lapply(all_simple_paths(g, v2,v1, mode="out"), function(p) c(v1,p))
      TempCyc = TempCyc[which(sapply(TempCyc, length) > 3)]                                 ###
      TempCyc = TempCyc[sapply(TempCyc, min) == sapply(TempCyc, `[`, 1)]
      Cycles  = c(Cycles, TempCyc)                   }
  }
  Cycles
}

I am interested in finding cycles of any length, so that the line marked ### should be removed.

The resultant code works for cycles of size at least two, but does not catch singleton cycles.
Is there a simple fix?

(I wrote an obvious modification of the code above to search for just singleton cycles, but it seems that there must be a simple tweak to the code above.)

Many thanks,

Simon


reply via email to

[Prev in Thread] Current Thread [Next in Thread]