0
0
DSA Cprogramming

Job Scheduling with Deadlines in DSA C

Choose your learning style9 modes available
Mental Model
We want to schedule jobs so that we finish as many as possible before their deadlines, choosing the most profitable ones first.
Analogy: Imagine you have several tasks to do before certain times, but you only have limited hours. You pick the most rewarding tasks that fit your schedule first.
Jobs: [Job1: profit=20, deadline=2]
      [Job2: profit=15, deadline=2]
      [Job3: profit=10, deadline=1]
      [Job4: profit=5, deadline=3]
      [Job5: profit=1, deadline=3]
Slots: 1 -> 2 -> 3 -> null (time slots available)
Dry Run Walkthrough
Input: Jobs: (id=1, profit=20, deadline=2), (id=2, profit=15, deadline=2), (id=3, profit=10, deadline=1), (id=4, profit=5, deadline=3), (id=5, profit=1, deadline=3)
Goal: Schedule jobs to maximize total profit without missing deadlines
Step 1: Sort jobs by profit descending
Jobs order: 1(20,2) -> 2(15,2) -> 3(10,1) -> 4(5,3) -> 5(1,3)
Why: We want to pick the most profitable jobs first to maximize profit
Step 2: Schedule Job 1 at latest free slot before deadline 2 (slot 2)
Slots: 1 -> [2: Job1] -> 3 -> null
Why: Slot 2 is free and before deadline 2, so assign Job 1 there
Step 3: Schedule Job 2 at latest free slot before deadline 2 (slot 1)
[1: Job2] -> [2: Job1] -> 3 -> null
Why: Slot 2 is taken, so assign Job 2 to slot 1 which is free and before deadline 2
Step 4: Try to schedule Job 3 before deadline 1 (slot 1 taken)
[1: Job2] -> [2: Job1] -> 3 -> null
Why: Slot 1 is occupied, no free slot before deadline 1, so skip Job 3
Step 5: Schedule Job 4 at latest free slot before deadline 3 (slot 3)
[1: Job2] -> [2: Job1] -> [3: Job4] -> null
Why: Slot 3 is free and before deadline 3, assign Job 4 there
Step 6: Try to schedule Job 5 before deadline 3 (slots 1,2,3 taken)
[1: Job2] -> [2: Job1] -> [3: Job4] -> null
Why: No free slot before deadline 3, skip Job 5
Result:
Slots: [1: Job2] -> [2: Job1] -> [3: Job4] -> null
Total profit = 15 + 20 + 5 = 40
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Job {
    int id;
    int deadline;
    int profit;
} Job;

// Compare function for qsort to sort jobs by profit descending
int compare(const void *a, const void *b) {
    Job *jobA = (Job *)a;
    Job *jobB = (Job *)b;
    return jobB->profit - jobA->profit;
}

// Function to find max deadline
int findMaxDeadline(Job jobs[], int n) {
    int max = 0;
    for (int i = 0; i < n; i++) {
        if (jobs[i].deadline > max) {
            max = jobs[i].deadline;
        }
    }
    return max;
}

// Job Scheduling function
void jobScheduling(Job jobs[], int n) {
    // Sort jobs by profit descending
    qsort(jobs, n, sizeof(Job), compare);

    int maxDeadline = findMaxDeadline(jobs, n);

    // Initialize time slots to -1 (free)
    int *slots = (int *)malloc(maxDeadline * sizeof(int));
    for (int i = 0; i < maxDeadline; i++) {
        slots[i] = -1;
    }

    int totalProfit = 0;
    int countJobs = 0;

    // Iterate over jobs
    for (int i = 0; i < n; i++) {
        // Find a free slot for this job, starting from last possible slot
        for (int j = jobs[i].deadline - 1; j >= 0; j--) {
            if (slots[j] == -1) {
                slots[j] = jobs[i].id; // Assign job id to slot
                totalProfit += jobs[i].profit;
                countJobs++;
                break; // Move to next job
            }
        }
    }

    // Print scheduled jobs in order of slots
    printf("Scheduled Jobs in slots:\n");
    for (int i = 0; i < maxDeadline; i++) {
        if (slots[i] != -1) {
            printf("Slot %d: Job %d\n", i + 1, slots[i]);
        } else {
            printf("Slot %d: Free\n", i + 1);
        }
    }
    printf("Total jobs scheduled: %d\n", countJobs);
    printf("Total profit: %d\n", totalProfit);

    free(slots);
}

int main() {
    Job jobs[] = {
        {1, 2, 20},
        {2, 2, 15},
        {3, 1, 10},
        {4, 3, 5},
        {5, 3, 1}
    };
    int n = sizeof(jobs) / sizeof(jobs[0]);

    jobScheduling(jobs, n);

    return 0;
}
qsort(jobs, n, sizeof(Job), compare);
sort jobs by profit descending to prioritize high profit jobs
for (int j = jobs[i].deadline - 1; j >= 0; j--) {
search for latest free slot before job deadline
if (slots[j] == -1) {
check if slot is free
slots[j] = jobs[i].id;
assign job to slot
totalProfit += jobs[i].profit;
add job profit to total
countJobs++;
increment count of scheduled jobs
break;
stop searching slots after scheduling job
OutputSuccess
Scheduled Jobs in slots: Slot 1: Job 2 Slot 2: Job 1 Slot 3: Job 4 Total jobs scheduled: 3 Total profit: 40
Complexity Analysis
Time: O(n log n) because we sort the jobs by profit, then for each job we scan slots up to its deadline which is at most maxDeadline ≤ n
Space: O(n) for the slots array to track scheduled jobs
vs Alternative: Naive approach tries all permutations of jobs which is factorial time; this greedy method is efficient and practical
Edge Cases
No jobs
No scheduling happens, output shows all slots free or no slots
DSA C
qsort(jobs, n, sizeof(Job), compare);
All jobs have deadline 1
Only one job with highest profit is scheduled, others skipped
DSA C
for (int j = jobs[i].deadline - 1; j >= 0; j--) {
Multiple jobs with same deadline and profit
Jobs are scheduled in order they appear after sorting; ties broken by input order
DSA C
qsort(jobs, n, sizeof(Job), compare);
When to Use This Pattern
When you need to schedule tasks with deadlines to maximize profit or count, use the greedy job scheduling pattern by sorting jobs by profit and placing them in latest free slots before deadlines.
Common Mistakes
Mistake: Scheduling jobs in order of deadlines instead of profit
Fix: Sort jobs by profit descending first to prioritize high profit jobs
Mistake: Assigning jobs to earliest free slot instead of latest before deadline
Fix: Search slots backward from deadline to find latest free slot
Mistake: Not checking if slot is free before assigning job
Fix: Check if slot is -1 (free) before assignment
Summary
Schedules jobs to maximize total profit by assigning each job to the latest free slot before its deadline.
Use when you have tasks with deadlines and profits and want to maximize profit by scheduling without conflicts.
Sort jobs by profit descending and assign each to the latest available slot before its deadline to get the best profit.