Weighted Interval Scheduling Algorithm Explained

Aladdin Persson · Intermediate ·⚡ Algorithms & Data Structures ·6y ago

Key Takeaways

The video explains the Weighted Interval Scheduling Algorithm using Dynamic Programming, covering the algorithm's approach, sorted jobs by finish time, and maximum weight maximum total weight, with a focus on disjoint jobs. The algorithm is further explained with steps to calculate the optimal solution using recursion and backtracking, and its time complexity is analyzed.

Full Transcript

hi and welcome in this video I want to go through how to solve the weighted interval scheduling problem with a dynamic programming approach and in the next video I will implement an algorithm in Python so the problem description is that we're given a list of jobs which all have a sorting finish and a profit or weight associated with it and the goal is that we want to find the subset of these jobs with the maximum weight maximum total weight where each job is disjoint which means that the jobs cannot occur at the same time the notation we'll use is they for a job I we have a starting time finish time and a weight very very quick and simple example we consider three jobs but if we consider taking the first job that means that we cannot pick job to because they overlap and if we pick job one then we can still pick job three so a total total weight will be two or we consider taking job two for a total weight of three but then we cannot pick job one or job three so in this case we take the second job for a total weight of three which is greater than two so if we're going to solve this problem using dynamic programming and idea is that we assume that we only look at the last job so the last job meaning we have sorted the jobs by finish time and we look at the last job with the latest finishing time then either we pick that job or we don't so that sounds simple let's examine it in a little bit more detail in case one that we pick it well then we need to remove all overlapping jobs and we need to compute a solution for the subproblem of the jobs left in case two we don't pick it well then we compute the optimal solution for the subproblem with n minus 1 items left so it becomes easy to see that if we have solved the subproblems then we can easily just examine the two choices well pick it or don't pick it but that assumes that we have solved the subproblems so this suggests a dynamic programming approach we can solve that we can solve it by recursively looking for J equals 1 2 up to n for the subproblems and we can define op J to be maximum weight that can be achieved by selecting the jobs from the first J intervals and we need to define a help function that is useful for removing overlapping intervals but we let P of J be the largest index I such that F of I is less than equal to s of J so what this means is that if the finish time is less than equal to the starting time of job J that means that they don't overlap right if the finish time for something is greater than the starting time that means that they overlap so since we have sorted by we have sorted by finish time if we take the largest index that means that we will get the job job I that is closest to job J so this jar this function is useful to remove overlapping jobs but let us understand this function a little bit better with an example so consider if we look at job one and there are no previous jobs so P of 1 is just 0 looking at job 2 then we notice here that the finish time of job 1 is a greater than the starting time of job 2 which means that they overlap and P of 2 is also 0 for job 3 we notice also that the finish time of both job 1 and job 2 is greater than starting time of job 3 so P of 3 is also 0 and now considering job for we see that well the finish time I mean it overlaps with job 2 and job 3 but it doesn't overlap with job 1 so if P of 4 is equal to 1 and for the last one or let's just look at one more because I think you get the idea so for job 5 we noticed that well it overlaps with job for and job 3 but not the job - or job 1 so we remember that we take the largest index and since 2 is greater than 1 then P of 5 is to another we understand the P function and also the the two cases that can arise either we picked item or we don't let's combine the two into a recursion formula and remember that we want to pick whichever is best and best meaning whichever results in the maximum total weight so we define up J to be the maximum of picking job J then we get the the weight of job J + opt of the remaining intervals when we have removed the ones that overlap so we get up of P of J or the second option which is just disregarding item J and looking at the J minus one items so we want to have whichever is the maximum of those two and remember that when we have a recursion formula we always need to have a base case and the base case is that opt of zero is zero so let's get some more intuition by seeing how we use this algorithm on the same example so we form a table for each job 1 2 8 and also the the the P of J we want to find those values and the goal is then to calculate opt of J with the end goal of finding opt of n where n is equal to 8 items in this case so note also that all jobs here are sorted by finished times and the recursion formula which we saw in a previous slide so filling out the weights which are just given in the problem then we want to find P of J which is which we calculated on a previous slide but just to reiterate let's say that we look at say that we look of job seven then we notice here that all of these three overlap right because the finish time of each of those are greater than the starting time but for job three to n1 the finish time is less than or equal to the starting time of this and pfj is gives us the great the greatest index I so P of seven would be 3 and P of 7 is 3 and now to find opt of J we start from the beginning we want to find upped of one so up to one is the maximum of taking the job job one which has a total weight of three plus up of P of one and I notice here also that there's a parenthesis missing here but anyways so opt of P of 1 P of 1 we see it's just 0 so we get the maximum of three plus up to zero up to zero is the base case which is a zero so up to one is a maximum of three plus zero comma zero so it will just be three the second one up to is the maximum of 2 plus opt of P of 2 or opt of 1 so up P of 2 is 0 as well and up to one which is the previous item it's three so we get the maximum of two or three so either we pick item two that we cannot pick item one I mean item job one so we see here that we want to pick job one because it has a greater weight oh the up to will also have a value of three so similarly for the rest of these values in the table we simply use the same recurrence formula and calculate the rest and what we can see for example is that when this value changes we note that this means that we pick item I mean job six so job six has a optimal I mean has a weight of 5 and then we consider which are the ones we can choose if if we remove if we remove the overlapping ones so we pick job six then we have P of six which is one right we only have job one we can pick which has a opt one of three so it means that we take this one 5 plus the job 1 we get a total of 8 and we can see here that we don't want to sense the values here are the same we don't want to pick job 7 or job 8 but rather we ignore those and we pick job 6 instead and then remember that the goal is always to return opt of n which is the maximum weight for all of the items right so in the previous slide we saw that opt of 8 is is 8 and then one might ask well we want to find which I mean how do we find which which jobs or items we we picked well we use a backtracking so we check if if the W J plus opt of p FJ is greater than opt of J minus 1 so if we take that item if that results in a total total weight that is greater if we ignore that item that means that we add item J to the optimal solution and we instead look at opt of p FJ else we ignore this item and we look at the problem as a problem of the opt of J minus 1 so a quick look at the time complexity remember that we first sorted by finish time sorting is n log n then to find the P of J for each of the J values has a time complexity of o of n log n with a clever use of binary search the I think the straightforward approach would be oh of N squared so be careful when implementing that one you'll find the maximum maximal weight assuming that we have the the P vector and unless it's sorted this is a linear time this is linear time and also to backtrack to find optimal items this is also linear so the total will be O of n log n now that we understand the actual algorithm and how it works we're ready to implement it in Python so check out the next video if you want to see how we actually implement this algorithm

