0% found this document useful (0 votes)
244 views24 pages

UNIT 3.1 (Greedy Method - Knapsack Problem)

The document discusses greedy algorithms and their application to the fractional knapsack problem. It defines greedy algorithms as making locally optimal choices at each step that may lead to a global optimum. The fractional knapsack problem aims to maximize the total value of items placed in a knapsack without exceeding its weight capacity. The greedy approach sorts items by their value-to-weight ratio and adds items in descending order until the knapsack is full, choosing fractions of items as needed. When applied to a sample problem, this greedy method finds the optimal solution that maximizes total value within the weight constraint.

Uploaded by

sejal
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)
244 views24 pages

UNIT 3.1 (Greedy Method - Knapsack Problem)

The document discusses greedy algorithms and their application to the fractional knapsack problem. It defines greedy algorithms as making locally optimal choices at each step that may lead to a global optimum. The fractional knapsack problem aims to maximize the total value of items placed in a knapsack without exceeding its weight capacity. The greedy approach sorts items by their value-to-weight ratio and adds items in descending order until the knapsack is full, choosing fractions of items as needed. When applied to a sample problem, this greedy method finds the optimal solution that maximizes total value within the weight constraint.

Uploaded by

sejal
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/ 24

Greedy Methods

UNIT 3
Greedy Algorithms
• Used in optimization problem
• Sequence of steps
• N inputs
• Find subsets that satisfies some constraints ( feasible solution )
• Objective function ( maximize or minimize )
• Feasible solution that dose this -> optimal solution
GENERAL STRATEGY OF GREEDY
ALGORITHM
• A greedy algorithm always makes the choice that looks best at the
moment
• Makes a locally optimal choice that may be lead to a globally optimal
solution.
• Simpler and more efficient compared to optimization algorithms
Two key ingredients in greedy algorithm

• Greedy choice property

• Optimal substructure
Greedy choice property
• Locally optimal (greedy) choice (depend on choices so far )
• Best choice in the current problem, without considering results from
the sub-problems.
• Solve the sub-problems after the choice is made.
• Algorithm progress in a top down manner, making one greedy choice
one after another,
Optimal substructure
• A problem is said to have optimal substructure if an optimal solution
can be constructed efficiently from optimal solution to its sub-
problem.
• How many sub-problems
• How many choices
• In Greedy algorithm a sub-problem is created by having made the
greedy choice in the original problem.
• Here, an optimal solution to the sub-problem, combined with the
greedy choice already made, yield an optimal solution to the original
problem.
• The function Select selects an input from a[ ] and removes it. The
selected input's value is assigned to x. Feasible is a Boolean-valued
function that determines whether x can be included into the solution
vector. The function Union combines x with the solution and updates
the objective function.
Algorithm
Greedy(a,n)
// a[l:n] contains the n inputs.
• {solution:=0 ; // Initialize the solution.
• for i :=1 to n do
• { x :=Select(a);
• if Feasible (solution , x) then
• solution:= Union (solution, x);
•}
• return solution
The Knapsack Problem.
The Knapsack Problem
• We are given n objects and a knapsack.
• Object i has a weight wi > 0 and profit pi >0
• the knapsack has a capacity M.
• If a fraction xi (0 ≤ xi ≤ 1) of object i is placed into the knapsack, then
a profit of pixi > 0 is earned.
• The objective is to obtain a filling of the knapsack that maximizes the
total profit earned.
• Mathematically, the problem may be formulated as follows.

• Max i= 1 to n ∑ pixi (1)


• s.t. i=1 to n ∑ wixi ≤ M (2)
• 0 ≤ xi ≤ 1 (3)
• A feasible solution is any set (x1, x2, …, xn) satisfying (2) and (3).
• An optimal solution is a feasible solution that maximizes (1).
• Greedy method 1 : largest profit first.

• Objects are selected in non increasing order of pi , i. e.,

• p1 ≥ p2 ≥ … ≥ pn.
• Greedy method 2 : smallest weight first.

• Objects are selected in non decreasing order of wi

• , i. e., w1 ≤w2 ≤ … ≤wn.


• Greedy method 3 : maximal profit per unit of capacity first.

• Objects are selected in non increasing order of pi /wi ,

i.e., p1/w1 ≥ p2/ws2 ≥ … ≥ pn/wn.

The time complexities of the above three greedy methods are all O(nlog n).
Greedy method 3 always generates an optimal solution to the knapsack
problem.
Example
• Consider the following instance of the knapsack problem:
• n = 3, m= 20, (p1 ,p2 ,P3 ) = (25,24,15), and
(w1 ,w2,w3)= (18,15,10).
Four feasible solutions are:
(Xi, X2, X3 ) ∑ wixi ∑ Pixi
1. (1/2,1/3, 1/4) 16.5 24.25 ( 25*1/2 +24*1/3+15*1/4)
2. (1, 2/15, 0) 20 28.2 ( 25 + 24*2/15 +0 )
3. (0, 2/3, 1) 20 31 ( 0 + 24*2/3 + 15 )
4. (0, 1, 1/2) 20 31.5 ( 0+ 24+ 15*1/2 )
Algorithm ?
.
 For the given set of items and knapsack capacity = 60 kg, find the optimal solution for the
fractional knapsack problem making use of greedy approach

Item Weight Value

1 5 30

2 10 40

3 15 45

4 22 77

5 25 90
• Find the optimal solution for the fractional knapsack problem making
use of greedy approach. Consider-
• n=5
• w = 60 kg
• (w1, w2, w3, w4, w5) = (5, 10, 15, 22, 25)
• (b1, b2, b3, b4, b5) = (30, 40, 45, 77, 90)
Compute the value / weight ratio for each item-

Items Weight Value Ratio

1 5 30 6

2 10 40 4

3 15 45 3

4 22 77 3.5

5 25 90 3.6
• Sort all the items in decreasing order of their value / weight ratio-
• 
• I1 I2 I5 I4 I3
• (6) (4) (3.6) (3.5) (3)
• 
Start filling the knapsack by putting the items into it one by one.

Knapsack Weight Items in Knapsack Total value

60 Ø 0
55 I1 30

45 I1, I2 70

20 I1, I2, I5 160


• Knapsack weight left to be filled is 20 kg but item-4 has a weight of
22 kg.
• Since in fractional knapsack problem, even the fraction of any item
can be taken.
• So, knapsack will contain the following items-

• < I1 , I2 , I5 , (20/22) I4 >


•  answer : ( 1 , 1 , 0 , 10/11, 1 )
• Total value of the knapsack
• = 160 + (20/22) x 77
• = 160 + 70
• = 230 units
• 

You might also like