Find all cycles in graph, redux
Posted
by Shadow
on Stack Overflow
See other posts from Stack Overflow
or by Shadow
Published on 2010-05-15T11:24:52Z
Indexed on
2010/05/15
11:34 UTC
Read the original article
Hit count: 358
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?
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.
© Stack Overflow or respective owner