Add JobSchedulingWithDeadline algorithm (#5608)

This commit is contained in:
Hardik Pawar
2024-10-09 12:03:39 +05:30
committed by GitHub
parent 5c79e5de5d
commit 4bcab89f05
3 changed files with 133 additions and 0 deletions

View File

@ -475,6 +475,7 @@
* scheduling
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
@ -963,6 +964,7 @@
* scheduling
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)

View File

@ -0,0 +1,88 @@
package com.thealgorithms.scheduling;
import java.util.Arrays;
import java.util.Comparator;
/**
* A class that implements a job scheduling algorithm to maximize profit
* while adhering to job deadlines and arrival times.
*
* This class provides functionality to schedule jobs based on their profit,
* arrival time, and deadlines to ensure that the maximum number of jobs is completed
* within the given timeframe. It sorts the jobs in decreasing order of profit
* and attempts to assign them to the latest possible time slots.
*/
public final class JobSchedulingWithDeadline {
private JobSchedulingWithDeadline() {
}
/**
* Represents a job with an ID, arrival time, deadline, and profit.
*
* Each job has a unique identifier, an arrival time (when it becomes available for scheduling),
* a deadline by which it must be completed, and a profit associated with completing the job.
*/
static class Job {
int jobId;
int arrivalTime;
int deadline;
int profit;
/**
* Constructs a Job instance with the specified job ID, arrival time, deadline, and profit.
*
* @param jobId Unique identifier for the job
* @param arrivalTime Time when the job becomes available for scheduling
* @param deadline Deadline for completing the job
* @param profit Profit earned upon completing the job
*/
Job(int jobId, int arrivalTime, int deadline, int profit) {
this.jobId = jobId;
this.arrivalTime = arrivalTime;
this.deadline = deadline;
this.profit = profit;
}
}
/**
* Schedules jobs to maximize profit while respecting their deadlines and arrival times.
*
* This method sorts the jobs in descending order of profit and attempts
* to allocate them to time slots that are before or on their deadlines,
* provided they have arrived. The function returns an array where the first element
* is the total number of jobs scheduled and the second element is the total profit earned.
*
* @param jobs An array of Job objects, each representing a job with an ID, arrival time,
* deadline, and profit.
* @return An array of two integers: the first element is the count of jobs
* that were successfully scheduled, and the second element is the
* total profit earned from those jobs.
*/
public static int[] jobSequencingWithDeadlines(Job[] jobs) {
Arrays.sort(jobs, Comparator.comparingInt(job -> - job.profit));
int maxDeadline = Arrays.stream(jobs).mapToInt(job -> job.deadline).max().orElse(0);
int[] timeSlots = new int[maxDeadline];
Arrays.fill(timeSlots, -1);
int count = 0;
int maxProfit = 0;
// Schedule the jobs
for (Job job : jobs) {
if (job.arrivalTime <= job.deadline) {
for (int i = Math.min(job.deadline - 1, maxDeadline - 1); i >= job.arrivalTime - 1; i--) {
if (timeSlots[i] == -1) {
timeSlots[i] = job.jobId;
count++;
maxProfit += job.profit;
break;
}
}
}
}
return new int[] {count, maxProfit};
}
}

View File

@ -0,0 +1,43 @@
package com.thealgorithms.scheduling;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class JobSchedulingWithDeadlineTest {
@Test
void testJobSequencingWithDeadlines1() {
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 4, 20), new JobSchedulingWithDeadline.Job(2, 1, 1, 10), new JobSchedulingWithDeadline.Job(3, 1, 1, 40), new JobSchedulingWithDeadline.Job(4, 1, 1, 30)};
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
assertArrayEquals(new int[] {2, 60}, result); // Expected output: 2 jobs, 60 profit
}
@Test
void testJobSequencingWithDeadlines2() {
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 2, 100), new JobSchedulingWithDeadline.Job(2, 1, 1, 19), new JobSchedulingWithDeadline.Job(3, 1, 2, 27), new JobSchedulingWithDeadline.Job(4, 1, 1, 25), new JobSchedulingWithDeadline.Job(5, 1, 1, 15)};
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
assertArrayEquals(new int[] {2, 127}, result); // Expected output: 2 jobs, 127 profit
}
@Test
void testJobSequencingWithDeadlinesWithArrivalTimes() {
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 5, 50), new JobSchedulingWithDeadline.Job(2, 3, 4, 60), new JobSchedulingWithDeadline.Job(3, 1, 3, 20)};
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
assertArrayEquals(new int[] {3, 130}, result); // All 3 jobs fit within their deadlines
}
@Test
void testJobSequencingWithDeadlinesNoJobs() {
JobSchedulingWithDeadline.Job[] jobs = {};
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
assertArrayEquals(new int[] {0, 0}, result); // No jobs, 0 profit
}
@Test
void testJobSequencingWithDeadlinesSingleJob() {
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 1, 50)};
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
assertArrayEquals(new int[] {1, 50}, result); // 1 job scheduled, 50 profit
}
}