aspose file tools*
The moose likes Beginning Java and the fly likes How to detect circle in a directed graph? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to detect circle in a directed graph?" Watch "How to detect circle in a directed graph?" New topic
Author

How to detect circle in a directed graph?

George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Hello everyone,


My application needs a feature to detect whether a directed graph contains circle. Does anyone know any efficient implementation? Which implementation is the best (most efficient)?


thanks in advance & happy new year,
George
vivekkumar sharma
Ranch Hand

Joined: Dec 21, 2005
Posts: 70
Hi,
do bit of Google

Otherwise to detect a circle in a graph ,
keep track of nodes visited starting from a particular node,(using a set may be helpful) and if u get same node from where you started ,you have detected a circle.
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
Tarjan's algorithm for detecting cycles will find cycles in O(n+e) time in a directed graph with n vertices and e edges.


<a href="http://www.auptyma.com" target="_blank" rel="nofollow">The Peak of Performance</a>
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Virag,


Originally posted by Virag Saksena:
Tarjan's algorithm for detecting cycles will find cycles in O(n+e) time in a directed graph with n vertices and e edges.



It seems very efficient! But I can not find the algorithm you mentioned from Google by searching key words "Tarjan cycle graph". Could you help to provide more information please?


regards,
George
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
vivekkumar,


Originally posted by vivekkumar sharma:
Hi,
do bit of Google


I do made a search before I posted this question before. The issue is that, I do not know the name of the algorithm so that I can not find much useful information from Google. Could you help please? :-)

Originally posted by vivekkumar sharma:
Otherwise to detect a circle in a graph ,
keep track of nodes visited starting from a particular node,(using a set may be helpful) and if u get same node from where you started ,you have detected a circle.


I have tried this method. I do not think this method is correct. For example, suppose in a simple graph, there are four nodes, A, B, C, D. The directed graph has the following edges,

A-->B
A-->C
B-->D
C-->D

In this graph, there is no cycle. But when running your method, since node D will be accessed twice both by node B and by node C, the directed graph will be detected cycle by your method.

Maybe I do not quite understand your method, could you provide a simple implementaion of your method please?

regards,
George
Praveena Jayanthi
Greenhorn

Joined: Dec 30, 2005
Posts: 10
George,

I tried to implement a sample implementation with a simple DataStructure "NodeLink" to indicate "From" and "To" vertices of a link.

Following are the steps to approach this problem
1)Make an appropriate data structure to represent the graph, its vertices and node links.
2)Construct partial graphs for each entry in the nodelinks i.e all the vertices approachable from a given vertex.
3)Iterate through the partial graphs constructed to see if any node is visited more than once in a subgraph.

In the program below, I used a hastable to keep track of nodes and the number of times visited count. I would initialize the hashtable with "0 visit counts" for each partial graph.






I hope this helps!

-Praveena
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
George,
You can try searching for
robert tarjan strongly connected components in a directed graph
If you can't find it, send me a private message, and I'll send you a copy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to detect circle in a directed graph?