Finding cycles in a directed graph
From: Steinar H. Gunderson (sgunderson_at_bigfoot.com)
Date: 02/25/04
- Next message: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Previous message: Axel Boldt: "Re: Comparing two notions of computable number"
- Next in thread: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Reply: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Reply: Glenn C. Rhoads: "Re: Finding cycles in a directed graph"
- Reply: Emiel vl: "Re: Finding cycles in a directed graph"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 25 Feb 2004 16:05:01 +0000 (UTC)
Hi,
Is there a standard algorithm for finding cycles in a directed graph?
Basically, I want to find any cycle (if any), that is, node A has an
edge to B, which in turn has an edge to C, which has an edge back again
to A (of any length, of course). I also need to be able to "wind up" the
cycle back again to find out what nodes are involved; just knowing that
there _is_ a loop somewhere is not good enough.
I'm currently using Floyd-Warshall to find the transitive closure of the
graph, and then checking if any node has an edge to itself (from there
it's quite simple to find any cycle, by finding a node has both an edge
to itself and from the node I already knew was part of a cycle), but
this is O(V^3) and I was wondering if there was something more
efficient. (The big advantage is of course that it's simple; the
Floyd-Warshall code itself is five lines of C, and I'd rather not
implement full Fibonacci heaps etc. if I can help it :-) )
I'm currently working with data sets of about V=500, E=1500, but this
might grow in the future, and I'm not really guaranteed a very fast
machine either, so an improvement down to something along the lines of
O(V^2) would be very helpful. It should be noted that _if_ there is a
cycle, it's very likely to be quite short (about 15-20 nodes long); I
don't know if I can take advantage of this somehow.
Any help or pointers would be much appreciated. :-)
/* Steinar */
-- Homepage: http://www.sesse.net/
- Next message: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Previous message: Axel Boldt: "Re: Comparing two notions of computable number"
- Next in thread: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Reply: Matthias Klaey: "Re: Finding cycles in a directed graph"
- Reply: Glenn C. Rhoads: "Re: Finding cycles in a directed graph"
- Reply: Emiel vl: "Re: Finding cycles in a directed graph"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|