0% found this document useful (0 votes)
342 views27 pages

Greedy

The document describes a greedy algorithm to solve the jumping frog problem. The problem is to find the shortest path for a frog to jump from position 0 to position n, where it can jump at most r units at a time and there are lily pads at various positions. The greedy algorithm is: (1) Always jump as far forward as possible from the current position. (2) Start at position x=0 and while x is less than n, find the furthest lily pad reachable from x that is before n, jump to it and update x. (3) Return the series of jumps.

Uploaded by

vishu joshi
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)
342 views27 pages

Greedy

The document describes a greedy algorithm to solve the jumping frog problem. The problem is to find the shortest path for a frog to jump from position 0 to position n, where it can jump at most r units at a time and there are lily pads at various positions. The greedy algorithm is: (1) Always jump as far forward as possible from the current position. (2) Start at position x=0 and while x is less than n, find the furthest lily pad reachable from x that is before n, jump to it and update x. (3) Return the series of jumps.

Uploaded by

vishu joshi
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/ 27

Lecture 6

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.

Goal: Find the path the frog should take to


minimize jumps, assuming a solution exists.

2
The Approach

Always jump as far forward as possible.

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.39 = $5 bill + $1 bill + 25¢ coin + 10¢ coin + 1¢ coin

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.

 GREEDY CHOICE PROPERTY: A globally optimal solution can be arrived at by


making a locally optimal (greedy) choice.

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

• Sort in non-increasing order of values.


First approach
Second • Sort the objects based on increasing
weight values.
approach
• Sort the objects based on decreasing
Third approach Profit/weight values.

13
First Greedy approach

14
Second Greedy approach

15
Third Greedy approach

16
Comparison of profits

Approach Description Profit value


obtained
First Greedy for Profits 28.2
Second Greedy for Weights 31
Third Greedy for Profit/Weights 31.5

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 ]

Total Profit = = 160 + (20/27) x 77 = 230 Units

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

2.Fractional knapsack problem can be solved in time O(n).


a) True
b) False

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)

4.Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity of


knapsack=20. Find the maximum value output assuming items to be divisible.
a) 60
b) 80
c) 100
d) 40

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

6. In Greedy method we get ________ Feasible solutions.


a) one
b) more than one
c) zero
d) hundred

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

You might also like