I came across this SO post where it is suggested that cycle detection using DFS in a directed graph is faster because of backtracking. Here I quote from that link:
Depth first search is more memory efficient than breadth first search as you can backtrack sooner. It is also easier to implement if you use the call stack but this relies on the longest path not overflowing the stack.
Also if your graph is directed then you have to not just remember if you have visited a node or not, but also how you got there. Otherwise you might think you have found a cycle but in reality all you have is two separate paths A->B but that doesn't mean there is a path B->A. With a depth first search you can mark nodes as visited as you descend and unmark them as you backtrack.
Why is backtracking essential?
Can someone explain with an example graph as what is meant in the given A->B
example?
Finally, I have a DFS
code for cycle detection in directed graph that does not use backtracking but still detects cycle in O(E+V)
.
public boolean isCyclicDirected(Vertex v){
if (v.isVisited) {
return true;
}
v.setVisited(true);
Iterator<Edge> e = v.adj.iterator();
while (e.hasNext()) {
Vertex t = e.next().target;
// quick test:
if (t.isVisited) {
return true;
}
// elaborate, recursive test:
if (isCyclicDirected(t)) {
return true;
}
}
// none of our adjacent vertices flag as cyclic
v.setVisited(false);
return false;
}
if (t.isVisited) { return true; }
is uneccessary because when isCyclicDirected(t) is called the first thing it will do is check if the Vertex passed in has been visited. – Lostv.setVisited(false);
right before thereturn false;
I think it would be! :) – Lost