Greedy
Greedy
Greedy algorithms
The Jumping Frog
• The frog begins at position 0 in the river.
Its goal is to get to position n.
• There are lily pads at various positions.
There is always a lily pad at position 0
and position n.
• The frog can jump at most r units at a
time.
2
The Approach
3
The Algorithm
1. Let J be an empty series of jumps.
2. Let our current position x = 0.
3. While x < n:
Find the furthest lily pad l reachable from x that is not after position n.
Add a jump to J from x to l's location.
Set x to l's location.
4. Return J.
4
The Solution
5
Optimisation Problem
An optimization problem is one in which you want to find, not just a solution, but the best
solution.
Example: Suppose you want to count out a certain amount of money, using the fewest
possible bills and coins. How will you approach it?
At each step, take the largest possible bill or coin that does not overshoot.
6
Greedy Algorithms
• A greedy algorithm is an algorithm that constructs an object X one step at a time, at each
step choosing the locally best option.
• At each phase:
• You take the best you can get right now, without regard for future consequences.
• You hope that by choosing a local optimum at each step, you will end up at a global
optimum.
7
Greedy Advantages
Simplicity
• It is quite easy to come up with a greedy algorithm for a problem.
Easy to Analyse
• Analyzing the run time for greedy algorithms will generally be much easier than for
other techniques
8
Greedy Challenges
Hard to verify : Proving that a greedy algorithm is correct is more of an art than a
science.
9
Where to use Greedy?
Optimal
Sub-structure Greedy Choice
Property
10
OPTIMAL SUB-STRUCTURE: A problem exhibits optimal substructure if
an optimal solution to the problem contains within its optimal solutions to
subproblems.
Ex: That is, if the shortest route from Seattle to Los Angeles passes through Portland
and then Sacramento, then the shortest route from Portland to Los Angeles must
pass through Sacramento too.
11
Knapsack Problem
Given: 1) A set of items, each with a weight and a value.
2) Capacity/weight of Knapsack.
Goal: Determine a subset of items to include in a collection so that the total weight is
less than or equal to a given limit and the total value is as large as possible.
12
Greedy approach to Knapsack Problem
13
First Greedy approach
14
Second Greedy approach
15
Third Greedy approach
16
Comparison of profits
Maximum Profit
17
Fractional Knapsack algorithm
Fractional Knapsack (Array W, Array V, int M)
for i 1 to size (V)
calculate cost[i] V[i] / W[i]
Sort-Descending (cost)
i←1
while (i <= size(V))
if W[i] <= M
M ← M – W[i]
total ← total + V[i];
if W[i] > M
i ← i+1
18
Complexity Analysis
• The main time taking step is the sorting of all items in the decreasing order of their
value / weight ratios.
If using a simple sort algorithm (selection, bubble…) then the complexity of the whole problem
is O(n2).
If using quick sort or merge sort then the complexity of the whole problem is O(nlogn).
• If the items are already arranged in the required order, the while loop takes O(n)
time.
19
In Class Practice
Q. 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.
20
Solution
Step 1: Compute the value / weight ratio for each item-
21
Step 2: Sort all the items in the decreasing order of their value / weight ratios-
Step 3: Start filling the knapsack by putting the items in it one by one.
22
• Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg.
• But because in fractional knapsack problem, we can even take the fraction of any
item.
• So, our knapsack will contain the items-
[ I1 , I2 , I5 , (20/22) I4 ]
23
Quiz Questions
1. What is the objective of the knapsack problem?
a) To get maximum total value in the knapsack
b) To get minimum total value in the knapsack
c) To get maximum weight in the knapsack
d) To get minimum weight in the knapsack
24
Quiz Questions
3. Time complexity of fractional knapsack problem is ____________
a) O(n log n)
b) O(n)
c) O(n2)
d) O(nW)
25
Quiz Questions
5. The main time taking step in fractional knapsack problem is ___________
a) Breaking items into fraction
b) Adding items into knapsack
c) Sorting
d) Looping through sorted items
26
Solutions
1. a)
2. a)
3. a)
4. a)
Explanation: The value/weight ratio are-{2,3,4}. So we include the second and third
items wholly into the knapsack. This leaves only 5 units of volume for the first item.
So we include the first item partially. Final value = 20+30+(40/4)=60.
5. c)
6. a)
27