Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Unit 2 Greedy algorithm

A greedy algorithm is an algorithmic strategy that makes the


best optimal choice at each small stage with the goal of this
eventually leading to a globally optimum solution.

This means that the algorithm picks the best solution at the
moment without regard for consequences.

It picks the best immediate output, but does not consider the
big picture, hence it is considered greedy.
A greedy algorithm works by choosing the best possible answer in each step
and then moving on to the next step until it reaches the end, without regard for
the overall solution. It only hopes that the path it takes is the globally optimum
one, but as proven time and again, this method does not often come up with a
globally optimum solution.
Greedy methods
How to be greedy?
• At every step, make the best move you can make
• Keep going until you’re done
Advantages
• Don’t need to pay much effort at each step
• Usually finds a solution very quickly
• The solution found is usually not bad
Possible problem
• The solution found may NOT be the best one
Greedy methods - examples
Task Scheduling
0/1 Knapsack and fractional Knapsack problem

Minimum spanning tree


• Kruskal’s algorithm
Single-source shortest-paths
• Dijkstra’s algorithm
Both algorithms find one of the BEST solutions
• greedy algorithm does NOT find the BEST solution
Task
Scheduling
Given: a set T of n tasks, each having:
 A start time, si
 A finish time, fi (where si < fi)
Goal: Perform all the tasks using a minimum number of
“machines.”

Machine 3
Machine 2
Machine 1

1 2 3 4 5 6 7 8 9
Example
Given: a set T of n tasks, each having:
 A start time, si
 A finish time, fi (where si < fi)
 T1= [1,4], T2= [1,3], T3=[2,5], T4=[3,7], T5=[4,7],
T6= [6,9], T7= [7,8] (ordered by start)
Goal: Perform all tasks on min. number of machines
Machine 3
Machine 2
Machine 1

1 2 3 4 5 6 7 8 9
Task Scheduling Algorithm
Greedy choice: consider tasks by their start time
and use as few machines as possible with this
order.
Algorithm taskSchedule(T)
Run time: O(n log n).
Input: set T of tasks w/ start time si
and finish time fi
Output: non-conflicting schedule
with minimum number of machines
m0 {no. of machines}
while T is not empty
remove task i w/ smallest si
if there ’ s a machine j for i then
schedule i on machine j
else
mm+1
schedule i on machine m
Knapsack Problem
Input: Given n items with weights w1, w2, …, wn and values v1, v2, …, vn, and a
knapsack with capacity W.
Output: Find the most valuable subset of items that can fit into the knapsack
Application: A transport plane is to deliver the most valuable set of items to a
remote location without exceeding its capacity
Example 1
capacity = 50

w = 10 w = 20 w = 30
v = 60 v = 100 v = 120
item 1 item 2 item 3 knapsack
total total
subset weight value
 0 0
{1} 10 60
{2} 20 100
{3} 30 120
{1,2} 30 160
{1,3} 40 180
{2,3} 50 220
{1,2,3} 60 N/A
Greedy approach

capacity = 50

w = 10 w = 20 w = 30
v = 60 v = 100 v = 120
item 1 item 2 item 3 knapsack
Greedy: pick the item with the next largest value if
total weight ≤ capacity.
Time complexity?
Result: O(n log n)
 item 3 is taken, total value = 120, total weight = 30
 item 2 is taken, total value = 220, total weight = 50
 item 1 cannot be taken
Does this
always work?
Example 2
capacity = 10

w=7 w=3 w=4 w=5


v = 42 v = 12 v = 40 v = 25
item 1 item 2 item 3 item 4 knapsack
total total total total
subset weight value subset weight value
 0 0 {2,3} 7 52
{1} 7 42 {2,4} 8 37
{2} 3 12 {3,4} 9 65
{3} 4 40 {1,2,3} 14 N/A
{4} 5 25 {1,2,4} 15 N/A
{1,2} 10 54 {1,3,4} 16 N/A
{1,3} 11 N/A {2,3,4} 12 N/A
{1,4} 12 N/A {1,2,3,4} 19 N/A
Greedy approach

capacity = 10

w=7 w=3 w=4 w=5


