Skip to content

Commit 5f4015d

Browse files
committed
Cycle detection in Directed and undirected graph
1 parent bcfde1a commit 5f4015d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.ArrayList;
2+
3+
4+
// check whether the graph is DAG, directed acyclic(without cycles) graph
5+
public class cycleDetectionInDirectedGraph {
6+
private boolean checkCycle(int node, ArrayList<ArrayList<Integer>> adj, int[] visited, int dfsVisited[]) {
7+
visited[node] = 1;
8+
dfsVisited[node] = 1;
9+
for (Integer child : adj.get(node)) {
10+
if (visited[child] == 0) {
11+
if (checkCycle(child, adj, visited, dfsVisited))
12+
return true;
13+
} else if (dfsVisited[child] == 1)
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
public boolean isCyclic(int N, ArrayList<ArrayList<Integer>> adjlist, int n) {
20+
int visited[] = new int[N];
21+
int dfsVisited[] = new int[N];
22+
for (int i = 0; i < n; i++) {
23+
if (visited[i] == 0) {
24+
if (checkCycle(i, adjlist, visited, dfsVisited) == true) {
25+
return true;
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
// CORE: if children is already visited and
7+
// is not the parent of the node then there isa cycle
8+
public class cycledetectionInUndirectedGraph {
9+
private boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
10+
boolean[] visited = new boolean[V + 1];
11+
Arrays.fill(visited, false);
12+
for (int i = 1; i <= V; i++) {
13+
if (!visited[i]) {
14+
if (checkForCycle(adj, i, visited))
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
static boolean checkForCycle(ArrayList<ArrayList<Integer>> adj, int s, boolean visited[]) {
22+
Queue<Node> q = new LinkedList<>(); // bfs queue
23+
q.add(new Node(s, -1)); // add source node parent of start Node is -1 <-- 0
24+
visited[s] = true;
25+
while (!q.isEmpty()) {
26+
int node = q.peek().self;
27+
int parent = q.peek().Parent;
28+
q.remove();
29+
for (Integer child : adj.get(node)) {
30+
if (!visited[child]) {
31+
q.add(new Node(child, node));
32+
visited[child] = true;
33+
} else if (child != parent) { // child is visited and is not parent, then cycle
34+
return true;
35+
}
36+
}
37+
}
38+
return false;
39+
40+
}
41+
42+
// driver code
43+
public static void main(String[] args) {
44+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
45+
adj.add(new ArrayList<>(Arrays.asList(1, 2)));
46+
adj.add(new ArrayList<>(Arrays.asList(2, 3)));
47+
adj.add(new ArrayList<>(Arrays.asList(3, 4)));
48+
// adj.add(new ArrayList<>(Arrays.asList(4, 5)));
49+
cycledetectionInUndirectedGraph obj = new cycledetectionInUndirectedGraph();
50+
System.out.println(obj.isCycle(4, adj));
51+
}
52+
}
53+
54+
class Node {
55+
int self;
56+
int Parent;
57+
58+
public Node(int self, int Parent) {
59+
this.self = self;
60+
this.Parent = Parent;
61+
}
62+
}
63+
64+
class cycleDetectionUsingDSU{
65+
66+
}

0 commit comments

Comments
 (0)