Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.92 KB | None | 0 0
  1. package adt.heap;
  2.  
  3. import static org.junit.Assert.assertArrayEquals;
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertFalse;
  6. import static org.junit.Assert.assertTrue;
  7.  
  8. import java.util.Arrays;
  9. import java.util.Comparator;
  10.  
  11. import org.junit.Before;
  12. import org.junit.Test;
  13.  
  14. public class StudentMinHeapTest {
  15.  
  16.     Heap<Integer> heap;
  17.  
  18.     @Before
  19.     public void setUp() {
  20.         // TODO Instancie seu comparator para fazer sua estrutura funcionar como
  21.         // uma min heap aqui. Use instanciacao anonima da interface
  22.         // Comparator!!!!
  23.         Comparator<Integer> comparator = new Comparator<Integer>() {
  24.             @Override
  25.             public int compare(Integer o1, Integer o2) {
  26.                 return o2.compareTo(o1);
  27.             }
  28.         };
  29.         heap = new HeapImpl<Integer>(comparator);
  30.     }
  31.  
  32.     @Test
  33.     public void testBuild() {
  34.         heap.buildHeap(new Integer[] { 82, 6, 99, 12, 34, 64, 58, 1 });
  35.  
  36.         assertEquals(8, heap.size());
  37.         assertFalse(heap.isEmpty());
  38.  
  39.         verifyHeap(new Integer[] { 1, 6, 58, 12, 34, 64, 99, 82 });
  40.     }
  41.  
  42.     @Test
  43.     public void testInsert() {
  44.         heap.insert(8);
  45.         heap.insert(12);
  46.         heap.insert(-2);
  47.         heap.insert(7);
  48.         heap.insert(8);
  49.         heap.insert(-5);
  50.         heap.insert(14);
  51.         heap.insert(3);
  52.         heap.insert(-10);
  53.         heap.insert(0);
  54.  
  55.         assertEquals(10, heap.size());
  56.         assertFalse(heap.isEmpty());
  57.  
  58.         verifyHeap(new Integer[] { -10, -5, -2, 3, 0, 8, 14, 12, 7, 8 });
  59.     }
  60.  
  61.     @Test
  62.     public void testRemove() {
  63.         heap.insert(22);
  64.         heap.insert(45);
  65.         heap.insert(38);
  66.         heap.insert(17);
  67.         heap.insert(40);
  68.         heap.insert(15);
  69.         heap.insert(26);
  70.         heap.insert(79);
  71.         heap.insert(53);
  72.         heap.insert(30);
  73.  
  74.         assertEquals(new Integer(15), heap.extractRootElement());
  75.         assertEquals(new Integer(17), heap.extractRootElement());
  76.         assertEquals(new Integer(22), heap.extractRootElement());
  77.         assertEquals(new Integer(26), heap.extractRootElement());
  78.         assertEquals(new Integer(30), heap.extractRootElement());
  79.  
  80.         assertEquals(5, heap.size());
  81.         assertFalse(heap.isEmpty());
  82.  
  83.         verifyHeap(new Integer[] { 38, 40, 79, 45, 53 });
  84.     }
  85.  
  86.     @Test
  87.     public void testCustom() {
  88.         assertTrue(heap.isEmpty());
  89.         assertEquals(0, heap.size());
  90.  
  91.         heap.insert(0);
  92.         heap.insert(10);
  93.         heap.insert(20);
  94.         heap.insert(-1);
  95.         heap.insert(-2);
  96.         heap.insert(Integer.MAX_VALUE);
  97.         heap.insert(Integer.MIN_VALUE);
  98.         heap.insert(1);
  99.         heap.insert(0);
  100.         heap.insert(0);
  101.         heap.insert(12);
  102.         heap.insert(100);
  103.         heap.insert(109);
  104.         heap.insert(101);
  105.  
  106.         assertFalse(heap.isEmpty());
  107.         assertEquals(14, heap.size());
  108.         verifyHeap(new Integer[]{Integer.MIN_VALUE, -1, -2, 0, 0, 100, 20, 10, 1, 0, 12, Integer.MAX_VALUE, 109, 101});
  109.  
  110.         Integer[] array = new Integer[]{Integer.MAX_VALUE, 12, 109, 0, 1, 100, 101, -1, 0, -2, 0, 10, 20, Integer.MIN_VALUE};
  111.         Arrays.sort(array);
  112.  
  113.         assertArrayEquals(array, heap.heapsort(new Integer[]{Integer.MAX_VALUE, 12, 109, 0, 1, 100, 101, -1, 0, -2, 0, 10, 20, Integer.MIN_VALUE}));
  114.         assertTrue(heap.isEmpty());
  115.         verifyHeap(new Integer[]{});
  116.  
  117.         assertArrayEquals(new Integer[]{1, 2, 3, 4}, heap.heapsort(new Integer[]{4, 2, 3, 1}));
  118.         assertTrue(heap.isEmpty());
  119.         verifyHeap(new Integer[]{});
  120.  
  121.         heap.insert(0);
  122.         heap.insert(10);
  123.         heap.insert(20);
  124.         heap.insert(-1);
  125.         heap.insert(-2);
  126.         heap.insert(Integer.MAX_VALUE);
  127.         heap.insert(Integer.MIN_VALUE);
  128.         heap.insert(1);
  129.         heap.insert(0);
  130.         heap.insert(0);
  131.         heap.insert(12);
  132.         heap.insert(100);
  133.         heap.insert(109);
  134.         heap.insert(101);
  135.         heap.insert(null);
  136.         assertEquals(14, heap.size());
  137.         heap.insert(null);
  138.         assertEquals(14, heap.size());
  139.  
  140.         Comparator<Integer> comparator = new Comparator<Integer>() {
  141.             @Override
  142.             public int compare(Integer o1, Integer o2) {
  143.                 return o2.compareTo(o1);
  144.             }
  145.         };
  146.  
  147.         ((HeapImpl)heap).setComparator(comparator);
  148.  
  149.         assertFalse(heap.isEmpty());
  150.         assertEquals(14, heap.size());
  151.         verifyHeap(new Integer[]{Integer.MAX_VALUE, 12, 109, 0, 1, 100, 101, -1, 0, -2, 0, 10, 20, Integer.MIN_VALUE});
  152.     }
  153.  
  154.     @Test
  155.     public void testSort() {
  156.         assertArrayEquals(new Integer[] { 5, 6, 12, 20, 34, 43, 49, 92 },
  157.                 heap.heapsort(new Integer[] { 34, 92, 5, 12, 49, 20, 43, 6 }));
  158.  
  159.         assertEquals(0, heap.size());
  160.         assertTrue(heap.isEmpty());
  161.  
  162.         assertArrayEquals(new Integer[] {}, heap.toArray());
  163.     }
  164.  
  165.     private void verifyHeap(Integer[] expected) {
  166.         boolean isHeap = true;
  167.  
  168.         Comparable<Integer>[] original = heap.toArray();
  169.  
  170.         Arrays.sort(expected);
  171.         Arrays.sort(original);
  172.  
  173.         if (Arrays.equals(expected, original) == false)
  174.             isHeap = false;
  175.  
  176.         original = heap.toArray();
  177.  
  178.         for (int i = 0; i < original.length; i++) {
  179.             if (2 * i + 1 < original.length && original[i].compareTo((Integer) original[2 * i + 1]) > 0)
  180.                 isHeap = false;
  181.             if (2 * i + 2 < original.length && original[i].compareTo((Integer) original[2 * i + 2]) > 0)
  182.                 isHeap = false;
  183.         }
  184.  
  185.         assertTrue(isHeap);
  186.     }
  187.  
  188.  
  189.     @Test
  190.     public void testShortestPath(){
  191.  
  192.         int INF = 10233912;
  193.  
  194.         Comparator<Pair> comp = new Comparator<Pair>() {
  195.             @Override
  196.             public int compare(Pair o1, Pair o2) {
  197.                 return o2.compareTo(o1);
  198.             }
  199.         };
  200.  
  201.         Heap<Pair> pq = new HeapImpl<>(comp);
  202.  
  203.         int[][] graph = new int[][]{{0,5,28,0,0,3,0},{0,0,0,7,11,0,0}, {15,0,0,0,0,0,19},{0,0,0,0,0,0,30},
  204.                 {9,0,0,0,0,2,0}, {0,0,0,0,0,0,91}, {0,0,0,0,0,7,0}};
  205.         int[] dist = new int[]{INF,INF,INF,INF,INF,INF,INF};
  206.         int numberOfVertices = 7;
  207.  
  208.         int start = 0;
  209.         int end = 6;
  210.         int ans = 42;
  211.  
  212.         pq.insert(new Pair(0,0));
  213.         dist[start] = 0;
  214.  
  215.         while(!pq.isEmpty()){
  216.  
  217.  
  218.             int vertex = pq.rootElement().left;
  219.             int distSoFar = pq.rootElement().right;
  220.             pq.extractRootElement();
  221.  
  222.             for(int i = 0 ; i < numberOfVertices ; i++){
  223.  
  224.                 if(graph[vertex][i] == 0) continue;
  225.  
  226.                 if(distSoFar + graph[vertex][i] < dist[i]) {
  227.                     dist[i] = distSoFar + graph[vertex][i];
  228.                     pq.insert(new Pair(i, dist[i]));
  229.                 }
  230.  
  231.             }
  232.  
  233.         }
  234.  
  235.         assertEquals(ans, dist[end]);
  236.         assertEquals(16, dist[4]);
  237.         assertEquals(3, dist[5]);
  238.         assertEquals(28, dist[2]);
  239.         assertEquals(16, dist[4]);
  240.  
  241.  
  242.     }
  243.  
  244.     @Test
  245.     public void testKruskal(){
  246.  
  247.         int INF = 10233912;
  248.  
  249.         Comparator<PairTwo> comp = new Comparator<PairTwo>() {
  250.             @Override
  251.             public int compare(PairTwo o1, PairTwo o2) {
  252.                 return o2.compareTo(o1);
  253.             }
  254.         };
  255.  
  256.         Heap<PairTwo> pq = new HeapImpl<>(comp);
  257.  
  258.         int[][] graph = new int[][]{{0,7,0,5,0,0,0},{7,0,8,9,7,0,0}, {0,8,0,0,5,0,0},{5,9,0,0,15,6,0},
  259.                 {0,7,5,15,0,8,9}, {0,0,0,6,8,0,11}, {0,0,0,0,9,11,0}};
  260.  
  261.         int total = 0;
  262.  
  263.         for(int i = 0 ; i < 7 ; i++){
  264.             for(int j = 0 ; j < 7 ; j++){
  265.                 if(graph[i][j] != 0) {
  266.                     total += graph[i][j];
  267.                     pq.insert(new PairTwo(i, j, graph[i][j]));
  268.                 }
  269.             }
  270.         }
  271.  
  272.         total /= 2;
  273.         int ans = 0;
  274.         UnionFind uf = new UnionFind(7);
  275.         while(!pq.isEmpty()){
  276.             int from = pq.rootElement().from;
  277.             int to = pq.rootElement().to;
  278.             int peso = pq.rootElement().peso;
  279.             pq.extractRootElement();
  280.  
  281.             if(!uf.find(from,to)){
  282.                 ans += peso;
  283.                 uf.uni(from,to);
  284.                 uf.uni(to,from);
  285.             }
  286.         }
  287.  
  288.         assertEquals(51,total - ans);
  289.     }
  290.  
  291.     static class Pair implements Comparable<Pair>{
  292.         public int left;
  293.         public int right;
  294.         public Pair(int a, int b){
  295.             left = a;
  296.             right = b;
  297.         }
  298.         @Override
  299.         public boolean equals(Object other){
  300.             if(other instanceof Pair){
  301.                 return ((Pair) other).left == this.left && ((Pair) other).right == this.right;
  302.             }
  303.             return false;
  304.         }
  305.  
  306.         @Override
  307.         public int compareTo(Pair other) {
  308.             if (other.left == this.left)
  309.                 if (other.right == this.right) return 0;
  310.                 else if (this.right > other.right) return 1;
  311.                 else return -1;
  312.  
  313.             if (this.left > other.left) return 1;
  314.             else return -1;
  315.         }
  316.     }
  317.  
  318.     static class PairTwo implements Comparable<PairTwo>{
  319.         public int from;
  320.         public int to;
  321.         public int peso;
  322.         public PairTwo(int a, int b, int p){
  323.             from = a;
  324.             to = b;
  325.             peso = p;
  326.         }
  327.         @Override
  328.         public boolean equals(Object other){
  329.             if(other instanceof PairTwo){
  330.                 return ((PairTwo) other).to == this.to && ((PairTwo) other).from == this.from;
  331.             }
  332.             return false;
  333.         }
  334.  
  335.         @Override
  336.         public int compareTo(PairTwo other) {
  337.             if(other.peso == this.peso) return 0;
  338.             if(other.peso > this.peso) return -1;
  339.             return 1;
  340.         }
  341.     }
  342.  
  343.     static class UnionFind{
  344.  
  345.         int []ar;
  346.         int []tree_size;
  347.         int n;
  348.  
  349.         public UnionFind(int tam){
  350.             n = tam;
  351.             ar = new int[n+1];
  352.             tree_size = new int[n+1];
  353.             for(int i = 0 ; i <= n ; i++){
  354.                 tree_size[i] = 1;
  355.                 ar[i] = i;
  356.             }
  357.         }
  358.  
  359.     int root(int i){
  360.         while(i != ar[i]){
  361.             ar[i] = ar[ar[i]]; // Comprimindo a arvore
  362.             i = ar[i];
  363.         }
  364.         return i;
  365.     }
  366.  
  367.     void uni(int a , int b){
  368.         int i = root(a);
  369.         int j = root(b);
  370.         if(tree_size[i] < tree_size[j]){
  371.             ar[i] = j;
  372.             tree_size[j] += tree_size[i];
  373.         }
  374.         else{
  375.             ar[j] = i;
  376.             tree_size[i] += tree_size[j];
  377.         }
  378.     }
  379.  
  380.     boolean find(int a,int b){
  381.         return root(a) == root(b);
  382.     }
  383.  
  384. };
  385.  
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement