Possible Duplicate:
Finding all cycles in graph
can anybody give me tutorial,algorithm,... for detect cycles in graph ?
i find several algorithm and implement them but doesn't detect all cycles
Possible Duplicate:
Finding all cycles in graph
can anybody give me tutorial,algorithm,... for detect cycles in graph ?
i find several algorithm and implement them but doesn't detect all cycles
From more mathematical point of view:
Input: Graph G=(V,E)
Assume that your graph is not disjoint (there exists a path between every two vertices)
Compute the spanning tree T of the graph (there are easy algorithms to do that)
Let E' be a subset of E, that don't belong to the spanning tree T. For each edge e in E', its addition to the tree creates exactly a single cycle. Let's put all those cycles into set B.
We define a binary cycle space over cycles in your graph. In that space, two cycles can be added. The addition is simply an exclusive sum over the edges.
The set of cycles B is a "cycle basis". Every other cycle in your graph can be formed as a linear combination of the cycles B.
This way you obtain all possible cycles in your graph.
Warning: if your input graph has v vertices and e edges then there are 2^(e - v +1)-1 different cycles! That's quite a lot - you might not want to explicitly write all of them.
A BFS alg. is also ok. I believe it's easier to implement than DFS, since it keeps the visited / to-visit nodes in a queue. It's easier to check if a certain nodes has already been visited.
I think DFS will do your job coz we mark visited node and if we find that node again there is a cycle