Greedy Algorithms and
JobScheduling
To.
D.P. Khem Sir.
Present by :
Krisha Patel(230130107053)
Hemanshi Malaviya(230130107060)
Analysis And Design Of
Algorithms (3150703)
2.
What is aGreedy Strategy?
Key Characteristics
• Safe Move Property: The local choice doesn't compromise the global solution.
• Optimal Substructure: Optimal solution contains optimal solutions to subproblems.
• No Backtracking: Once a choice is made, we never reconsider it.
Definition :-
An algorithmic paradigm that makes the locally optimal choice at
each stage with the hope of finding a global optimum.
3.
Greedy vs DynamicProgramming
Greedy Algorithm
• Makes one choice and solves
remaining subproblem
• Commits to decisions immediately
• Time complexity typically O(n log n)
• Works when greedy choice is
always safe
Dynamic Programming
• Explores all choices and selects
the best
• Stores results of subproblems
• Higher time complexity but more
versatile
• Required when greedy choices
may be suboptimal
Key Insight: When a greedy choice is always safe, use greedy
algorithms for their simplicity and efficiency. Otherwise, consider
dynamic programming for comprehensive exploration.
4.
The Job SchedulingProblem Defined
Given a set of jobs J = {J₁, J₂, ..., Jₙ}, each with a deadline
(dᵢ) and a profit (pᵢ). All jobs take exactly one unit of
time. Find a schedule that maximises the total ofit.
Problem Statement
Deadline Constraint
Each job must be completed by its specified deadline, or it cannot contribute any
profit to the solution.
Profit Maximisation
Our objective is to select and schedule jobs to achieve the highest possible
total profit.
Unit Time Jobs
The simplification that all jobs take exactly one time unit allows us to focus
on scheduling logic.
Given a set of jobs J = {J₁, J₂, ..., Jₙ}, each with a deadline (dᵢ) and a
profit (pᵢ). All jobs take exactly one unit of time. Find a schedule
that maximises the total profit.
Problem Statement
5.
The Greedy Strategyfor Job Scheduling
Arrange all jobs in non-increasing order of their profit values (pᵢ).
Higher profit jobs get priority consideration.
Step 1: Sort by Profit
Iterate through sorted jobs and select a job if and only if it can be completed by its deadline,
given the jobs already scheduled.
Step 3: Schedule Assignment
For each selected job, assign it to the latest available time slot
before its deadline to maintain maximum flexibility for future jobs.
Step 2: Greedy Selection
Core Principle: Prioritising high-profit jobs first ensures we capture
maximum value while maintaining feasibility.
6.
Example :
Let's workthrough a concrete example to see how our greedy strategy performs
in practice.
Job Profit (pᵢ) Deadline (dᵢ) Status
J₁
J2
J3
J4
₹20
₹15
₹10
₹5
2
3
2
1
Our task is to schedule these four jobs to maximise total profit while respecting their
deadlines.
Pending
Pending
Pending
Pending
7.
Step 1: Sortby Profit
First, we arrange our jobs in descending order of profit to prioritise high-value
opportunities.
J₁: ₹20 profit, deadline 2
Highest profit job gets first consideration in our greedy selection process.
J₂: ₹15 profit, deadline 2
Second highest profit, same deadline as J₁ - potential scheduling conflict.
J3: ₹10 profit, deadline 1
Medium profit but very tight deadline constraint - early scheduling required.
J4: ₹5 profit, deadline 3
Lowest profit but most flexible deadline - good fallback option.
2
1
3
4
8.
Step 2: GreedyScheduling Process
Select J₁ (₹20, deadline 2)
Schedule at time slot 2Available slots before deadline 2: {1, 2}. We choose slot 2
to maintain flexibility.
Select J₂ (₹15, deadline 2)
Schedule at time slot 1Only slot 1 remains available before deadline 2. J₂ gets
scheduled.
Consider J3 (₹10, deadline 2)
Reject - no available slotsTime slot 1 is occupied by J₂. J₃ cannot meet its
deadline.
Select J4 (₹5, deadline 3)
Schedule at time slot 3Slot 3 is available and meets the deadline requirement.
2
3
4
1
Final Schedule: {J₂, J₁, J₄} at time slots {1, 2, 3}Total Profit: ₹15 + ₹20 + ₹5
= ₹40
9.
Why Does ThisGreedy Strategy Work?
Safe Move Property
Selecting the highest
available profit job that
meets its deadline never
compromises our ability to
find the optimal solution.
This choice is always "safe".
Optimal Substructure
After making a greedy
choice, the remaining
problem has the same
structure. Optimal solution
to the whole problem
contains optimal solutions
to subproblems.
Profit Density
Maximisation
Since all jobs take one time
unit, prioritising higher
profits maximises our profit
per time slot - the optimal
density strategy.
Key Insight: For Job Scheduling with deadlines and profits, our greedy approach of prioritising highest
profit jobs that meet their deadlines yields the mathematically optimal solution. The local optimization
aligns perfectly with global optimization.
Time Complexity: O(n log n) for sorting + O(n²) for scheduling = O(n²) overall complexity.