Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Lecture 15 - Gready Algorithm 1-1

Uploaded by

swatikar.2708
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)
14 views

Lecture 15 - Gready Algorithm 1-1

Uploaded by

swatikar.2708
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/ 22

Lecture 15

Greedy algorithms
Greedy Algorithm
• A greedy algorithm is an approach for solving a problem by selecting
the best option available at the moment. It doesn't worry whether the
current best result will bring the overall optimal result.
• The algorithm never reverses the earlier decision even if the choice is
wrong. It works in a top-down approach.
• This algorithm may not produce the best result for all the problems.
It's because it always goes for the local best choice to produce the
global best result.

2
1. Greedy Choice Property
If an optimal solution to the problem can be found by choosing the best choice at
each step without reconsidering the previous steps once chosen, the problem can
be solved using a greedy approach. This property is called greedy choice
property.

2. Optimal Substructure
If the optimal overall solution to the problem corresponds to the optimal solution
to its subproblems, then the problem can be solved using a greedy approach. This
property is called optimal substructure.

3
Greedy Approach
1. Let's start with the root node 20. The weight of the right child is 3 and the weight of the left child is 2.
2. Our problem is to find the largest path. And, the optimal solution at the moment is 3. So, the greedy algorithm
will choose 3.
3. Finally the weight of an only child of 3 is 1. This gives us our final result 20 + 3 + 1 = 24.
However, it is not the optimal solution. There is another path that carries more weight (20 + 2 + 10 = 32)

4
5
Greedy Algorithm

1.To begin with, the solution set (containing answers) is empty.


2.At each step, an item is added to the solution set until a solution is
reached.
3.If the solution set is feasible, the current item is kept.
4.Else, the item is rejected and never considered again.

6
7
8
Greedy Algorithm
• Follows local optimal choice at each stage with intend of
finding global optimum.

• Feasible Solution (Selection)

• Optimal Solution

• Minimum Cost
• Maximum Profit
• Minimum Risk
9
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.

10
The Approach

Always jump as far forward as possible.

11
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.

12
The Solution

13
14
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
15
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.

16
17
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

18
Greedy Challenges
Hard to verify : Proving that a greedy algorithm is
correct is more of an art than a science.

19
Where to use Greedy?

Optimal
Sub-structure Greedy Choice
Property

20
 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.
21
22

You might also like