Original Description

Explanation of how to solve the weighted interval scheduling problem using Dynamic Programming! In the video I explain the algorithm and give an example. In the next video we code this algorithm from scratch in Python. Link to implementation in Python: https://youtu.be/dU-coYsd7zw
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Aladdin Persson · Aladdin Persson · 22 of 60

1 computeCost.m Linear Regression Cost Function - Machine Learning
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
2 gradientDescent.m Gradient Descent Implementation -  Machine Learning
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
3 Neural Network from scratch - Part 1 (Standard Notation)
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
4 Neural Network from scratch - Part 2 (Forward Propagation)
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
5 Neural Network from scratch - Part 3 (Backward Propagation)
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
6 Neural Network from scratch - Part 4 (With Python)
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
7 sigmoid.m - Programming Assignment 2 Machine Learning
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
8 costFunction.m - Programming Assignment 2 Machine Learning
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
9 predict.m - Programming Assignment 2 Machine Learning
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
10 costFunctionReg.m - Programming Assignment 2 Machine Learning
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
11 lrCostFunction.m - Programming Assignment 3 Machine Learning
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
12 oneVsAll.m - Programming Assignment 3 Machine Learning
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
13 predictOneVsAll.m - Programming Assignment 3 Machine Learning
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
14 predict.m - Programming Assignment 3 Machine Learning
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
15 Caesar Cipher Encryption and Decryption with example
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
16 Cryptography: Caesar Cipher Python
Cryptography: Caesar Cipher Python
Aladdin Persson
17 Vigenere Cipher Explained (with Example)
Vigenere Cipher Explained (with Example)
Aladdin Persson
18 Cryptography: Vigenere Cipher Python
Cryptography: Vigenere Cipher Python
Aladdin Persson
19 Hill Cipher Explained (with Example)
Hill Cipher Explained (with Example)
Aladdin Persson
20 Cryptography: Hill Cipher Python
Cryptography: Hill Cipher Python
Aladdin Persson
21 Interval Scheduling Greedy Algorithm: Python
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
Weighted Interval Scheduling Algorithm Explained
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
23 Weighted Interval Scheduling Python Code
Weighted Interval Scheduling Python Code
Aladdin Persson
24 Sequence Alignment | Needleman Wunsch Algorithm
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
25 Sequence Alignment | Needleman Wunsch in Python
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
26 Codility BinaryGap Python
Codility BinaryGap Python
Aladdin Persson
27 Codility CyclicRotation Python
Codility CyclicRotation Python
Aladdin Persson
28 Derivation Linear Regression with Gradient Descent
Derivation Linear Regression with Gradient Descent
Aladdin Persson
29 Linear Regression Gradient Descent From Scratch in Python
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
30 Pytorch Neural Network example
Pytorch Neural Network example
Aladdin Persson
31 Pytorch CNN example (Convolutional Neural Network)
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
32 Pytorch LeNet implementation from scratch
Pytorch LeNet implementation from scratch
Aladdin Persson
33 Pytorch VGG implementation from scratch
Pytorch VGG implementation from scratch
Aladdin Persson
34 Pytorch GoogLeNet / InceptionNet implementation from scratch
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
35 How to save and load models in Pytorch
How to save and load models in Pytorch
Aladdin Persson
36 How to build custom Datasets for Images in Pytorch
How to build custom Datasets for Images in Pytorch
Aladdin Persson
37 Pytorch Transfer Learning and Fine Tuning Tutorial
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
38 Pytorch Data Augmentation using Torchvision
Pytorch Data Augmentation using Torchvision
Aladdin Persson
39 Pytorch Quick Tip: Weight Initialization
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
40 Pytorch Quick Tip: Using a Learning Rate Scheduler
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
41 Pytorch ResNet implementation from Scratch
Pytorch ResNet implementation from Scratch
Aladdin Persson
42 Pytorch TensorBoard Tutorial
Pytorch TensorBoard Tutorial
Aladdin Persson
43 Pytorch DCGAN Tutorial (See description for updated video)
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
44 Naive Bayes from Scratch - Machine Learning Python
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
45 Spam Classifier using Naive Bayes in Python
Spam Classifier using Naive Bayes in Python
Aladdin Persson
46 K-Nearest Neighbor from scratch - Machine Learning Python
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
47 Linear Regression Normal Equation Python
Linear Regression Normal Equation Python
Aladdin Persson
48 SVM from Scratch - Machine Learning Python (Support Vector Machine)
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
49 Neural Network from Scratch - Machine Learning Python
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
50 Pytorch RNN example (Recurrent Neural Network)
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
51 Pytorch Bidirectional LSTM example
Pytorch Bidirectional LSTM example
Aladdin Persson
52 Pytorch Text Generator with character level LSTM
Pytorch Text Generator with character level LSTM
Aladdin Persson
53 Logistic Regression from Scratch - Machine Learning Python
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
54 K-Means Clustering from Scratch - Machine Learning Python
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
55 Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
56 Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
57 Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
58 Paper Review: Sequence to Sequence Learning with Neural Networks
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
59 Pytorch Seq2Seq Tutorial for Machine Translation
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
60 Pytorch Seq2Seq with Attention for Machine Translation
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson

The Weighted Interval Scheduling Algorithm is a Dynamic Programming approach to find the maximum weight that can be achieved by selecting disjoint jobs. The algorithm uses recursion and backtracking to find the optimal solution, with a time complexity of O(n log n) due to sorting and binary search. The video explains the algorithm's steps and provides an example, with a link to a Python implementation.

Key Takeaways
  1. Sort jobs by finish time
  2. Define P function to remove overlapping jobs
  3. Define op J to be maximum weight that can be achieved by selecting jobs from first J intervals
  4. Compute solution for subproblem with n-1 items left
  5. Compute solution for subproblem with picked job
  6. Calculate P of J
  7. Find opt of J using recurrence formula
  8. Backtrack to find which jobs were picked
💡 The algorithm's time complexity is O(n log n) due to sorting and binary search, and the P vector must be sorted for linear time complexity.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →