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

The Greedy Method

• An optimisation problem (OP) is a problem that involves searching


through a set of configurations to find one that minimises or maximizes
an objective function defined on these configurations
• The greedy method solves a given OP going through a sequence of
(feasible) choices
• The sequence starts from well-understood starting configuration, and
then iteratively makes the decision that seems best from all those that
are currently possible.
The Greedy Method
• The greedy approach does not always lead to an optimal solution.
• The problems that have a greedy solution are said to posses the
greedy-choice property.
• The greedy approach is also used in the context of hard (difficult to
solve) problems in order to generate an approximate solution.
Fractional Knapsack Problem

In fractional knapsack problem, where we are given a set S of n


items, s.t., each item i has a positive benefit(profit or value) bi and a
positive weight wi, and we wish to find the maximum-benefit subset
that doesn’t exceed a given weight W (total capacity of the
knapsack).

We are also allowed to take arbitrary fractions of each item.


Fractional Knapsack Problem

• I.e., we can take an amount xi of each item i such


that

The total benefit of the items taken is


determined by the objective function
One Relatable Example
One Relatable Example
• You are studying for an exam and you have to study N
questions/topics.
• The questions/topics take {t1, t2, t3, ...., tn} time(in
hours) and carry {m1, m2, m3, ...., mn} marks.
• You can study for a maximum of T hours.
Fractional Knapsack Problem - Greedy
Fractional knapsack problem satisfies the greedy-
choice property, hence, Given an instance of a
fractional knapsack problem with set S of n items,
we can construct a maximum benefit subset of S.
Knapsack Problem
Fractional Knapsack Problem
Example 1
Item A B C
Value or Profit 60 100 120
• Input:  Weight 20 20 30

Items as (value, weight) pairs 

Knapsack Capacity, W = 50; 

• Value/Weight Ratio
A (60/20=3), B (100/20=5), C=(120/30=4)
Example 1
Item B C A
Value or Profit 100 120 60
Output:  Weight 20 30 20
Value/weight 5 4 3
Taking B
• Profit = 100
• Remaining Capacity = 30
Taking C
• Profit = 100 + 120 = 220
• Remaining Capacity = 0
Considering Item A
Since, Wight of A > Remaining Capacity we can not take A
Total profit = 220
Item A B C
Example 2 Value or Profit 60 100 120
Weight 20 20 30
• Input: 
Items as (value, weight) pairs 

Knapsack Capacity, W = 60; 

• Value/Weight Ratio
A (60/20=3), B (100/20=5), C=(120/30=4)
Example 2
Item B C A
Value or Profit 100 120 60
Output:  Weight 20 30 20
Taking B Value/weight 5 4 3
• Profit = 100
• Remaining Capacity = 40
Taking C
• Profit = 100 + 120 = 220
• Remaining Capacity = 10
Taking A
Since, Weight of A > Remaining Capacity we can not take A completely, only 10/20 can be taken in
to the sack.
Total profit = 220 + (10/20)*60 = 220 + 30 = 250.
Fractional Knapsack Problem - Complexity
In the solution we use a heap-based PQ to store the items of S, where the
key of each item is its value index
With PQ, each greedy choice, which removes an item with the greatest
value index, takes O(log n) time
The fractional knapsack algorithm can be implemented in time O(n log n).

You might also like