views:

247

answers:

2

Hi,

I know there are a quite some answers existing on this question. However, I found none of them really bringing it to the point.
Some argue that a cycle is (almost) the same as a strongly connected components (s. http://stackoverflow.com/questions/546655/finding-all-cycles-in-graph/549402#549402) , so one could use algorithms designed for that goal.
Some argue that finding a cycle can be done via DFS and checking for back-edges (s. boost graph documentation on file dependencies).

I now would like to have some suggestions on whether all cycles in a graph can be detected via DFS and checking for back-edges?
http://www.me.utexas.edu/~bard/IP/Handouts/cycles.pdf (found here on S.O.) states one methode based on cycle bases. Me personally, I don't find it very intuitive so I'm looking for a different solution.

EDIT: My initial opinion was apparently wrong. S. next answer by "Moron".
Initial opinion: My opinion is that it indeed could work that way as DFS-VISIT (s. pseudocode of DFS) freshly enters each node that was not yet visited. In that sense, each vertex exhibits a potential start of a cycle. Additionally, as DFS visits each edge once, each edge leading to the starting point of a cycle is also covered. Thus, by using DFS and back-edge checking it should indeed be possible to detect all cycles in a graph. Note that, if cycles with different numbers of participant nodes exist (e.g. triangles, rectangles etc.), additional work has to be done to discriminate the acutal "shape" of each cycle.

+1  A: 

If a traversal algorithm visits each edge only once, then it cannot find all cycles. An edge could be part of multiple cycles.

btw, what is a back-edge?

Also, perhaps you should rephrase/format your question. It is very hard to read.

Moron
A back-edge is an edge that goes from a node to one of its ancestors in a search tree. E.g. the edges (A,B), (B,C) and (C,A) of an undirected graph, then either (A,B) or (C,A) will be a back-edge (if root = A). It's not a tree edge of the search tree, and it is created when, during DFS-VISIT, an edge leads from a new node to an already visited (gray), but not yet finished, node.<br>To your first answer: If an edge is part of multiple cycles, then each cycle will be traversed once => all back-edges will be detected => all cycles should be detected AFAIK.<br>Please correct me if I'm wrong!
Shadow
Sorry, I have to correct myself and agree with you "Moron". I was limiting my "cycles" to triangles when exploring examples and focussing strongly on detecting those triangles. This means that the "algorithm" always followed the edges that directly lead to triangles, although this needs not to be the case. E.g. 2 triangles, sharing an edge (bigger cycle = rectangle), the rectangle might be discovered first as a cycle and the 2 triangles might be missed. And vice versa.
Shadow
A: 

I have already answered this thoroughly, so check this:

http://stackoverflow.com/questions/2568277/will-a-source-removal-sort-always-return-a-maximal-cycle/2588758#2588758

The relevant part of the answer:

Perform a Depth-First Search on your graph.

You are interested in recognizing back edges, i.e., in the traversal, an edge which points back to an ancestor (in the DFS tree, which is induced by edges of visiting nodes for the first time) of the visited node. For example, if the DFS stack has nodes [A->B->C->D] and while you explore D you find an edge D->B, that's a back edge. Each back edge defines a cycle.

More importantly, the cycles induced by back-edges are a basic set of cycles of the graph. "A basic set of cycles": you can construct all cycles of the graph just by UNIONing and XORing cycles of the basic set. For example, consider the cycles [A1->A2->A3->A1] and [A2->B1->B2->B3->A2]. You can union them to the cycle: [A1->A2->B1->B2->B3->A2->A3->A1].

Dimitris Andreou