Guest User

Permutations

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. RWS-101:      1
  2. CS-237:       2
  3. MATH-254:     2
  4. RWS-100:      6
  5. Math-151:    14
  6. MATH-150:    22
  7. Comm-103:   100
  8.  
  9. iterationVariables [simplified]
  10. ------------------------------------------------------------------------------------
  11. 0   0   0   0   0   0
  12. 0   0   0   0   0   1
  13. 0   0   0   0   0   2
  14. 0   0   0   0   0   ..
  15. 0   0   0   0   0   99
  16. 0   0   0   0   0   100*  () -> 0
  17. 0   0   0   0   1   0
  18.                 ...
  19.  
  20.  
  21. Computations
  22. -----------------------------------------------------------------------------------------------
  23.     Class 0          Class 1            Class 2          Class 3            Class n-1
  24. 1). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    MWF 8:00am-9:15am  ...
  25. 2). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    TTH 5:00pm-5:45pm  ...
  26. 3). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    MWF 9:00am-10:15am ...
  27. 4). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    TTH 5:30pm-6:20pm  ...
  28. 5). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    MWF 9:30am-10:15am ...
  29. 6). MWF 9:00am-9:50am   TTH 4:30pm-5:20pm   WF 1:30pm-1:20pm    TTH 6:15pm-6:50pm  ...
  30.                                                     M  T  W  TH F
  31.     * For each class and each iteration, create an array of days. (convDaysToArray("MWF") ()-> [1, 0, 1, 0, 1])
  32.     *   For each day, if daysArray[day] == 1, convert timeblock to a long composed of binary bits
  33.     *   ex). convTimeToBits("9:00am-9:50am",convDaysToArray("MWF") ()->
  34.             each bit represents 15min starting from 8:00am. "9:00am-9:50am" ()-> "..000011110000" = -16 (decimal from signed 2's comp)
  35.                                 Left to Right:      (9:45am, 9:30am, 9:15am, 9:00am, etc.)
  36.  
  37.     *   For each string of courses (schedule), bitwise `AND` with each active day in long[] timeblockBits. If it equals 0 every day, then they fit
  38.     *   ex). Line 1, Class 0:  long timeblocks[] = [11110000, 0, 11110000, 0, 11110000] = [-16, 0, -16, 0, -16]
  39.     *        Line 1, Class 3:  long timeblocks[] = [00111111, 0, 00111111, 0, 00111111] = [ 63, 0,  63, 0,  63]
  40.                             (AND) ---------------------------------------------------------------
  41.                                [00110000, 0, 00110000, 0, 00110000] = [ 48, 0,  48, 0,  48] != 0 each day --> Schedule has collisions
  42.     Optimization
  43.       ---------------
  44.     * Order the classes from LEAST number of sections to MOST number of sections
  45.     *   ex). List<Course> class_0;  class_0.size() == 22
  46.              List<Course> class_1;  class_1.size() == 5
  47.              List<Course> class_2;  class_2.size() == 100
  48.              List<Course> class_3;  class_3.size() == 18
  49.             ()-> List<List<Course>> sizeSortedSections = new ArrayList<>();
  50.                  sizeSortedSections.add(class_2);
  51.                  sizeSortedSections.add(class_4);
  52.                  sizeSortedSections.add(class_1);
  53.                  sizeSortedSections.add(class_3);
  54.     *   Create an array[numClasses] to iterate through the permutations
  55.     *      int[] iterationVariables = new int[4];  // {0, 0, 0, 0}
  56.             Keep the value of iterationVariables[i] < sizeSortedCourses.get(i)
  57.                ex). {0, 0, 0, 0} -> {0, 0, 0, 1} -> {0, 0, 0, 2} ... -> {0, 0, 0, 99} -> {0, 0, 1, 0} -> {0, 0, 0, 1}
  58.           * The most commonly used string of classes (with respect to storage space) is 0,0,0 (will be used 100 times), 0,0,1 (will be used 100 times), etc.
  59.             Therefore, store the combinedTimeblocks of:
  60.                 sizeSortedSections.get(0).get(iterationVariables[0]).convTimeToBits(course.times, convDaysToArray(course.days));
  61.         (AND)   sizeSortedSections.get(1).get(iterationVariables[1]).convTimeToBits(course.times, convDaysToArray(course.days));
  62.                 sizeSortedSections.get(n-2).get(iterationVariables[n-2]).convTimeToBits(course.times, convDaysToArray(course.days));
  63.                   ------------------------------------------------------------------------------------------------------------------
  64.                 ()-> long[] combinedTimeblocks = new long[5];  // 5 days (M,T,W,TH,F)
  65.                     combinedTimeblocks[0] = ${Collective results of bitwise AND-ing classes 0 through (n-2) for Monday}
  66.                     combinedTimeblocks[1] = ${Collective results of bitwise AND-ing classes 0 through (n-2) for Tuesday}
  67.                     combinedTimeblocks[2] = ${Collective results of bitwise AND-ing classes 0 through (n-2) for Wednesday} 
  68.                     combinedTimeblocks[3] = ${Collective results of bitwise AND-ing classes 0 through (n-2) for Thursday}
  69.                     combinedTimeblocks[4] = ${Collective results of bitwise AND-ing classes 0 through (n-2) for Friday}
  70.           * Store the combinedTimeblocks in a String -> long[] Map
  71.              - The String is a concatenation of your iterationVariables[0] through iterationVariables[n-2]
  72.             ex). Map<String, long[]> storedBlocks = new HashMap<>();
  73.                 // iterationVariables = {0, 0, 0, 0}; n = 4; String permutation = iterationVariables[0, 1, 2].toString() = "000"; *pseudo*
  74.                 if(storedBlocks.containsKey(permutation)) { combinedTimeBlocks = storedBlocks.get(permutation);
  75.                 else {
  76.                    // Calculate the combinedTimeblocks for each class 0 through (n-2) for days Monday through Friday for each associated
  77.                       course.days and course.times
  78.                    combinedTimeblocks = ...
  79.                    storedBlocks.put(permutation, combinedTimeblocks);
  80.                 }
  81.           * Now that you have the combinedTimeBlocks for class_0 through class_(n-2), calculate the convTimeToBits() for class(n-1), then
  82.               bitwise `AND` it with combinedTimeBlocks for each day 0 through 4
  83.        
  84.         // General idea for last step:
  85.           while(iterationVaribles[0] < sizeSortedSections.get(0).size()) {
  86.              for(int day = 0; day < 5; day++) {
  87.                 if(combinedTimeBlocks[day] & class_(n-1)_bits[day] != 0) {
  88.                // Classes collide; increment the iteration variables and repeat
  89.                continue whileLoopAbove;
  90.                  }
  91.               }
  92.              addToResultSet();
  93.            }
Add Comment
Please, Sign In to add comment