Unit-5 Greedy Algorithm
Unit-5 Greedy Algorithm
(ADA)
GTU # 3150703
Unit-5
Greedy Algorithms
Rupesh G. Vaishnav
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
9428037452
Outline
Looping
General Characteristics of greedy algorithms
Elements of Greedy Strategy
Make change Problem
Minimum Spanning trees (Kruskal’s algorithm, Prim’s
algorithm)
The Knapsack Problem
Job Scheduling Problem
Huffman code
Introduction
Characteristics of Greedy Algorithms
Greedy algorithms are characterized by the following features.
1. Greedy approach forms a set or list of candidates 𝑪.
2. Once a candidate is selected in the solution, it is there forever: once a candidate is excluded from the
solution, it is never reconsidered.
3. To construct the solution in an optimal way, Greedy Algorithm maintains two sets.
4. One set contains candidates that have already been considered and chosen, while the other set contains
candidates that have been considered but rejected.
Our problem is to devise an algorithm for paying a given amount to a customer using the
smallest possible number of coins.
10 2
5 1
2 1
1 1
Rupesh G. Vaishnav #3150703 (ADA) Unit 5 – Greedy Algorithms 7
Make Change - Algorithm
# Input: C = {10, 5, 2, 1, 0.5} //C is a candidate set
# Output: S: set of selected coins
Function make-change(n): set of coins
S ← Ø {S is a set that will hold the solution}
sum ← 0 {sum of the items in solution set S}
while sum ≠ n do
x ← the largest item in C such that sum + x ≤ n
if there is no such item then
return "no solution found"
S ← S U {a coin of value x}
sum ← sum + x
return S
Rupesh G. Vaishnav #3150703 (ADA) Unit 5 – Greedy Algorithms 8
Make Change – The Greedy Property
The algorithm is greedy because,
At every step it chooses the largest available coin, without worrying whether this will prove to be a
correct decision later.
It never changes the decision, i.e., once a coin has been included in the solution, it is there forever.
Examples:
1. Some coins with denominations 50, 20, 10, 5, 1 are available.
How many minimum coins required to make change for 37 cents? 5
How many minimum coins required to make change for 91 cents? 4
H H
D F D F
1 2 {1, 2} 1
1 2 3 {2, 3} 2
{4, 5} 3
4 6 4 5 6
{6, 7} 3
{1, 4} 4
4 3 5 8 6 {2, 5} 4
{4, 7} 4
7 {3, 5) 5
4 3
{2, 4} 6
{3, 6} 6
7
{5, 7} 7
{5, 6} 8
Rupesh G. Vaishnav #3150703 (ADA) Unit 5 – Greedy Algorithms 14
Kruskal’s Algorithm for MST – Example Step:2
Select the minimum weight edge but no cycle.
2
Edges Weight
1 2 {1, 2} 1
1 2 3 {2, 3} 2
{4, 5} 3
4 6 4 5 6
{6, 7} 3
{1, 4} 4
4 3 5 8 6 {2, 5} 4
{4, 7} 4
7 {3, 5) 5
4 3
{2, 4} 6
{3, 6} 6
7
{5, 7} 7
{5, 6} 8
Rupesh G. Vaishnav #3150703 (ADA) Unit 5 – Greedy Algorithms 15
Kruskal’s Algorithm for MST – Example Step:3
The minimum spanning tree for the given
2 graph.
Edges Weight
1 2 {1, 2} 1
1 2 3 {2, 3} 2
{4, 5} 3
4
{6, 7} 3
{1, 4} 4
4 3 5 6 {4, 7} 4
4 3 Total Cost = 17
Write the kruskal’s Algorithm to find out Minimum Spanning Tree. Apply the same and find
MST for the graph given below.
𝟏. 𝟐.
10
3
3 A 1
F C
A 4 4 3 7
B 2 4
C 5
8
5 6
4 B D D E F G
4
H 2 3 2 8
1 9
3
G 3
E H
1 2
1 2 3 1
4 6 4 5 6
4 3 5 8 6
7
4 3
1 2
1 2 3 1 {1, 2}, {1,
4}
4 6 1, 2 {1, 4}, {2, 3} {2, 4}, {2, 5}
6 4 5
1, 2, 3 {1,4}, {2,4}, {2,5}, {3,5},
4 3 5 8 6 {3,6}
1, 2, 3, 4 {2,4} {2,5} {3,5} {3,6}
7 {4,5} {4,7}
4 3 1, 2, 3, 4, 5 {2,4} {2,5} {3,5} {3,6}
{4,7} {5,6} {5,7}
7 1, 2, 3, 4, 5, 7 {2,4} {2,5} {3,5} {3,6}
{5,6} {5,7} {6,7}
1, 2, 3, 4, 5, 6, 7
Rupesh G. Vaishnav #3150703 (ADA) Unit 5 – Greedy Algorithms 22
Step:3
Prim’s Algorithm for MST – Example 1
The minimum spanning tree for the given
graph.
Node Edges
1 2
1 2 3 1
1, 2 {1, 2}
4
1, 2, 3 {2, 3}
1, 2, 3, 4 {1, 4}
4 3 5 6 1, 2, 3, 4, 5 {4, 5}
1, 2, 3, 4, 5, 7 {4, 7}
4 3 1, 2, 3, 4, 5, 6, 7 {6, 7}
Total Cost = 17
7
17
𝟏. 𝟐.
10 F
3
3 A 1
C
A 4 4 3 7
B 2 4
C 5
8
5 6
4 B D D E F G
4
H 2 3 2 8
1 9
3
G 3
E H
5 2
100 30
Source node = 1
10 5 Step v C 2 3 4 5
20
{2, 3, 4,
Init. - 50 30 100 10
4 3 5}
50 1 5 {2, 3, 4} 50 30 20 10
5 2
100 30
Source node = 1
10 5 Step v C 2 3 4 5
20
{2, 3, 4,
Init. - 50 30 100 10
4 3 5}
50 1 5 {2, 3, 4} 50 30 20 10
2 4 {2, 3} 40 30 20 10
5 2
100 30
Source node = 1
10 5 Step v C 2 3 4 5
20
{2, 3, 4,
Init. - 50 30 100 10
4 3 5}
50 1 5 {2, 3, 4} 50 30 20 10
2 4 {2, 3} 40 30 20 10
3 3 {2} 35 30 20 10
1. 2.
2
2 A B
B D
10 4 1 3 10
1 4 8 7 9 2 2
A C D E
3 2 5 8 4 6
C E
1
F G
Object
Fill the knapsack with given objects such that the total value of knapsack is maximized.
Job
Profit
Deadline
P 1 2 3
Job selected J2 0 J1
P 1 2 3
Job selected J2 J4 J1
This is how Huffman Coding makes sure that there is no ambiguity when decoding the
generated bit stream.
There are mainly two major parts in Huffman Coding
1. Build a Huffman Tree from input characters.
2. Traverse the Huffman Tree and assign codes to characters.
Characters a b c d e f
Frequency (in thousand) 45 13 12 16 9 5
14
14
f:5 e:9
14 d:16 25 a:45
0 1 0 1
14 d:16 25 a:45
0 1 0 1
25 30 a:45
0 1 0 1
55
0 1
25 30 a:45
0 1
0 1
a:45 55
0 1
25 30
0 1
0 1
3. Frequency
Characters a b c d e f g
Frequency (in thousand) 37 28 29 13 30 17 6