panaewboi

MYSP.WeightJobSchedule

May 12th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package specialproject2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.List;
  7.  
  8. public class WeightJobSchedule {
  9. /**
  10. * Sort jobs by finish time.
  11. * For every job, find the first job which does not overlap with this job
  12. * and see if this job profit plus profit till last non overlapping job is greater than profit till last job.
  13. */
  14.  
  15. public static int findLastNonConflictingJob(List<Job> jobs, int n)
  16. {
  17. // search space
  18. int low = 0;
  19. int high = n;
  20.  
  21. // iterate till search space is not exhausted
  22. while (low <= high)
  23. {
  24. int mid = (low + high) / 2;
  25. if (jobs.get(mid).finishtime <= jobs.get(n).starttime) {
  26. if (jobs.get(mid + 1).finishtime <= jobs.get(n).starttime) {
  27. low = mid + 1;
  28. }
  29. else {
  30. return mid;
  31. }
  32. }
  33. else {
  34. high = mid - 1;
  35. }
  36. }
  37.  
  38. // return negative index if no non-conflicting job is found
  39. return -1;
  40. }
  41.  
  42. // Function to print the non-overlapping jobs involved in maximum profit
  43. // using Dynamic Programming
  44. public static void findMaxProfitJobs(List<Job> jobs)
  45. {
  46. // sort jobs in increasing order of their finish times
  47. Collections.sort(jobs, (x, y) -> x.finishtime - y.finishtime);
  48.  
  49. // get number of jobs
  50. int n = jobs.size();
  51.  
  52. // maxProfit[i] stores the maximum profit possible for the first i jobs and
  53. // tasks[i] stores the index of jobs involved in the maximum profit
  54. int[] maxProfit = new int[n];
  55. List<List<Integer>> tasks = new ArrayList<>(n);
  56.  
  57. for (int i = 0; i < n; i++) {
  58. tasks.add(i, new ArrayList<>());
  59. }
  60.  
  61. // initialize maxProfit[0] and tasks[0] with the first job
  62. maxProfit[0] = jobs.get(0).profit;
  63. tasks.get(0).add(0);
  64.  
  65. // fill tasks[] and maxProfit[] in bottom-up manner
  66. for (int i = 1; i < n; i++)
  67. {
  68. // find the index of last non-conflicting job with current job
  69. int index = findLastNonConflictingJob(jobs, i);
  70.  
  71. // include the current job with its non-conflicting jobs
  72. int currentProfit = jobs.get(i).profit;
  73. if (index != -1) {
  74. currentProfit += maxProfit[index];
  75. }
  76.  
  77. // if including the current job leads to the maximum profit so far
  78. if (maxProfit[i-1] < currentProfit)
  79. {
  80. maxProfit[i] = currentProfit;
  81.  
  82. if (index != -1) {
  83. tasks.set(i, tasks.get(index));
  84. }
  85. tasks.get(i).add(i);
  86. }
  87.  
  88. // excluding the current job leads to the maximum profit so far
  89. else {
  90. tasks.set(i, tasks.get(i-1));
  91. maxProfit[i] = maxProfit[i-1];
  92. }
  93. }
  94.  
  95. // maxProfit[n-1] stores the maximum profit
  96. System.out.println("The maximum profit is " + maxProfit[n-1]);
  97.  
  98. // tasks[n-1] stores the index of jobs involved in the maximum profit
  99. System.out.print("The jobs involved in the maximum profit are: ");
  100. for (int i: tasks.get(n-1))
  101. {
  102. System.out.print("(" + jobs.get(i).starttime + ", " + jobs.get(i).finishtime + ", " + jobs.get(i).profit + ") ");
  103. }
  104. }
  105.  
  106.  
  107.  
  108. public static void main(String[] args){
  109. List<Job> jobs = Arrays.asList(
  110. new Job( 0, 6, 2000 ),
  111. new Job( 1, 5, 530 ),
  112. new Job( 3, 5, 200 ),
  113. new Job( 5, 7, 680 ),
  114. new Job( 5, 9, 750 ),
  115. new Job( 7, 8, 100 ) );
  116.  
  117. findMaxProfitJobs(jobs);
  118. }
  119. }
Add Comment
Please, Sign In to add comment