Lecture 6 - Greedy
Lecture 6 - Greedy
Greedy Algorithm
What is a Greedy Method
A…………………………B
s1 s2 s3 s4 s5
Objects: O 1 2 3 4 5 6 7
Profits: P 10 5 15 7 6 18 3
Weights: w 2 3 5 7 1 4 1
The question of this problem is that we have to fill this bag with theses
objects, then we have to carry this bag to a different place, and we will sale
these objects and get profits.
We have to fill the objects in the bag such that the profit is maximized, so this
is an optimization problem.Can we follow the greedy method, right?
What are the constraints here?
2-2=0
Calculate the weights
Objects: O 1 2 3 4 5 6 7
Profits: P 10 5 15 7 6 18 3
Weights: w 2 3 5 7 1 4 1
P/W 5 1.3 3 1 6 4.5 3
X ( x1 x2 x3 x4 x5 x6 x7)
f 2/3 f 0 f f f
1x2+2/3x3+1x5+0x7+1x1+1x4+1x1
2+2+5+0+1+4+1 =15.
Total weights= 15
Calculate the profits
Objects: O 1 2 3 4 5 6 7
Profits: P 10 5 15 7 6 18 3
Weights: w 2 3 5 7 1 4 1
P/W 5 1.3 3 1 6 4.5 3
X ( x1 x2 x3 x4 x5 x6 x7)
f 2/3 f 0 f f f
= 1x10+2/3x5+1x15+1x6+1x18+1x3
10+2x1.3+15+6+18+3= 54.6
Total Profit=54.6
Conclusion
What is the Constraint of the Problem
1. Capacity
Problem:
For the given set of items and knapsack capacity = 6 kg, find the optimal
solution for the fractional knapsack problem making use of the greedy
approach.
Find the optimal solution for the fractional knapsack problem making use of
greedy approach. Consider:
n=4
m = 6 kg
(w1, w2, w3, w4) = (3,2,10,2)
(p1, p2, p3, p4) = (15,20,30,14)
Solution
The first profitable item we have are item no.2 so we select is 6-2=4 now the remaining
knapsack capacity is 4 and our selection is 1(means selected)
Then we have the next profitable item is item no .4, so we select 4-2=2 now the remaining
knapsack capacity is 2 and our selection is 1(means selected)
Then we have the next profitable item is item no .1 and its weight is 3 and our knapsack
remaining capacity is 2. Now we are dealing with a greedy approach and select 2/3 items.
(like take as we can )
So our selection is 2/3.
Now we don’t have the remaining capacity so we can’t take the last item no. 3 so it’s
selection is 0.
Problem 2: Job Sequencing with Deadlines
n=5
Jobs j1 j2 j3 j4 j5
Profits 20 15 10 5 1
Deadlines 2 2 1 3 3
1- We should carry out the analysis in the way that the profit
should be maximized.
2- what is the constraint of the problem: job must be completed
within the deadline.
n=5
Jobs j1 j2 j3 j4 j5
Profits 20 15 10 5 1
Deadlines 2 2 1 3 3
0--------->1---------->2--------- >3
9 10 11 12
Home work
n=7
Jobs j1 j2 j3 j4 j5 j6 j7
Profits 35 30 25 20 15 12 5
Deadlines 3 4 4 2 3 1 2
Optimal Merge Pattern
Example:
A B C
3 5
8 9 what about the cost of the merging???
12 11
List A B C D
Sizes 6 5 2 3
A B C D A B C D A B C D
6 5 2 3 6 5 2 3 6 5 2 3
A B C D
6 5 2 3
11
13
16
Cost 11+13+16 = 40
A B C D
6 5 2 3
11 5
16
Cost 11+5+16 = 32
A B C D
6 5 2 3
10
16
Cost 16+10+5 = 31
Remark:
We can conclude that: the Greedy Method we need to follow is
always we should merge
a pair of smaller size list to get the best result.
Problem
Lists x1 x2 x3 x4 x5
Sizes 20 30 10 5 30
Note: Greedy method said that you should always select the
smaller pair of file for merging to get the best result.
Questions