publicvoidaddEgde(int source, int destination){ Edge edge = new Edge(source, destination); allEdges.add(edge); // add to total edges }
publicvoidmakeSet(int[] parent){ // Make set- creating a new element with a parent pointer to itself. for (int i = 0; i < vertices; i++) { parent[i] = i; } }
publicintfind(int[] parent, int vertex){ // chain of parent pointers from x upwards through the tree // until an element is reached whose parent is itself if (parent[vertex] != vertex) return find(parent, parent[vertex]); return vertex; }
publicvoidunion(int[] parent, int x, int y){ int x_set_parent = find(parent, x); int y_set_parent = find(parent, y); // make x as parent of y parent[y_set_parent] = x_set_parent; }
publicbooleanisCycle(){ // create a parent [] int[] parent = newint[vertices]; // makeset makeSet(parent); // iterate through all the edges and keep making the sets for (int i = 0; i < allEdges.size(); i++) { Edge edge = allEdges.get(i); int x_set = find(parent, edge.source); int y_set = find(parent, edge.destination); // check if source vertex and destination vertex belongs to the same set // if in same set then cycle has been detected else combine them into one set if (x_set == y_set) returntrue; else union(parent, x_set, y_set); } // if here, means cycle was not found returnfalse; } } } /* public class MyClass { public static void main(String[] args) { int vertices = 6; DisjointSet.Graph graph = new DisjointSet.Graph(vertices); graph.addEgde(0, 1); graph.addEgde(0, 2); graph.addEgde(1, 3); graph.addEgde(3, 4); graph.addEgde(2, 3); graph.addEgde(4, 5); System.out.println("Graph contains cycle: " + graph.isCycle()); } }*/