Guest User

Untitled

a guest
Feb 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class DisjointSet {
  4. static class Edge{
  5. int source;
  6. int destination;
  7.  
  8. public Edge(int source, int destination) {
  9. this.source = source;
  10. this.destination = destination;
  11. }
  12. }
  13.  
  14. static class Graph{
  15. int vertices;
  16. LinkedList<Edge>[] adjList;
  17. ArrayList<Edge> allEdges = new ArrayList<>();
  18.  
  19. Graph(int vertices){
  20. this.vertices = vertices;
  21. adjList = new LinkedList[vertices];
  22. for (int i = 0; i <vertices ; i++) {
  23. adjList[i] = new LinkedList<>();
  24. }
  25. }
  26. public void addEgde(int source, int destination){
  27. Edge edge = new Edge(source, destination);
  28. adjList[source].addFirst(edge);
  29. allEdges.add(edge); //add to total edges
  30. }
  31.  
  32. public void makeSet(int [] parent){
  33. //Make set- creating a new element with a parent pointer to itself.
  34. for (int i = 0; i <vertices ; i++) {
  35. parent[i] = i;
  36. }
  37. }
  38.  
  39. public int find(int [] parent, int vertex){
  40. //chain of parent pointers from x upwards through the tree
  41. // until an element is reached whose parent is itself
  42. if(parent[vertex]!=vertex)
  43. return find(parent, parent[vertex]);;
  44. return vertex;
  45. }
  46.  
  47. public void union(int [] parent, int x, int y){
  48. int x_set_parent = find(parent, x);
  49. int y_set_parent = find(parent, y);
  50. //make x as parent of y
  51. parent[y_set_parent] = x_set_parent;
  52. }
  53.  
  54. public void disjointSets(){
  55. //create a parent []
  56. int [] parent = new int[vertices];
  57.  
  58. //makeset
  59. makeSet(parent);
  60.  
  61. //iterate through all the edges and keep making the sets
  62. for (int i = 0; i <allEdges.size() ; i++) {
  63. Edge edge = allEdges.get(i);
  64. int x_set = find(parent, edge.source);
  65. int y_set = find(parent, edge.destination);
  66.  
  67. //check if source vertex and destination vertex belongs to the same set
  68. // if in same set then cycle has been detected else combine them into one set
  69. if(x_set==y_set) {
  70. //do nothing
  71. }else
  72. union(parent, x_set, y_set);
  73. }
  74. printSets(parent);
  75. }
  76.  
  77. public void printSets(int [] parent){
  78. //Find different Sets
  79. HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
  80. for (int i = 0; i <parent.length ; i++) {
  81. if(map.containsKey(parent[i])){
  82. ArrayList<Integer> list = map.get(parent[i]);
  83. list.add(i);//add vertex
  84. map.put(parent[i],list);
  85. }else{
  86. ArrayList<Integer> list = new ArrayList<>();
  87. list.add(i);
  88. map.put(parent[i],list);
  89. }
  90. }
  91. //Print the Different sets
  92. Set<Integer> set = map.keySet();
  93. Iterator<Integer> iterator = set.iterator();
  94. while(iterator.hasNext()){
  95. int key = iterator.next();
  96. System.out.println("Set Id: " + key + " elements: " + map.get(key));
  97. }
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102. int vertices = 6;
  103. Graph graph = new Graph(vertices);
  104. graph.addEgde(0, 1);
  105. graph.addEgde(0, 2);
  106. graph.addEgde(1, 3);
  107. graph.addEgde(4, 5);
  108. graph.disjointSets();
  109. }
  110. }
Add Comment
Please, Sign In to add comment