Job Scheduling समस्या
In this page:
jobs.sort(key=lambda j: j[2], reverse=True) # (id, deadline, profit)
slots = [None] * max_deadline
for job in jobs:
for t in range(min(max_deadline, job[1]) - 1, -1, -1):
if slots[t] is None:
slots[t] = job
break
Problem Idea
Deadlines के साथ Job sequencing हर job को एक deadline और एक profit देता है, और goal यह चुनना है कि असल में कौन से jobs चलाने हैं (हर job एक unit time लेती है, और per unit time सिर्फ एक job चल सकती है) total profit कमाया maximize करने के लिए।
उदाहरण: Problem Idea
#include <iostream>
using namespace std;
int main() {
int deadlines[] = {2,1,2,1,3}, profits[] = {100,19,27,25,15};
cout << "Choose jobs to maximize profit, each takes 1 unit of time, only one job per time slot";
return 0;
}
public class Main {
public static void main(String[] args) {
int[] deadlines = {2,1,2,1,3}, profits = {100,19,27,25,15};
System.out.println("Choose jobs to maximize profit, each takes 1 unit of time, only one job per time slot");
}
}
deadlines, profits = [2,1,2,1,3], [100,19,27,25,15]
print("Choose jobs to maximize profit, each takes 1 unit of time, only one job per time slot")
#include <stdio.h>
int main() {
int deadlines[] = {2,1,2,1,3}, profits[] = {100,19,27,25,15};
printf("Choose jobs to maximize profit, each takes 1 unit of time, only one job per time slot");
return 0;
}
Login to try C/C++/Java code in the editor
Sort by Profit
यहां safe greedy order profit के अनुसार है, highest से lowest तक — सबसे valuable jobs को पहले consider करना उन्हें उनकी deadline गुज़रने से पहले एक available time slot ढूंढने का best मौका देता है।
उदाहरण: Sort by Profit
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<pair<int,int>> jobs = {{2,100},{1,19},{2,27},{1,25},{3,15}};
sort(jobs.begin(), jobs.end(), [](auto&a, auto&b){ return a.second > b.second; });
cout << "Highest profit job first: deadline=" << jobs[0].first << " profit=" << jobs[0].second;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
int[][] jobs = {{2,100},{1,19},{2,27},{1,25},{3,15}};
Arrays.sort(jobs, (a,b) -> b[1]-a[1]);
System.out.println("Highest profit job first: deadline=" + jobs[0][0] + " profit=" + jobs[0][1]);
}
}
jobs = [(2,100),(1,19),(2,27),(1,25),(3,15)]
jobs.sort(key=lambda j: -j[1])
print(f"Highest profit job first: deadline={jobs[0][0]} profit={jobs[0][1]}")
#include <stdio.h>
int main() {
int jobs[5][2] = {{2,100},{1,19},{2,27},{1,25},{3,15}};
for (int i = 0; i < 5; i++)
for (int j = i+1; j < 5; j++)
if (jobs[j][1] > jobs[i][1]) { int t0=jobs[i][0],t1=jobs[i][1]; jobs[i][0]=jobs[j][0]; jobs[i][1]=jobs[j][1]; jobs[j][0]=t0; jobs[j][1]=t1; }
printf("Highest profit job first: deadline=%d profit=%d", jobs[0][0], jobs[0][1]);
return 0;
}
Login to try C/C++/Java code in the editor
Find a Slot
उस order में हर job के लिए, algorithm latest available time slot ढूंढता है जो अब भी उस job की deadline पर या इससे पहले हो; जितना early possible के बजाय जितना late possible schedule करना वह trick है जो इस greedy approach को काम कराती है।
उदाहरण: Find a Slot
#include <iostream>
using namespace std;
int main() {
int slots[4] = {0,0,0,0};
int deadline = 2, jobId = 7;
for (int t = deadline; t >= 1; t--)
if (slots[t] == 0) { slots[t] = jobId; cout << "Placed job " << jobId << " at slot " << t; break; }
return 0;
}
public class Main {
public static void main(String[] args) {
int[] slots = new int[4];
int deadline = 2, jobId = 7;
for (int t = deadline; t >= 1; t--)
if (slots[t] == 0) { slots[t] = jobId; System.out.println("Placed job " + jobId + " at slot " + t); break; }
}
}
slots = [0]*4
deadline, job_id = 2, 7
for t in range(deadline, 0, -1):
if slots[t] == 0:
slots[t] = job_id
print(f"Placed job {job_id} at slot {t}")
break
#include <stdio.h>
int main() {
int slots[4] = {0,0,0,0};
int deadline = 2, jobId = 7;
for (int t = deadline; t >= 1; t--)
if (slots[t] == 0) { slots[t] = jobId; printf("Placed job %d at slot %d", jobId, t); break; }
return 0;
}
Login to try C/C++/Java code in the editor
Maximize Profit
Earliest के बजाय latest available slot भरना profit-sorted order में बाद में आने वाली earlier deadlines वाली jobs के लिए earlier, ज़्यादा constrained slots खुले छोड़ता है — बहुत जल्दी उपयोग हुआ एक earlier slot एक tight-deadline job को कहीं न जाने दे सकता है।
उदाहरण: Maximize Profit
#include <iostream>
using namespace std;
int main() {
cout << "Filling latest slot first leaves earlier, tighter slots open for later, earlier-deadline jobs";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Filling latest slot first leaves earlier, tighter slots open for later, earlier-deadline jobs");
}
}
print("Filling latest slot first leaves earlier, tighter slots open for later, earlier-deadline jobs")
#include <stdio.h>
int main() {
printf("Filling latest slot first leaves earlier, tighter slots open for later, earlier-deadline jobs");
return 0;
}
Login to try C/C++/Java code in the editor
Practice
Mixed deadlines और profits वाली कुछ jobs try करें: profit से sort करें, फिर हर एक को उसकी deadline पर या इससे पहले latest अब भी खुले slot में place करें, और देखें total profit एक naive earliest-slot approach से कैसे compare करता है।
उदाहरण: Practice
#include <iostream>
using namespace std;
int main() {
cout << "Sort by profit, place each job in the latest still-open slot at or before its deadline";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Sort by profit, place each job in the latest still-open slot at or before its deadline");
}
}
print("Sort by profit, place each job in the latest still-open slot at or before its deadline")
#include <stdio.h>
int main() {
printf("Sort by profit, place each job in the latest still-open slot at or before its deadline");
return 0;
}
Login to try C/C++/Java code in the editor
- Profit के बजाय deadline से sort करना, इसलिए सस्ती jobs valuable वालों से slots ले लेती हैं।
- किसी job को इसकी deadline से पहले latest free slot के बजाय earliest free slot में रखना।
- Deadline को available slots की संख्या तक cap न करना, इसलिए slot index bounds से बाहर है।
- Greedy algorithms हर step पर best local choice करते हैं।
- Activity selection, fractional knapsack, और job scheduling classic greedy problems हैं।
- Huffman coding compact codes बनाने के लिए greedy approach उपयोग करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: