-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
feat: Added Weighted Interval Scheduling #11765
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66d6969
feat: Added Weighted Interval Scheduling
BKarthik7 c11743e
ruff
BKarthik7 85117f6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d2b05c
variable change
BKarthik7 08e87b1
Merge branch 'master' of https://github.com/BKarthik7/Python
BKarthik7 129bba2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
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,81 @@ | ||
# Implementation of Weighted Interval Scheduling algorithm | ||
# In this algorithm, we are given a list of jobs with start and end times, | ||
# and each job has a specific weight. | ||
# The goal is to find the maximum weight subset of non-overlapping jobs. | ||
# https://en.wikipedia.org/wiki/Interval_scheduling | ||
|
||
from __future__ import annotations | ||
|
||
|
||
def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int: | ||
""" | ||
This function finds the latest job that does not conflict with | ||
the current job at index `n`. | ||
The jobs are given as (start_time, end_time, weight), and the | ||
jobs should be sorted by end time. | ||
It returns the index of the latest job that finishes before the | ||
current job starts. | ||
Return: The index of the latest non-conflicting job. | ||
>>> latest_non_conflict([(1, 3, 50), (2, 5, 20), (4, 6, 30)], 2) | ||
0 | ||
>>> latest_non_conflict([(1, 3, 50), (3, 4, 60), (5, 9, 70)], 2) | ||
1 | ||
""" | ||
for j in range(n - 1, -1, -1): | ||
if jobs[j][1] <= jobs[n][0]: | ||
return j | ||
return -1 | ||
|
||
|
||
def find_max_weight(jobs: list[tuple[int, int, int]]) -> int: | ||
""" | ||
This function calculates the maximum weight of non-overlapping jobs | ||
using dynamic programming. | ||
Each job is represented by a tuple (start_time, end_time, weight). | ||
The function builds a DP table where each entry `dp[i]` represents | ||
the maximum weight achievable | ||
using jobs from index 0 to i. | ||
Return: The maximum achievable weight without overlapping jobs. | ||
>>> find_max_weight([(1, 3, 50), (2, 5, 20), (4, 6, 30)]) | ||
80 | ||
>>> find_max_weight([(1, 3, 10), (2, 5, 100), (6, 8, 15)]) | ||
115 | ||
>>> find_max_weight([(1, 3, 20), (3, 5, 30), (6, 19, 60), (2, 100, 200)]) | ||
200 | ||
""" | ||
# Sort jobs based on their end times | ||
jobs.sort(key=lambda x: x[1]) | ||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Initialize dp array to store the maximum weight up to each job | ||
n = len(jobs) | ||
dp = [0] * n | ||
dp[0] = jobs[0][2] # The weight of the first job is the initial value | ||
|
||
for i in range(1, n): | ||
# Include the current job | ||
include_weight = jobs[i][2] | ||
latest_job = latest_non_conflict(jobs, i) | ||
if latest_job != -1: | ||
include_weight += dp[latest_job] | ||
|
||
# Exclude the current job, and take the maximum of including or | ||
# excluding | ||
dp[i] = max(include_weight, dp[i - 1]) | ||
|
||
return dp[-1] # The last entry contains the maximum weight | ||
|
||
|
||
if __name__ == "__main__": | ||
# Example list of jobs (start_time, end_time, weight) | ||
jobs = [(1, 2, 50), (3, 5, 20), (6, 19, 100), (2, 100, 200)] | ||
|
||
# Ensure we have jobs to process | ||
if len(jobs) == 0: | ||
print("No jobs available to process") | ||
raise SystemExit(0) | ||
|
||
# Calculate the maximum weight for non-overlapping jobs | ||
max_weight = find_max_weight(jobs) | ||
|
||
# Print the result | ||
print(f"The maximum weight of non-overlapping jobs is {max_weight}") |
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.