jTruBela

RodCut Data Structure

Nov 1st, 2021 (edited)
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. /*****************************************************************
  2.  *
  3.  * CSIT212 - RodCut.java        Justin Trubela      10/25/21
  4.  *
  5.  * Purpose: Implement all the parts of RodCutting algorithm
  6.  *
  7.  * •Task 1 (40 pts). Implement the memoized cut rod()
  8.  * and memoized cut rod aux() function as discussed in Lecture 9.
  9.  * •Task 2 (40 pts). Implement the bottom up cut rod()
  10.  * function as discussed in Lecture 9.
  11.  * •Task 3 (20 pts). Implement the extended bottom up cut rod
  12.  * function as discussed in Lecture 9.
  13.  *
  14.  *****************************************************************/
  15.  
  16.  
  17. package rodCutting;
  18.  
  19. public class RodCut {
  20.     int rodSize;
  21.     int[] price;
  22.     int[] revenue;
  23.     int[] s;
  24.    
  25.     public RodCut () {
  26.         rodSize = 10;
  27.         price = new int[11];
  28.         price[0] = 0;
  29.         price[1] = 1;
  30.         price[2] = 5;
  31.         price[3] = 8;
  32.         price[4] = 9;
  33.         price[5] = 10;
  34.         price[6] = 17;
  35.         price[7] = 17;
  36.         price[8] = 20;
  37.         price[9] = 24;
  38.         price[10] = 30;
  39.     }
  40.    
  41.     public int memoized_cut_rod () {
  42.         // create a temporary array of size n+1 to store the information
  43.         //                          needed for the memoized cut rod aux method
  44.         int [] revenue = new int [rodSize+1];
  45.  
  46.         // for 11 times, set each value in array to the minimum value possible
  47.         for(int i = 0; i <= rodSize; i++) {
  48.             revenue[i] = Integer.MIN_VALUE;
  49.         }
  50.        
  51.         return memoized_cut_rod_aux (price, rodSize, revenue);
  52.     }
  53.    
  54.     public int memoized_cut_rod_aux (int price[], int rodSize, int revenue[]) {
  55.         int q = 0;
  56.        
  57.         if( revenue[rodSize] >= 0 ) {
  58.             return revenue[rodSize];
  59.         }
  60.         if (rodSize == 0) {
  61.             q = 0;
  62.         }
  63.         else {
  64.             for (int i=1; i<=rodSize; i++) {
  65.                 q = Math.max(q, price[i]+memoized_cut_rod_aux (price, rodSize-i, revenue));
  66.             }
  67.         }
  68.         revenue[rodSize] = q;
  69.         return q;
  70.        
  71.        
  72.     }
  73.    
  74.     public int bottom_up_cut_rod () {
  75.         revenue = new int [rodSize+1];
  76.        
  77.         revenue[0] = 0;
  78.  
  79.         for (int j=1; j<=rodSize; j++) {
  80.            
  81.             int q = Integer.MIN_VALUE;
  82.            
  83.             for (int i=1; i<=j; i++) {
  84.                 q = Math.max(q, price[i]+revenue[j-i]);
  85.             }
  86.             revenue[j] = q;
  87.            
  88.         }
  89.         return revenue[rodSize];
  90.     }
  91.    
  92.     public void extended_bottom_up_cut_rod () {
  93.         revenue = new int [rodSize+1];
  94.         s = new int [rodSize+1];
  95.        
  96.         revenue[0] = 0;
  97.        
  98.         for (int j = 1; j<=rodSize; j++) {
  99.             int q = Integer.MIN_VALUE;
  100.            
  101.             for (int i = 1; i <= j ; i++) {
  102.                 if (q < price[i] + revenue[j-i]){
  103.                     q = price[i] + revenue[j-i];
  104.                     s[j] = i;
  105.                 }
  106.             }
  107.             revenue[j] = q;
  108.         }  
  109.     }
  110.    
  111.     public void print_cut_rod_solution () {
  112.         for (int i = 0; i <= rodSize; i++) {
  113.             System.out.print(i + "\t");
  114.         }
  115.         System.out.print("\n");
  116.         for (int i = 0; i <= rodSize; i++) {
  117.             System.out.print(revenue[i] + "\t");
  118.         }
  119.         System.out.print("\n");
  120.         for (int i = 0; i <= rodSize; i++) {
  121.             System.out.print(s[i] + "\t");
  122.         }
  123.         System.out.print("\n");
  124.     }
  125.    
  126.  
  127.     /**
  128.      * @param args
  129.      */
  130.     public static void main(String[] args) {
  131.         // TODO Auto-generated method stub
  132.         RodCut rc;
  133.        
  134.         rc = new RodCut();
  135.         System.out.println("memoized_cut_rod starts ------------------");
  136.         System.out.println(rc.memoized_cut_rod());
  137.         System.out.println("memoized_cut_rod ends ------------------");
  138.         System.out.print("\n\n");
  139.        
  140.         System.out.println("bottom_up_cut_rod starts ------------------");
  141.         System.out.println(rc.bottom_up_cut_rod());
  142.         System.out.println("bottom_up_cut_rod ends ------------------");
  143.         System.out.print("\n\n");
  144.  
  145.         System.out.println("extended_bottom_up_cut_rod starts ------------------");
  146.         rc.extended_bottom_up_cut_rod();
  147.         rc.print_cut_rod_solution();
  148.         System.out.println("extended_bottom_up_cut_rod ends ------------------");
  149.         System.out.print("\n\n");
  150.     }
  151.  
  152. }
Add Comment
Please, Sign In to add comment