Advertisement
rfop

ConjuntosDisjuntos

Apr 5th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4.  
  5. public class Sets {
  6.     /*
  7.      * This class implements the union-find data structure
  8.      * with union by rank and find with path compression
  9.      */
  10.      
  11.     private int[] parent;
  12.     private int[] rank;
  13.      
  14.     public Sets(int n){
  15.         parent = new int[n];
  16.         for(int i=0; i<n; i++) parent[i] = i;
  17.         rank = new int[n];
  18.         for(int i=0; i<n; i++) rank[i] = 0;
  19.     }
  20.      
  21.     public int find(int v){
  22.         if(v!=parent[v]){
  23.             parent[v] = find(parent[v]);
  24.         }
  25.         return parent[v];
  26.     }
  27.      
  28.     public void union(int x, int y){
  29.         int xRoot = find(x);
  30.         int yRoot = find(y);
  31.         if(xRoot == yRoot) return;
  32.         if(rank[xRoot] > rank[yRoot]){
  33.             parent[yRoot] = xRoot;
  34.         }
  35.         else{
  36.             parent[xRoot] = yRoot;
  37.             if(rank[xRoot] == rank[yRoot]){
  38.                 rank[yRoot]++;
  39.             }
  40.         }
  41.     }
  42.      
  43.     public void printParent(){
  44.         int[] index = new int[parent.length];
  45.         for(int i=0; i<parent.length; i++) index[i] = i;
  46.         System.out.println("index:  " + Arrays.toString(index));
  47.         System.out.println("parent: " + Arrays.toString(parent));
  48.     }
  49.      
  50.      
  51.     public static void main(String[] args) {
  52.         // Part a)
  53.         Sets uf = new Sets(9);
  54.         uf.union(2,1);
  55.         uf.union(4,3);
  56.         uf.union(6,5);
  57.         System.out.println("Parent array after union(2,1), union(4,3) and union(6,5):");
  58.         uf.printParent();
  59.          
  60.         // Part b)
  61.         uf.union(2, 4);;
  62.         System.out.println("\nParent array after union(2,1)");
  63.         uf.printParent();
  64.          
  65.         // Part c)
  66.         uf.find(2);
  67.         System.out.println("\nParent array after find(2)");
  68.         uf.printParent();
  69.          
  70.         // Part d)
  71.         System.out.println("\nDisjoint sets: ");
  72.         HashMap<Integer, HashSet<Integer>> myDict = new HashMap<Integer, HashSet<Integer>>();
  73.         for(int node=0; node<9; node++){
  74.             int root = uf.find(node);
  75.             if(!myDict.containsKey(root)){
  76.                 HashSet<Integer> mySet = new HashSet<Integer>();
  77.                 mySet.add(node);
  78.                 myDict.put(root, mySet);
  79.             }
  80.             else{
  81.                 myDict.get(root).add(node);
  82.             }
  83.         }
  84.          
  85.         for(HashSet<Integer> mySet : myDict.values()){
  86.             System.out.println(mySet);
  87.         }
  88.          
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement