Lecture 15 - Gready Algorithm 1-1
Lecture 15 - Gready Algorithm 1-1
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
6
7
8
Greedy Algorithm
• Follows local optimal choice at each stage with intend of
finding global optimum.
• 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.
10
The Approach
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.
At each step, take the largest possible bill or coin that does not
overshoot.
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.