-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add Interval Scheduling (Greedy Algorithm) and Tests #537
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 5 commits into
TheAlgorithms:master
from
ngtduc693:feature/interval-scheduling-solver
Oct 5, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6017b94
Add Interval Scheduling (Greedy Algorithm) and Tests
ngtduc693 71a7733
Merge branch 'master' into feature/interval-scheduling-solver
siriak 07b91b4
Add Simple Linear Regression Algorithm (#538)
ngtduc693 5fda02d
Merge branch 'master' into feature/interval-scheduling-solver
ngtduc693 b9ce9b5
Update the Readme.md
ngtduc693 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
110 changes: 110 additions & 0 deletions
110
Algorithms.Tests/Problems/JobScheduling/IntervalSchedulingSolverTests.cs
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,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Algorithms.Problems.JobScheduling; | ||
|
||
namespace Algorithms.Tests.Problems.JobScheduling; | ||
|
||
public class IntervalSchedulingSolverTests | ||
{ | ||
[Test] | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void Schedule_ReturnsEmpty_WhenNoJobs() | ||
{ | ||
var result = IntervalSchedulingSolver.Schedule(new List<Job>()); | ||
Assert.That(result, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void Schedule_ReturnsSingleJob_WhenOnlyOneJob() | ||
{ | ||
var jobs = new List<Job> { new Job(1, 3) }; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
Assert.That(result, Has.Count.EqualTo(1)); | ||
Assert.That(result[0], Is.EqualTo(jobs[0])); | ||
} | ||
|
||
[Test] | ||
public void Schedule_ThrowsArgumentNullException_WhenJobsIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => IntervalSchedulingSolver.Schedule(jobs: null!)); | ||
} | ||
|
||
[Test] | ||
public void Schedule_SelectsJobsWithEqualEndTime() | ||
{ | ||
var jobs = new List<Job> | ||
{ | ||
new Job(1, 4), | ||
new Job(2, 4), | ||
new Job(4, 6) | ||
}; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
Assert.That(result, Has.Count.EqualTo(2)); | ||
Assert.That(result, Does.Contain(new Job(1, 4))); | ||
Assert.That(result, Does.Contain(new Job(4, 6))); | ||
} | ||
|
||
[Test] | ||
public void Schedule_SelectsJobStartingAtLastEnd() | ||
{ | ||
var jobs = new List<Job> | ||
{ | ||
new Job(1, 3), | ||
new Job(3, 5), | ||
new Job(5, 7) | ||
}; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
Assert.That(result, Has.Count.EqualTo(3)); | ||
Assert.That(result[0], Is.EqualTo(jobs[0])); | ||
Assert.That(result[1], Is.EqualTo(jobs[1])); | ||
Assert.That(result[2], Is.EqualTo(jobs[2])); | ||
} | ||
|
||
[Test] | ||
public void Schedule_HandlesJobsWithNegativeTimes() | ||
{ | ||
var jobs = new List<Job> | ||
{ | ||
new Job(-5, -3), | ||
new Job(-2, 1), | ||
new Job(0, 2) | ||
}; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
Assert.That(result, Has.Count.EqualTo(2)); | ||
Assert.That(result, Does.Contain(new Job(-5, -3))); | ||
Assert.That(result, Does.Contain(new Job(-2, 1))); | ||
} | ||
|
||
[Test] | ||
public void Schedule_SelectsNonOverlappingJobs() | ||
{ | ||
var jobs = new List<Job> | ||
{ | ||
new Job(1, 4), | ||
new Job(3, 5), | ||
new Job(0, 6), | ||
new Job(5, 7), | ||
new Job(8, 9), | ||
new Job(5, 9) | ||
}; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
// Expected: (1,4), (5,7), (8,9) | ||
Assert.That(result, Has.Count.EqualTo(3)); | ||
Assert.That(result, Does.Contain(new Job(1, 4))); | ||
Assert.That(result, Does.Contain(new Job(5, 7))); | ||
Assert.That(result, Does.Contain(new Job(8, 9))); | ||
} | ||
|
||
[Test] | ||
public void Schedule_HandlesFullyOverlappingJobs() | ||
{ | ||
var jobs = new List<Job> | ||
{ | ||
new Job(1, 5), | ||
new Job(2, 6), | ||
new Job(3, 7) | ||
}; | ||
var result = IntervalSchedulingSolver.Schedule(jobs); | ||
// Only one job can be selected | ||
Assert.That(result, Has.Count.EqualTo(1)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Algorithms/Problems/JobScheduling/IntervalSchedulingSolver.cs
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,38 @@ | ||
namespace Algorithms.Problems.JobScheduling; | ||
ngtduc693 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Implements the greedy algorithm for Interval Scheduling. | ||
/// Finds the maximum set of non-overlapping jobs. | ||
/// </summary> | ||
public static class IntervalSchedulingSolver | ||
{ | ||
/// <summary> | ||
/// Returns the maximal set of non-overlapping jobs. | ||
/// </summary> | ||
/// <param name="jobs">List of jobs to schedule.</param> | ||
/// <returns>List of selected jobs (maximal set).</returns> | ||
public static List<Job> Schedule(IEnumerable<Job> jobs) | ||
{ | ||
if (jobs == null) | ||
{ | ||
throw new ArgumentNullException(nameof(jobs)); | ||
} | ||
|
||
// Sort jobs by their end time (earliest finish first) | ||
var sortedJobs = jobs.OrderBy(j => j.End).ToList(); | ||
var result = new List<Job>(); | ||
int lastEnd = int.MinValue; | ||
|
||
foreach (var job in sortedJobs) | ||
{ | ||
// If the job starts after the last selected job ends, select it | ||
if (job.Start >= lastEnd) | ||
{ | ||
result.Add(job); | ||
lastEnd = job.End; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
ngtduc693 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
namespace Algorithms.Problems.JobScheduling; | ||
|
||
/// <summary> | ||
/// Represents a single job with a start and end time. | ||
/// </summary> | ||
public record Job(int Start, int End); |
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
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.