v = 42 v = 12 v = 40 v = 25
item 1 item 2 item 3 item 4 knapsack
Greedy: pick the item with the next largest value if total weight
≤ capacity.
Result:
• item 1 is taken, total value = 42, total weight = 7
• item 3 cannot be taken not the
• item 4 cannot be taken best!!
• item 2 is taken, total value = 54, total weight = 10
Greedy approach 2
v/w = 6 v/w = 4 v/w = 10 v/w = 5 capacity = 10

w=7 w=3 w=4 w=5


v = 42 v = 12 v = 40 v = 25
item 1 item 2 item 3 item 4 knapsack
Greedy 2: pick the item with the next largest (value/weight) if
total weight ≤ capacity.
Result:
Work
• item 3 is taken, total value = 40, total weight = 4
for Eg 1?
• item 1 cannot be taken
• item 4 is taken, total value = 65, total weight = 9
• item 2 cannot be taken
Greedy approach 2

v/w = 6 v/w=5 v/w = 4 capacity = 50

w = 10 w = 20 w = 30
v = 60 v = 100 v = 120
item 1 item 2 item 3 knapsack
Greedy: pick the item with the next largest
(value/weight) if total weight ≤ capacity.
Result:
 item 1 is taken, total value = 60, total weight = 10
 item 2 is taken, total value = 160, total weight = 30
 item 3 cannot be taken
Not the best!!
The Fractional Knapsack
Given: A set S of n items, with each item i having
 bi - a positive benefit
 wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
If we are allowed to take fractional amounts, then this is
the fractional knapsack problem.
 In this case, we let xi denote the amount we take of item i

 Objective: maximize

i b ( x
i i /
S
 Constraint:
wi )

i xi  W
S
Example
Given: A set S of n items, with each item i having
 bi - a positive benefit
 wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”

Solution:
• 1 ml of 5
Items: • 2 ml of 3
1 2 3 4 5
• 6 ml of 4
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2
Benefit: Rs12 Rs32 Rs40 Rs30 Rs50 10 ml
Value = bi/wi: 3 4 20 5 50
(Rs per ml)
Algorithm
The Fractional Knapsack
Greedy choice: Keep taking
item with highest value Algorithm fractionalKnapsack(S, W)
(benefit to weight ratio) Input: set S of items w/ benefit bi
 and weight wi; max. weight W
Since iSb ( x
i i / wi )   (bi / wi )xi Output: amount xi of each item i
 Run time:
iS
O(n log n). to maximize benefit w/ weight
Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W) at most W
for i = 1 to n for each item i in S
do x[i] = 0 xi  0
weight = 0 vi  b i / wi {value}
for i = 1 to n w0 {total weight}
if weight + w[i] ≤ W
while w < W
then x[i] = 1
remove item i w/ highest vi
weight = weight + w[i]
else xi  min{wi , W - w}
x[i] = (W - weight) / w[i] w  w + min{w i, W - w}
weight = W
break
return x
Consider 5 items along their respective weights and values:- I = (I1,I2,I3,I4,I5)

w = (5, 10, 20, 30, 40)


v = (30, 20, 100, 90,160)
The capacity of knapsack W = 60

I: I1 I2 I3 I4 I5
W: 5 10 20 30 40
V 30 20 100 90 160
Pi = Vi/Wi6 2 5 3 4
Now, arrange the value of pi in decreasing order.

I: I1 I3 I5 I4 I2
W 5 20 40 30 10
V 30 100 160 90 20
Pi 6 5 4 3 2
First, we choose the item I1 whose Pi is 6 and weight is 5.
Then choose item I3 whose Pi is 5 and weight is 20.
Now, the total weight of knapsack is 20 + 5 = 25
Now the next item is I5, and its Pi is 4 and weight is 40, but we want only 35, so we chose
the fractional part of it
     
Analysis of Greedy Algorithm for
Fractional Knapsack Problem
We can sort the items by their benefit-to-weight values, and then
process them in this order.
This would require O(n log n) time to sort the items and then
O(n) time to process them in the while-loop.
A greedy algorithm has five components:
1. A set of candidates, from which to create solutions.
2. A selection function, to select the best candidate to add to the solution.
3. A feasible function is used to decide if a candidate can be used to build a
solution.
4.An objective function, fixing the value of a solution or an incomplete solution.
5.An evaluation function, indicating when you find a complete solution.

You might also like