From db9edfe1fc02c07329a6d3bc6ebb329627e6339d Mon Sep 17 00:00:00 2001 From: JOY SARKAR <32029022+joy98@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:04:21 +0530 Subject: [PATCH] Add files via upload --- Java/CycleInDirectedGraph.java | 67 +++++++++++++++++++++++++++++ Java/CycleInUndirectedGraph.java | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 Java/CycleInDirectedGraph.java create mode 100644 Java/CycleInUndirectedGraph.java diff --git a/Java/CycleInDirectedGraph.java b/Java/CycleInDirectedGraph.java new file mode 100644 index 0000000..3e05291 --- /dev/null +++ b/Java/CycleInDirectedGraph.java @@ -0,0 +1,67 @@ +import java.util.HashSet; +import java.util.Set; + +/** + * http://www.geeksforgeeks.org/detect-cycle-in-a-graph/ + */ +public class CycleInDirectedGraph { + + public boolean hasCycle(Graph graph) { + Set> whiteSet = new HashSet<>(); + Set> graySet = new HashSet<>(); + Set> blackSet = new HashSet<>(); + + for (Vertex vertex : graph.getAllVertex()) { + whiteSet.add(vertex); + } + + while (whiteSet.size() > 0) { + Vertex current = whiteSet.iterator().next(); + if(dfs(current, whiteSet, graySet, blackSet)) { + return true; + } + } + return false; + } + + private boolean dfs(Vertex current, Set> whiteSet, + Set> graySet, Set> blackSet ) { + //move current to gray set from white set and then explore it. + moveVertex(current, whiteSet, graySet); + for(Vertex neighbor : current.getAdjacentVertexes()) { + //if in black set means already explored so continue. + if (blackSet.contains(neighbor)) { + continue; + } + //if in gray set then cycle found. + if (graySet.contains(neighbor)) { + return true; + } + if(dfs(neighbor, whiteSet, graySet, blackSet)) { + return true; + } + } + //move vertex from gray set to black set when done exploring. + moveVertex(current, graySet, blackSet); + return false; + } + + private void moveVertex(Vertex vertex, Set> sourceSet, + Set> destinationSet) { + sourceSet.remove(vertex); + destinationSet.add(vertex); + } + + public static void main(String args[]){ + Graph graph = new Graph<>(true); + graph.addEdge(1, 2); + graph.addEdge(1, 3); + graph.addEdge(2, 3); + graph.addEdge(4, 1); + graph.addEdge(4, 5); + graph.addEdge(5, 6); + graph.addEdge(6, 4); + CycleInDirectedGraph cdg = new CycleInDirectedGraph(); + System.out.println(cdg.hasCycle(graph)); + } +} \ No newline at end of file diff --git a/Java/CycleInUndirectedGraph.java b/Java/CycleInUndirectedGraph.java new file mode 100644 index 0000000..5cc0936 --- /dev/null +++ b/Java/CycleInUndirectedGraph.java @@ -0,0 +1,74 @@ +import java.util.HashSet; +import java.util.Set; + + +public class CycleUndirectedGraph { + + public boolean hasCycleUsingDisjointSets(Graph graph){ + DisjointSet disjointSet = new DisjointSet(); + + for(Vertex vertex : graph.getAllVertex()){ + disjointSet.makeSet(vertex.getId()); + } + + for(Edge edge : graph.getAllEdges()){ + long parent1 = disjointSet.findSet(edge.getVertex1().getId()); + long parent2 = disjointSet.findSet(edge.getVertex2().getId()); + if(parent1 == parent2){ + return true; + } + disjointSet.union(edge.getVertex1().getId(), edge.getVertex2().getId()); + } + return false; + } + + public boolean hasCycleDFS(Graph graph){ + Set> visited = new HashSet>(); + for(Vertex vertex : graph.getAllVertex()){ + if(visited.contains(vertex)){ + continue; + } + boolean flag = hasCycleDFSUtil(vertex, visited, null); + if(flag){ + return true; + } + } + return false; + } + + public boolean hasCycleDFSUtil(Vertex vertex, Set> visited,Vertex parent){ + visited.add(vertex); + for(Vertex adj : vertex.getAdjacentVertexes()){ + if(adj.equals(parent)){ + continue; + } + if(visited.contains(adj)){ + return true; + } + boolean hasCycle = hasCycleDFSUtil(adj,visited,vertex); + if(hasCycle){ + return true; + } + } + return false; + } + + public static void main(String args[]){ + + CycleUndirectedGraph cycle = new CycleUndirectedGraph(); + Graph graph = new Graph(false); + + graph.addEdge(0, 1); + graph.addEdge(1, 2); + graph.addEdge(0, 3); + graph.addEdge(3, 4); + graph.addEdge(4, 5); + graph.addEdge(5, 1); + boolean isCycle = cycle.hasCycleDFS(graph); + System.out.println(isCycle); + isCycle = cycle.hasCycleUsingDisjointSets(graph); + System.out.print(isCycle); + + } + +} \ No newline at end of file