Greedy Algorithm
Coin Change Algorithm
Prepared By: Kafeel Hassan
Department: CS
Semester: 7th
Introduction to Greedy Algorithm
• A Greedy Algorithm is a problem-solving
approach that makes the locally optimal
choice at each step with the hope of finding
the global optimum solution.
• It works by choosing the best option available
at the current moment without considering
the overall problem, assuming that this local
optimization will lead to the best global result.
Characteristics of Greedy
Algorithm
• 1. Greedy Choice Property: A global optimal
solution can be reached by choosing the best
local option at each step.
• 2. Optimal Substructure: A problem has an
optimal substructure if an optimal solution to
the problem can be constructed from optimal
solutions of its subproblems.
General Steps
• 1. Initialization: Start with an empty solution.
• 2. Selection: Pick the best option available at
the current step.
• 3. Feasibility Check: Ensure the chosen option
does not violate any constraints.
• 4. Inclusion: Add the selected option to the
solution.
• 5. Repeat until all steps are complete or no
more choices are available.
Advantages and Disadvantages
• Advantages:
• - Simplicity: Easy to implement.
• - Efficiency: Often faster since it avoids
exhaustive search.
• Disadvantages:
• - Doesn't guarantee the global optimal
solution in all cases.
• - May fail for problems that require
Common Applications
• 1. Activity Selection Problem: Maximizing the
number of activities you can perform within
given time constraints.
• 2. Huffman Coding: Data compression using
minimum-length binary encoding.
• 3. Kruskal's Algorithm: Finding the minimum
spanning tree in a graph.
• 4. Dijkstra's Algorithm: Finding the shortest
path in a weighted graph.
Coin Change Algorithm
• The Coin Change Problem involves finding the
minimum number of coins needed to make a
specific amount of change using coins of given
denominations.
• There are two approaches to solving this:
• - Greedy Algorithm
• - Dynamic Programming
Steps in the Greedy Algorithm
• 1. Sort the coins in descending order of their
value.
• 2. Start with the largest denomination and
select as many of those coins as possible
without exceeding the target amount.
• 3. Subtract the value of the selected coins
from the target amount.
• 4. Repeat with the next largest denomination
until the target amount becomes zero.
Example 1: Coins that Allow the
Greedy Solution
• Problem: Find the minimum coins to make 37
units using denominations {1, 5, 10, 25}.
• Steps:
• 1. Sort denominations: {25, 10, 5, 1}.
• 2. Use 1 coin of 25 → Remaining = 37 - 25 =
12.
• 3. Use 1 coin of 10 → Remaining = 12 - 10 = 2.
• 4. Use 2 coins of 1 → Remaining = 2 - (2 × 1) =
Example 2: Coins that Fail with the
Greedy Solution
• Problem: Find the minimum coins to make 30
units using denominations {1, 9, 10}.
• Steps:
• 1. Sort denominations: {10, 9, 1}.
• 2. Use 3 coins of 10 → Remaining = 30 - (3 ×
10) = 0.
• Greedy Solution Output: {10, 10, 10} → Total
= 3 coins.
Summary
• 1. Greedy Algorithm works well when coin
denominations allow a locally optimal choice
to result in a globally optimal solution.
• 2. Dynamic Programming is preferred when
denominations can cause greedy solutions to
fail, as it examines all possible combinations to
ensure an optimal result.