Skip to content

Commit

Permalink
added code to check for cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackkeenan99 committed Nov 10, 2020
1 parent c08f554 commit da0798e
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions LCA.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;

public class LCA {

//here
Expand Down Expand Up @@ -72,12 +75,73 @@ public boolean search (Node root, int n1) {
}
else return false;
}











public static boolean hasCycles(DirectedGraph g) {
boolean[] visited = new boolean[g.V];
boolean[] stack = new boolean[g.V];

for (int i = 0; i < g.V; i++) {
if (isCyclicRecursive(i, visited, stack, g)) {
return true;
}
}
return false;

}
// Recursive util method which traverses graph checking for cycles
private static boolean isCyclicRecursive(int i, boolean[] visited, boolean[] stack, DirectedGraph graph) {
if (stack[i]) {
return true;
}
if (visited[i]) {
return false;
}

visited[i] = true;
stack[i] = true;

List<Integer> children = graph.adj.get(i);

for (Integer c : children) {
if (isCyclicRecursive(c, visited, stack, graph)) {
return true;
}
}

stack[i] = false;

return false;
}
















/*
public static void main(String[] args) {
LCA tree = new LCA();
/* Let us create following BST
Let us create following BST
50
/ \
30 70
Expand All @@ -92,7 +156,7 @@ public static void main(String[] args) {
tree.insert(40);
tree.insert(70);
tree.insert(n1);
tree.insert(n2); */
tree.insert(n2);
int n1 = 5;
int n2 = 14;
Expand All @@ -107,7 +171,7 @@ public static void main(String[] args) {
Node t = tree.lca(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2 + " is " + t.key);
}
} */
}


Expand Down

0 comments on commit da0798e

Please sign in to comment.