0% found this document useful (0 votes)
8 views34 pages

Lecture 6 - Greedy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views34 pages

Lecture 6 - Greedy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 6

Greedy Algorithm
What is a Greedy Method

A greedy algorithm is a mathematical process that looks for


simple, easy-to-implement solutions to complex, multi-step
problems by deciding which next step will provide the most
obvious benefit.
Greedy Methods
1. Greedy Method
2. Dynamic Programming
3. Branch and Bound

Note: These 3 Methods are for solving


Optimization Problem
Why Greedy?

Such algorithms are called greedy because while the optimal


solution to each smaller instance will provide an immediate
output, the algorithm doesn’t consider the larger problem as
a whole. Once a decision has been made, it is never
reconsidered.
The Greedy Method is used to solve Optimization Problems, a
problem which require Minimum result or Maximum result.

feasible, i.e. satisfying the constraints


Example….

A…………………………B
s1 s2 s3 s4 s5

Feasible solutions: which satisfy the


condition given in the problem or satisfying the
constraints
Note…
1. If we want to cover this Journey in minimum cost, we want to
spent less as much as possible. Now, this problem become a
Minimization Problem

2. Example: s4 and s5  are Feasible but s4 maybe taking


minimum cost so this become Optimal Solution
3. Optimal Solution: is a solution which is already feasible and
giving minimum cost.

4. There can be many solutions for a problem, there also can be


many feasible solutions but there will be definitively one
Optimal Solution
Summary

1- Feasible solution means a solution which is satisfying some


constraints.
2- Optimal Solution means which is achieving the objective of a
problem, satisfying the objective of the problem that is either
is minimum result or maximum result.

3- A problem which requires minimum or maximum result is


called Optimization Problem

Note: Greedy Method is used for solving Optimization


Problems
General Method of Greedy
Greedy(a, n)
{
for i=1 to n do n=5
{ a1 a2 a3 a4 a5
x= select(a); a
1 2 3 4 5
if Feasible(x) then
{
solution=solution+x;
}
}
}
0-1 Knapsack Problem or Fractional
Problem1 : Knapsack
n=7  7 objects in the bag
m=15  capacity of the bag

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

Container loading Problem


Let's Understand the Problem

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?

The total number of the objects that we want to fill in


the bag must be less or equal to 15kg.
Solution
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
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
15-1=14
14-2=12
12-4=8
15Kg 8-5=3
3-1=2

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

The Objective of the Problem


2. must be Max
Example…
Definition: 0/1 knapsack problem solves it by either selecting each item as
whole or none.

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

Find out profit per weight Pi/Wi

Arrange according to Pi/wi


Selection (Xi) Steps

Okay, let’s have the capacity m=6

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

What the total cost of merging the following elements

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: we have 5 files x1…x5 with their sizes, means numbers of


element in each file. By using greedy approach find a pattern in
which the total cost of merging is minimized.

Note: Greedy method said that you should always select the
smaller pair of file for merging to get the best result.
Questions

You might also like