├── README.md ├── .gitignore └── src └── main └── java └── edu └── nyu └── cs9053 └── homework8 ├── LambdaJob.java ├── LambdaWeightedJob.java ├── ReadMe.md ├── LambdaScheduler.java ├── Test.java └── LambdaWeightedScheduler.java /README.md: -------------------------------------------------------------------------------- 1 | # CS9053-Homework-8 2 | This is a CS9053 Java class homework with the goal to design algorithms using Java Collections. 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | *.DS_Store 7 | 8 | # Package Files # 9 | *.jar 10 | *.war 11 | *.ear 12 | 13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 14 | hs_err_pid* 15 | -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/LambdaJob.java: -------------------------------------------------------------------------------- 1 | package edu.nyu.cs9053.homework8; 2 | 3 | public class LambdaJob{ 4 | private final int startingTime; 5 | private final int finalTime; 6 | public LambdaJob(int startingTime, int finalTime){ 7 | this.startingTime = startingTime; 8 | this.finalTime = finalTime; 9 | } 10 | 11 | public Integer getStartingTime(){ 12 | return this.startingTime; 13 | } 14 | 15 | public Integer getFinalTime(){ 16 | return this.finalTime; 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/LambdaWeightedJob.java: -------------------------------------------------------------------------------- 1 | package edu.nyu.cs9053.homework8; 2 | 3 | public class LambdaWeightedJob extends LambdaJob{ 4 | private final int cost; 5 | 6 | public LambdaWeightedJob(int startingTime, int finalTime, int cost){ 7 | super(startingTime, finalTime); 8 | this.cost = cost; 9 | } 10 | 11 | @Override public Integer getStartingTime(){ 12 | return super.getStartingTime(); 13 | } 14 | 15 | @Override public Integer getFinalTime(){ 16 | return super.getFinalTime(); 17 | } 18 | 19 | public Integer getCost(){ 20 | return this.cost; 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/ReadMe.md: -------------------------------------------------------------------------------- 1 | # Class Hirarchy 2 | - `class` LambdaJob 3 | - `class` LambdaWeightedJob 4 | - `Interface` Scheduler 5 | - `Abstract class` AbstractScheduler 6 | - `class` LambdaScheduler 7 | - `class` LambdaWeightedScheduler 8 | 9 | # Task 1 10 | Check 11 | 12 | # Task 2 13 | Algorithm: `Greedy Algorithm`, sort the job list by the final time. Then every time choose the compatible job that has minimum final time. 14 | 15 | # Task 3 16 | Algorithm: `Dynamic Programming`, sort the job list by the final time. Add job 0, whose final time is 0, and job n+1, whose starting time is Integer.MAX_VALUE. For a job list interval (i,j), denote the maximum cost of compatible set by maxCost[i][j]. While maxCost[i][j] = max{maxCost[i][k]+maxCost[k][j] + k.cost, in which k is a compatible job in (i,j). 17 | 18 | Note that Task 2 is a special condition of Task 3 when all the jobs has the same cost. -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/LambdaScheduler.java: -------------------------------------------------------------------------------- 1 | package edu.nyu.cs9053.homework8; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Iterator; 6 | import java.util.Collections; 7 | import java.util.Comparator; 8 | 9 | public class LambdaScheduler{ 10 | private List jobList; 11 | private static final int minStartingTime = 0; 12 | private static final int maxFinalTime = Integer.MAX_VALUE; 13 | public LambdaScheduler(List jobList){ 14 | this.jobList = new ArrayList<>(jobList); 15 | } 16 | 17 | private void sortJobListByFinalTime(){ 18 | Collections.sort( jobList, new Comparator(){ 19 | @Override 20 | public int compare(LambdaJob job1, LambdaJob job2){ 21 | return job1.getFinalTime().compareTo(job2.getFinalTime()); 22 | } 23 | }); 24 | } 25 | 26 | public List getScheduleList(){ 27 | 28 | sortJobListByFinalTime(); 29 | 30 | List scheduleList = new ArrayList<>(); 31 | 32 | Iterator jobIterator = jobList.iterator(); 33 | final int jobListSize = jobList.size(); 34 | 35 | int lastFinalTime = minStartingTime; 36 | while( jobIterator.hasNext() ){ 37 | final LambdaJob currentJob = jobIterator.next(); 38 | if( currentJob.getStartingTime() >= lastFinalTime ){ 39 | scheduleList.add( currentJob ); 40 | lastFinalTime = currentJob.getFinalTime(); 41 | } 42 | } 43 | return scheduleList; 44 | } 45 | } -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/Test.java: -------------------------------------------------------------------------------- 1 | package edu.nyu.cs9053.homework8; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Iterator; 6 | import static org.junit.Assert.*; 7 | 8 | public class Test{ 9 | public static void main(String[] args){ 10 | 11 | long a = 5, b=5; 12 | assertEquals(a, b); 13 | 14 | System.out.println("Test for LambdaScheduler"); 15 | 16 | List jobList = new ArrayList<>(); 17 | jobList.add( new LambdaJob(7,8) ); 18 | 19 | jobList.add( new LambdaJob(2,4) ); 20 | jobList.add( new LambdaJob(1,5) ); 21 | jobList.add( new LambdaJob(7,8) ); 22 | jobList.add( new LambdaJob(6,10) ); 23 | jobList.add( new LambdaJob(9,13) ); 24 | jobList.add( new LambdaJob(12,16) ); 25 | jobList.add( new LambdaJob(15,17) ); 26 | 27 | final LambdaScheduler lambdaScheduler = new LambdaScheduler(jobList); 28 | final List scheduleList = lambdaScheduler.getScheduleList(); 29 | Iterator scheduleIterator = scheduleList.iterator(); 30 | while( scheduleIterator.hasNext() ){ 31 | final LambdaJob currentJob = scheduleIterator.next(); 32 | System.out.printf("(%d, %d)\n", currentJob.getStartingTime(), currentJob.getFinalTime() ); 33 | } 34 | 35 | System.out.println("Test for LambdaWeightedScheduler"); 36 | 37 | List weightedJobList = new ArrayList<>(); 38 | weightedJobList.add( new LambdaWeightedJob(7,8,1) ); 39 | 40 | weightedJobList.add( new LambdaWeightedJob(2,4,1) ); 41 | weightedJobList.add( new LambdaWeightedJob(1,5,1) ); 42 | weightedJobList.add( new LambdaWeightedJob(7,8,1) ); 43 | weightedJobList.add( new LambdaWeightedJob(6,10,1) ); 44 | weightedJobList.add( new LambdaWeightedJob(9,13,1) ); 45 | weightedJobList.add( new LambdaWeightedJob(12,16,1) ); 46 | weightedJobList.add( new LambdaWeightedJob(15,17,1) ); 47 | final LambdaWeightedScheduler lambdaWeightedScheduler = new LambdaWeightedScheduler(weightedJobList); 48 | final List weightedScheduleList = lambdaWeightedScheduler.getScheduleList(); 49 | Iterator weightedScheduleIterator = weightedScheduleList.iterator(); 50 | while( weightedScheduleIterator.hasNext() ){ 51 | final LambdaWeightedJob currentJob = weightedScheduleIterator.next(); 52 | System.out.printf("(%d, %d)\n", currentJob.getStartingTime(), currentJob.getFinalTime() ); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/edu/nyu/cs9053/homework8/LambdaWeightedScheduler.java: -------------------------------------------------------------------------------- 1 | package edu.nyu.cs9053.homework8; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Iterator; 6 | import java.util.Collections; 7 | import java.util.Comparator; 8 | 9 | public class LambdaWeightedScheduler{ 10 | private List weightedJobList; 11 | private static final int minStartingTime = 0; 12 | private static final int maxFinalTime = Integer.MAX_VALUE; 13 | 14 | public LambdaWeightedScheduler(List weightedJobList){ 15 | this.weightedJobList = new ArrayList<>(); 16 | this.weightedJobList.add( new LambdaWeightedJob(Integer.MIN_VALUE, minStartingTime, 0) ); 17 | this.weightedJobList.addAll( weightedJobList ); 18 | this.weightedJobList.add( new LambdaWeightedJob(maxFinalTime, maxFinalTime, 0) ); 19 | 20 | } 21 | 22 | private void sortWeightedJobListByFinalTime(){ 23 | Collections.sort( weightedJobList, new Comparator(){ 24 | @Override 25 | public int compare(LambdaWeightedJob job1, LambdaWeightedJob job2){ 26 | return job1.getFinalTime().compareTo(job2.getFinalTime()); 27 | } 28 | }); 29 | } 30 | 31 | private void traceMaxCost( int[][] traceIndex, int start, int right, List scheduleList){ 32 | if( traceIndex[ start ][ right ] != -1 ){ 33 | final int k = traceIndex[ start ][ right ]; 34 | traceMaxCost( traceIndex, start, k, scheduleList); 35 | scheduleList.add( weightedJobList.get(k) ); 36 | traceMaxCost( traceIndex, k, right, scheduleList); 37 | } 38 | } 39 | 40 | public List getScheduleList(){ 41 | 42 | sortWeightedJobListByFinalTime(); 43 | 44 | List scheduleList = new ArrayList<>(); 45 | final int weightedJobListSize = weightedJobList.size(); 46 | 47 | int maxCost[][] = new int[ weightedJobListSize ][ weightedJobListSize ]; 48 | int traceIndex[][] = new int[ weightedJobListSize ][ weightedJobListSize ]; 49 | for(int i=0; i=0; i--){ 57 | for(int j=i+1; j<=weightedJobListSize-1; j++){ 58 | for(int k=i+1; k= weightedJobList.get(i).getFinalTime() 61 | && currentJob.getFinalTime() <= weightedJobList.get(j).getStartingTime()){ 62 | final int tmp = maxCost[i][k] + maxCost[k][j] + currentJob.getCost(); 63 | if( maxCost[i][j] < tmp ){ 64 | maxCost[i][j] = tmp; 65 | traceIndex[i][j] = k; 66 | } 67 | } 68 | } 69 | } 70 | } 71 | 72 | traceMaxCost( traceIndex, 0, weightedJobListSize-1, scheduleList); 73 | 74 | return scheduleList; 75 | } 76 | } --------------------------------------------------------------------------------