Bug in Tarjan.strongconnect()

36 views
Skip to first unread message

Mykolas Mickus

unread,
May 26, 2016, 9:11:24 AM5/26/16
to Grph: High Performance Graph Library for Java
Hello,
Today I've been trying out Grph. In particular, I needed implementation of strongly connected compoents search algorithm, which this library has. Unfortunately, it has a bug inside the Tarjan.stongconnect() method. Here is the problematic code:

// Consider successors of v
for (int w : g.getOutNeighbors(v).toIntArray())
{
   if (indexes[w] == -1)
   {
      // Successor w has not yet been visited; recurse on it
      strongconnect(w, g, indexes, lowlink, stack, cycles);
      lowlink[v] = Math.min(lowlink[v], lowlink[w]);
   }
   else if (stack.contains(w))
   {
      // Successor w is in stack S and hence in the current SCC
      lowlink[v] = Math.min(lowlink[v], indexes[w]);
   }
}

Problem is that stack.contains() is a O(|N|) operation, where |N| is number of nodes in the graph. For this algorithm to have an upper bound of O(|N| + |E|) checking if a node is on stack should be O(1). I would suggest using an array of booleans or something similar for this purpose. Because at the moment implementation has complexity of O(|N|^2), which is unusable for graphs of larger sizes. Hopefully, my find will be useful.

Luc Hogie

unread,
Nov 6, 2017, 7:32:17 AM11/6/17
to Grph: High Performance Graph Library for Java
Hi,

Sorry for the long delay. This clever change will be available in the next release.
Reply all
Reply to author
Forward
0 new messages