6

Is there a way to find all vertices that are a part of a simple cycle in a graph in a linear time?

I found a way to do it in O(|V|(|V|+|E|)) time but was wondering if there is a way to do it faster?

Amnon
  • 195
  • 3
  • 12
  • Possible [duplicate](http://cs.stackexchange.com/questions/7216/find-the-simple-cycles-in-a-directed-graph)? – Peter de Rivaz Jul 05 '13 at 10:13
  • I don't understand what exactly you're asking for. Are you given a simple cycle and must find the vertices on it? How is that cycle specified? Are you given a graph, and must filter out any vertices not in some cycle somewhere? – Craig Gidney Jul 05 '13 at 20:42
  • given a graph G=(V,E), find a group of vertices U so that every vertex u that belongs to U is a vertex on a simple cycle – Amnon Jul 07 '13 at 08:00

4 Answers4

11

What you want to do is remove all of the Bridges (i.e. edges that disconnect a component when removed). The Wikipedia article gives a linear time algorithm for finding all of the bridges.

Once all the bridges are gone, each node is either isolated (has degree 0) or is part of a cycle. Throw out the lonely nodes, and what's left is the nodes you want.

Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
3

Using DFS (Depth First Search), you can do in O(|V|+|E|) time. while implementing the DFS just keep the record of the vertives you had been tracking with their parent node as soon as you find child node same as parent node(meaning the completion of cycle) you can declare all the nodes from that parent till the last child as the part of some simple cycle.

Comment below if you need more explanation.

  • This isn't a **simple cycle**, this is just a **cycle**. Simple cycle should not contain any other cycle. – Saeed Amiri Jul 05 '13 at 11:53
  • This was the algorithm I had in mind but I think that if very time I find a vertex that have been visited track all its predecessors, I can have |V| circles (a circle that's part of a circle that is part of a circle etc.) and for each one O(|V|) time for this backtrack. which sums to O(|V|(|V|+|E|)) – Amnon Jul 05 '13 at 11:58
  • @SaeedAmiri: "Simple cycle" usually means a cycle in which no vertex appears more than once. The standard term for what you described is "induced cycle". – j_random_hacker Jun 07 '18 at 14:39
0

I like Bhavya's answer: but it'll help if I elaborate on it.

Usually if you do DFS you have a visited array that holds either visited/not-visited state.

Now have

int visited[N];//N is the number of nodes/vertices in graph

Let

visited[i] =-1 if i-th vertex is not yet visited
visited[i] = 0 if i-th vertex is being processed
visited[i] = 1 if processing of i-th vertex is done

This way if you encounter a vertex along dfs with visited value 0, it means you have a cycle.To find vertices on cycle, use a predecessor array to store previous vertex in path.

Aravind
  • 3,169
  • 3
  • 23
  • 37
0

Ok, I think I have the answer. while processing DFS, for every vertex v calculate low(v) (explained below). then running DFS once again and check for every vertex v:

if low(v) != d(v) (where d(v) is the distance from the DFS tree root)

vertex v is a part of a simple cycle.

*low(v) = min ( d(v),d(u),low(w)) where (v,u) is a back edge and w is a child of v in the DFS tree. calculated in O(|V|+|E|) time.

Amnon
  • 195
  • 3
  • 12