Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
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 Algorithms/Problems/JobScheduling/IntervalSchedulingSolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Algorithms.Problems.JobScheduling;

/// <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;
}
}
10 changes: 10 additions & 0 deletions Algorithms/Problems/JobScheduling/Job.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Algorithms.Problems.JobScheduling;

/// <summary>
/// Represents a single job with a start and end time.
/// </summary>
public record Job(int Start, int End);
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ find more than one implementation for the same objective but using different alg
* [Dynamic Programming](./Algorithms/Problems/DynamicProgramming)
* [Coin Change](./Algorithms/Problems/DynamicProgramming/CoinChange/DynamicCoinChangeSolver.cs)
* [Levenshtein Distance](./Algorithms/Problems/DynamicProgramming/LevenshteinDistance/LevenshteinDistance.cs)
* [Job Scheduling](./Algorithms/Problems/JobScheduling)
* [Interval Scheduling (Greedy)](./Algorithms/Problems/JobScheduling/IntervalSchedulingSolver.cs)
* [Weighted Interval Scheduling (DP)](./Algorithms/Problems/JobScheduling/WeightedIntervalSchedulingSolver.cs)
* **Applications:** Work schedule management, room booking, production optimization, and more.

* [Data Structures](./DataStructures)
* [Bag](./DataStructures/Bag)
Expand Down