Skip to content

Commit 4bcab89

Browse files
authored
Add JobSchedulingWithDeadline algorithm (#5608)
1 parent 5c79e5d commit 4bcab89

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
* scheduling
476476
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
477477
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
478+
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
478479
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
479480
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
480481
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
@@ -963,6 +964,7 @@
963964
* scheduling
964965
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
965966
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
967+
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
966968
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
967969
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
968970
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* A class that implements a job scheduling algorithm to maximize profit
8+
* while adhering to job deadlines and arrival times.
9+
*
10+
* This class provides functionality to schedule jobs based on their profit,
11+
* arrival time, and deadlines to ensure that the maximum number of jobs is completed
12+
* within the given timeframe. It sorts the jobs in decreasing order of profit
13+
* and attempts to assign them to the latest possible time slots.
14+
*/
15+
public final class JobSchedulingWithDeadline {
16+
private JobSchedulingWithDeadline() {
17+
}
18+
19+
/**
20+
* Represents a job with an ID, arrival time, deadline, and profit.
21+
*
22+
* Each job has a unique identifier, an arrival time (when it becomes available for scheduling),
23+
* a deadline by which it must be completed, and a profit associated with completing the job.
24+
*/
25+
static class Job {
26+
int jobId;
27+
int arrivalTime;
28+
int deadline;
29+
int profit;
30+
31+
/**
32+
* Constructs a Job instance with the specified job ID, arrival time, deadline, and profit.
33+
*
34+
* @param jobId Unique identifier for the job
35+
* @param arrivalTime Time when the job becomes available for scheduling
36+
* @param deadline Deadline for completing the job
37+
* @param profit Profit earned upon completing the job
38+
*/
39+
Job(int jobId, int arrivalTime, int deadline, int profit) {
40+
this.jobId = jobId;
41+
this.arrivalTime = arrivalTime;
42+
this.deadline = deadline;
43+
this.profit = profit;
44+
}
45+
}
46+
47+
/**
48+
* Schedules jobs to maximize profit while respecting their deadlines and arrival times.
49+
*
50+
* This method sorts the jobs in descending order of profit and attempts
51+
* to allocate them to time slots that are before or on their deadlines,
52+
* provided they have arrived. The function returns an array where the first element
53+
* is the total number of jobs scheduled and the second element is the total profit earned.
54+
*
55+
* @param jobs An array of Job objects, each representing a job with an ID, arrival time,
56+
* deadline, and profit.
57+
* @return An array of two integers: the first element is the count of jobs
58+
* that were successfully scheduled, and the second element is the
59+
* total profit earned from those jobs.
60+
*/
61+
public static int[] jobSequencingWithDeadlines(Job[] jobs) {
62+
Arrays.sort(jobs, Comparator.comparingInt(job -> - job.profit));
63+
64+
int maxDeadline = Arrays.stream(jobs).mapToInt(job -> job.deadline).max().orElse(0);
65+
66+
int[] timeSlots = new int[maxDeadline];
67+
Arrays.fill(timeSlots, -1);
68+
69+
int count = 0;
70+
int maxProfit = 0;
71+
72+
// Schedule the jobs
73+
for (Job job : jobs) {
74+
if (job.arrivalTime <= job.deadline) {
75+
for (int i = Math.min(job.deadline - 1, maxDeadline - 1); i >= job.arrivalTime - 1; i--) {
76+
if (timeSlots[i] == -1) {
77+
timeSlots[i] = job.jobId;
78+
count++;
79+
maxProfit += job.profit;
80+
break;
81+
}
82+
}
83+
}
84+
}
85+
86+
return new int[] {count, maxProfit};
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class JobSchedulingWithDeadlineTest {
8+
9+
@Test
10+
void testJobSequencingWithDeadlines1() {
11+
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)};
12+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
13+
assertArrayEquals(new int[] {2, 60}, result); // Expected output: 2 jobs, 60 profit
14+
}
15+
16+
@Test
17+
void testJobSequencingWithDeadlines2() {
18+
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)};
19+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
20+
assertArrayEquals(new int[] {2, 127}, result); // Expected output: 2 jobs, 127 profit
21+
}
22+
23+
@Test
24+
void testJobSequencingWithDeadlinesWithArrivalTimes() {
25+
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 5, 50), new JobSchedulingWithDeadline.Job(2, 3, 4, 60), new JobSchedulingWithDeadline.Job(3, 1, 3, 20)};
26+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
27+
assertArrayEquals(new int[] {3, 130}, result); // All 3 jobs fit within their deadlines
28+
}
29+
30+
@Test
31+
void testJobSequencingWithDeadlinesNoJobs() {
32+
JobSchedulingWithDeadline.Job[] jobs = {};
33+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
34+
assertArrayEquals(new int[] {0, 0}, result); // No jobs, 0 profit
35+
}
36+
37+
@Test
38+
void testJobSequencingWithDeadlinesSingleJob() {
39+
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 1, 50)};
40+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
41+
assertArrayEquals(new int[] {1, 50}, result); // 1 job scheduled, 50 profit
42+
}
43+
}

0 commit comments

Comments
 (0)