Elimination
Elimination

Reputation: 2723

Detecting a cycle in a directed graph

I read a discussion here, at SO about finding a cycle in a directed graph. Now, the OP claims that we need to validate two things:

  1. There's a back edge from u to v
  2. v is in the recursion-stack

Why do we need the second test? Could you give an example to demonstrate it's necessity?

Upvotes: 3

Views: 3345

Answers (3)

A. Sarid
A. Sarid

Reputation: 3996

Well, you probably got confused between the definition of a back-edge in a directed graph and a back-edge in an undirected graph. Yes, they are different.

While in undirected graph back edges are edges from the current vertex to an-already-visited vertex. (as OP from your link mentioned).
In directed graph the definition for back edge is different. A back edge in a directed graph is an edge from current vertex to a GREY vertex (the DFS for this vertex has started but not yet finished), meaning it is still in the recursion stack.

So if you take the definition of a back edge as it is in a directed graph then yes, it is enough for detecting a cycle.
But if you take the definition of a back edge as it is in an undirected graph then you will need also to make sure that v is in the recursion stack in order to detect a cycle.

See this and this for more information and examples.

Example:
Consider the DFS visit order to be A -> B -> C.
In this example, the edge <A,C> is a back edge in the undericted graph (as C was already visited).
But it is not a back edge in this directed graph - C was already visited but is not in the recursion stack, meaning it is not a cycle. enter image description here

Upvotes: 5

D. Law.
D. Law.

Reputation: 244

The second test is needed when it is a cross edge, not a back edge. A cross edge refers to an edge that goes from one vertex to an already visited one regardless of position. A back edge refers to an edge that points to an ancestor of the starting vertex, one that is still in the recursive stack. In terms of how the question was asked, the OP refers to a back edge as an edge that points to another already visited edge, but a more accurate explanation would be a cross edge. Knowing it is a back edge is sufficient because it implies the second step. Those steps are required when the first is a cross edge because the second proves the cross edge is a back edge. In a directed graph, a cross edge does not always mean that a loop occurs. Here is an example:

vertices a,b,c,d
a->b
a->c
b->d
d->c

Depending on the order that this is processed, d->c can be considered a cross edge, therefore step number 2 would be required to detect a loop. Unfortunately, back edge and cross edge are mixed up quite often causing confusion like this. Heres a link to another description of the difference between the two, Depth-First Search.

Upvotes: 1

user2512323
user2512323

Reputation:

Only the fact that we already visited v is not sufficient. It allows us to go from u to v, but not from v to u.

Simple graphical counterexample:

Counterexample

Numbers are traversal order. We have a back edge from 4 to 3, but we don't have any cycles.

Upvotes: 1

Related Questions