Problem Statement
A set of tasks, each with a profit and deadline, must be scheduled on a single resource (like a processor).
Each task takes one unit of time, and the goal is to maximize profit by scheduling as many tasks as possible within their deadlines.
Example
You are given the following tasks:
Task | Deadline | Profit |
---|---|---|
A | 2 | 100 |
B | 1 | 19 |
C | 2 | 27 |
D | 1 | 25 |
E | 3 | 15 |
Objective: Maximize profit while ensuring that tasks are completed within their deadlines.
Solution
The problem can be solved using a greedy algorithm:
- Sort tasks in descending order of profit.
- Assign each task to the latest available time slot before its deadline (if available).
- Skip the task if no slot is available.
Python Implementation
Here’s the $Python$ code to solve the problem:
1 | import matplotlib.pyplot as plt |
Explanation of Code
Class Definition:
ATask
class is defined to store details about each task, including its name, deadline, and profit.Sorting Tasks:
Tasks are sorted in descending order of profit to maximize the total profit.Scheduling:
For each task, the algorithm attempts to schedule it in the latest available time slot before its deadline.Visualization:
A bar chart is used to represent the profits for the scheduled tasks, with annotations for task names.
Results and Visualization
For the given example:
- Scheduled Tasks:
['A', 'C', 'E']
- Total Profit:
142
The graph displays the profits earned from scheduled tasks across time slots, with task names annotated above each bar:
- Slot 1: Task
A
(Profit = 100) - Slot 2: Task
C
(Profit = 27) - Slot 3: Task
E
(Profit = 15)
This approach ensures that the total profit is maximized while adhering to the deadlines.