-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add JobSchedulingWithDeadline.java
new algorithm with Junit tests
#5608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 15 commits into
TheAlgorithms:master
from
Hardvan:jobscheduling_deadline_new_algo
Oct 9, 2024
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
10f7e24
Add `JobSchedulingWithDeadline.java` new algorithm with Junit tests
Hardvan 2a0e7f1
Update directory
Hardvan 253f1b7
Merge branch 'master' into jobscheduling_deadline_new_algo
Hardvan 66b7ddb
Update directory
Hardvan d68bbd5
Fix
Hardvan e42fb12
Merge remote-tracking branch 'origin/jobscheduling_deadline_new_algo'…
Hardvan 797623a
Fix
Hardvan 271ddc5
Fix
Hardvan 5decf1e
Merge branch 'master' into jobscheduling_deadline_new_algo
siriak bffda74
Update directory
siriak 7f047c6
Fix
Hardvan fd37272
Merge remote-tracking branch 'origin/jobscheduling_deadline_new_algo'…
Hardvan 53d50df
Fix
Hardvan e0126d8
Fix
Hardvan 2442dff
Merge branch 'master' into jobscheduling_deadline_new_algo
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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. | ||
* | ||
* This class provides functionality to schedule jobs based on their profit | ||
* 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, deadline, and profit. | ||
* | ||
* Each job has a unique identifier, a deadline by which it must be completed, | ||
* and a profit associated with completing the job. | ||
*/ | ||
static class Job { | ||
int jobId; | ||
int deadline; | ||
int profit; | ||
|
||
/** | ||
* Constructs a Job instance with the specified job ID, deadline, and profit. | ||
* | ||
* @param jobId Unique identifier for the job | ||
* @param deadline Deadline for completing the job | ||
* @param profit Profit earned upon completing the job | ||
*/ | ||
Job(int jobId, int deadline, int profit) { | ||
this.jobId = jobId; | ||
this.deadline = deadline; | ||
this.profit = profit; | ||
} | ||
} | ||
|
||
/** | ||
* Schedules jobs to maximize profit while respecting their deadlines. | ||
* | ||
* 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. | ||
* 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, | ||
* 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) { | ||
for (int i = job.deadline - 1; i >= 0; i--) { | ||
if (timeSlots[i] == -1) { | ||
timeSlots[i] = job.jobId; | ||
count++; | ||
maxProfit += job.profit; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return new int[] {count, maxProfit}; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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, 4, 20), new JobSchedulingWithDeadline.Job(2, 1, 10), new JobSchedulingWithDeadline.Job(3, 1, 40), new JobSchedulingWithDeadline.Job(4, 1, 30)}; | ||
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); | ||
assertArrayEquals(new int[] {2, 60}, result); | ||
} | ||
|
||
@Test | ||
void testJobSequencingWithDeadlines2() { | ||
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 100), new JobSchedulingWithDeadline.Job(2, 1, 19), new JobSchedulingWithDeadline.Job(3, 2, 27), new JobSchedulingWithDeadline.Job(4, 1, 25), new JobSchedulingWithDeadline.Job(5, 1, 15)}; | ||
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); | ||
assertArrayEquals(new int[] {2, 127}, result); | ||
} | ||
|
||
@Test | ||
void testJobSequencingWithDeadlinesNoJobs() { | ||
JobSchedulingWithDeadline.Job[] jobs = {}; | ||
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); | ||
assertArrayEquals(new int[] {0, 0}, result); | ||
} | ||
|
||
@Test | ||
void testJobSequencingWithDeadlinesSingleJob() { | ||
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 50)}; | ||
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); | ||
assertArrayEquals(new int[] {1, 50}, result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.