Greedy Approach
Greedy Approach
V. Balasubramanian
SSN College of Engineering
Introduction
• Charles Dickens’ - Ebenezer Scrooge
greedy person ever, fictional or real.
• Each day his only drive was to
greedily grab as much gold as he
could.
• Christmas Carol
Contd…
• They often lead to very efficient and
simple solutions.
• Like dynamic programming, greedy
algorithms are often used to solve
optimization problems.
• The greedy approach is more
straightforward.
• DP, a recursive property is used to
divide an instance into smaller
instances.
Contd…
• In the greedy approach, there is no
division into smaller instances. A
greedy algorithm arrives at a
solution by making a sequence of
choices, each of which simply looks
the best at the moment. That is,
each choice is locally optimal. The
hope is that a globally optimal
solution will be obtained, but this is
not always the case.
Greedy Stratergy
Example
• penny, nickel, dime, quarter, half
dollar
• D1 = 25 (quarter), d2= 10 (dime),
• d3= 5 (nickel), and d4= 1 (penny).
• Change for 36,48
Example
• dime, nickel, and penny and 12 paise
coin
• Change for 16
• Example:
Example
• Coins 1,3,4
• Change for 6.
Approach
• A selection procedure chooses the next item to
add to the set. The selection is performed
according to a greedy criterion that satisfies
some locally optimal consideration at the time.
• • A feasibility check determines if the new set
is feasible by checking whether it is possible to
complete this set in such a way as to give a
solution to the instance.
• • A solution check determines whether the new
set constitutes a solution to the instance.
Greedy Technique
• Constructs a solution to an optimization problem
piece by piece through a sequence of choices that
are:
• feasible
• locally optimal
• irrevocable
• For some problems, yields an optimal solution for
every instance.
• For most, does not but can be useful for fast
approximations.
Applications
• Optimal solutions:
– change making for “normal” coin
denominations
– minimum spanning tree (MST)
– single-source shortest paths
– simple scheduling problems
– Huffman codes
Coin Change Problem
Given unlimited amounts of coins of denominations d1 > … >
dm ,
give change for amount n with the least number of coins
Greedy solution:
Greedy solution is
• optimal for any amount and “normal’’ set of denominations
• may not be optimal for arbitrary coin denominations
Minimum Spanning Tree (MST)
• Spanning tree of a connected graph
G: a connected acyclic subgraph of G
that includes all of G’s vertices