Below algo is failing after stack overflow exception. Please let me know how can i correct it for cycle detection in Directed Graph or if possible can somebody provide algo based on stack instead of recursion.
public boolean hasCycle(Graphnode<T> n) {
n.setMark(IN_PROGRESS);
for (Graphnode<T> m : n.getSuccessors()) {
if (m.getMark() == IN_PROGRESS) {
return true;
}
if (m.getMark() != DONE) {
if (hasCycle(m)) {
return true;
}
}
}
n.setMark(DONE);
return false;
}
Thanks, Vikrant