Chapter -3
Greedy Algorithms
Optimization Problems
For most optimization problems you want to
find, not just a solution, but the best
solution.
A greedy algorithm sometimes works well
for optimization problems. It works in
phases. At each phase:
You take the best you can get right now,
without regard for future consequences.
You hope that by choosing a local optimum at
each step, you will end up at a global optimum.
Example: Counting Money
Suppose you want to count out a certain amount
of money, using the fewest possible bills and
coins
A greedy algorithm to do this would be:
At each step, take the largest possible bill or
coin that does not overshoot
Example: To make $6.39, you can choose:
a $5 bill
a $1 bill, to make $6
a 25¢ coin, to make $6.25
A 10¢ coin, to make $6.35
four 1¢ coins, to make $6.39
For US money, the greedy algorithm always
gives the optimum solution
Other Greedy Algorithms
Dijkstra’s algorithm for finding the shortest
path in a graph
Always takes the shortest edge connecting a
known node to an unknown node
Kruskal’s algorithm for finding a minimum-
cost spanning tree
Always tries the lowest-cost remaining edge
Prim’s algorithm for finding a minimum-
cost spanning tree
Always takes the lowest-cost edge between
nodes in the spanning tree and nodes not yet in
the spanning tree
Schedule
• a problem to find the most optimal sequence
of Jobs when executed in a single processor
operating system, to obtain the maximum
profit. There are many sequences of Jobs
possible, Since we need the most optimal
sequence, hence it is a greedy problem.
Schedule……….
Problem Statement:
• In the table below, jobs with their profits and
deadlines are given. What would be the
optimal sequencing of jobs which will give
maximum profit?
Problem Statement:
Jobs Deadlines Profits
Job 1 5 200
Job 2 3 180
Job 3 3 190
Job 4 2 300
Job 5 4 120
Job 6 2 100
Solution:
• Step 1:
Sort the jobs in decreasing order of their profit.
Jobs Deadlines Profits
Job 4 2 300
Job 1 5 200
Job 3 3 190
Job 2 3 180
Job 5 4 120
Job 6 2 100
Sol…..
• Step 2:
• Here we can see that value of the maximum
deadline is 5.
• Its Gantt chart will be :
Sol…..
• Step 3
Now, pick the jobs one by one as presented in
step, 1, and place them on the Gantt chart as far
as possible from 0.
• We will pick Job 4. Its deadline is 2. So placing
the job in the empty slot available just before
the deadline
Sol…..
• We will now pick Job 1. Its deadline is 5. So
placing the job in the empty slot available just
before the deadline.
Sol…..
• We will now pick Job 3. Its deadline is 3. So
placing the job in the empty slot available just
before the deadline.
• We will now pick Job 2. Its deadline is 3. Here second and
third slots are already filled. So place job 2 on the next
available free slot farthest from 0, i.e first slot.
Sol…..
• We will now pick Job 5. Its deadline is 4. Place the job in the
first empty slot before the deadline,i.e fourth slot.
• We will now pick Job 6. Its deadline is 2. Now we need to place
the job in the first empty slot before the deadline. Since, no
such slot is available, hence Job 6 can not be completed.
• So, the most optimal sequence of jobs to maximize profit is Job
2, Job 4, Job 3, Job 5, and Job 1.
• And the maximum profit earned can be calculated as:
• Profit of Job 2 + Profit of Job 4 + Profit of Job 3 + profit of Job 5
+ profit of Job 1
• =180+300+190+120+200=990=180+300+190+120+200=990