Skip to content

Commit

Permalink
Add PR2.java
Browse files Browse the repository at this point in the history
  • Loading branch information
seanprashad committed Apr 15, 2021
1 parent dc9262d commit f19b536
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Miscellaneous/PR2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.*;

public class PR2 {

public static boolean detectCycle(int[][] edgeList) {
if (edgeList.length == 0) {
return false;
}

Map<Integer, Set<Integer>> graph = new HashMap<>();
int[] inorder = new int[edgeList.length];

for (int[] edge : edgeList) {
graph.putIfAbsent(edge[0], new HashSet<>());
graph.get(edge[0]).add(edge[1]);

inorder[edge[1]]++;
}

Queue<Integer> q = new LinkedList<>();

for (int i = 0; i < inorder.length; i++) {
if (inorder[i] == 0) {
q.offer(i);
}
}

int nodeCount = 0;

while (!q.isEmpty()) {
int key = q.poll();
++nodeCount;

if (!graph.containsKey(key)) {
continue;
}

for (int neighbour : graph.get(key)) {
inorder[neighbour]--;

if (inorder[neighbour] == 0) {
q.offer(neighbour);
}
}
}

return edgeList.length - nodeCount > 1;
}

public static void main(String[] args) {
int[][] edgeList1 = new int[][] { { 0, 1 }, { 1, 3 }, { 2, 3 }, { 1, 2 }, { 4, 1 }, { 0, 4 }, { 1, 3 } };
int[][] edgeList2 = new int[][] { { 0, 1 }, { 1, 3 }, { 2, 3 }, { 3, 4 }, { 1, 2 }, { 4, 1 }, { 0, 4 } };

System.out.println(detectCycle(edgeList1)); // no cycle - return false
System.out.println(detectCycle(edgeList2)); // cycle present - return true

return;
}
}

0 comments on commit f19b536

Please sign in to comment.