Greedy Algorithm
Greedy Algorithm
Introduction
Greedy Algorithm
• Greedy algorithms make the choice that looks
best at the moment.
• Disadvantages
– Does not always lead to a global optimal solution.
3
Example
• Find the Shortest Path From a Graph
4
Greedy Algorithms
6
Objective
Pay out a given sum S with the smallest
number of coins possible.
7
Coin Change
• Assume that we have an unlimited number of coins of
various denominations:
$1, $5, $10, $25, $50
41 – 25 = 16 ------ 25
16 – 10 = 6 -------10
6 – 5=1 ------- 5
1 –1=0 -------1
We have to give: 25 10 5 1
8
Making Change – A big problem - 1
• Assume that we have an unlimited number of coins of
various denominations:
$4 , $10, $25
41 – 25 = 16 ------ 25
16 – 10 = 6 ------- 10
6– 4 =2 ------- 4
2 ------- ???
We should choose 25 4 4 4 4
9
Making Change – A big problem -2
• Example 2: Coins are valued $30, $20, $5, $1
10
Algorithm
• The greedy coin changing algorithm:
while S > 0 do
c := value of the largest coin no larger than S;
num := S / c;
pay out num coins of value c;
S := S - num*c;
11
Greedy Algorithms
13
Bin Packing
• In the bin packing problem, objects of different volumes must be
packed into a finite number of bins or containers each of volume V
in a way that minimizes the number of bins used.
14
Ways of Solution
15
Find The Lower Bound
Total size = 12 + 12 + 7 + 6
+ 10 + 4 + 5 + 1 = 57
Bin Size = 20
16
First Fit Algorithm
17
First Fit
18
First Fit Solution
19
First Fit Decreasing Algorithm
20
First Fit Decreasing
21
First Fit Decreasing Solution
22
Full Bin Algorithm
23
Full Bin Concept
24
Full Bin Example
25
Full Bin Solution
26
Analysis
• FIRST FIT
– Easiest to use
– Isn’t optimal
• FULL BIN
– Optimal
– Difficult to use
27
Try By yourself
28
Greedy Algorithms
• Fractional Knapsack
– You can take fractional amount of an item.
30
Concept of Knapsack Problem
• A Set of Items are given where,
– No of Items = n [X1, X2, X3, ….. Xn]
– Weight of Xi = Wi
– Profit of Xi = Pi
– Knapsack Size = m
• Goal:
– Maximize the Profit ΣPiXi
within ΣWiXi <= m
31
Fractional Knapsack
32
Fractional Knapsack problem
ITEM PRICE WEIGHT Three Cases:
1. Maximum Price
X1 25 18 2. Minimum Weight
3. Maximum Pi/Wi
X2 24 15
X3 15 10 X1 X2 X3 Σ WiXi Σ PiXi
1 2/15 0 20 28.2
Total Item , n = 10
0 10/15 1 20 31
Knapsack Size , m = 20
0 1 1/2 20 31.5
33
0/1 Knapsack
34
Drawbacks of Greedy Methods
35