04 Greedy
04 Greedy
cse.iitkgp.ac.in
Algorithms – I (CS21203)
Greedy Algorithms
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Greedy Algorithms
• Greedy algorithm repeatedly makes locally best choice/decision
ignoring what its effect will be in future
Optimization Problems
• A class of problems in which we are asked to
• Find a set (or a sequence) of “items”
• That satisfy some constraints and simultaneously optimize (i.e., maximize
or minimize) some objective function
• Formally
Knapsack Problem
• Given items of known weights and values and a knapsack of
capacity , find the most valuable subset of the items that fit into the
knapsack
• Two variations of Knapsack problem is there
• (Fractional Knapsack): You are allowed to take fractions of items
• (0-1 Knapsack): You have to take an item either whole or none
•Objective: Total value in the knapsack
•Constraints: Sum of weights of items in Knapsack can’t exceed the
knapsack capacity. (In 0-1 version) Only whole item or none
Item 3
Item 2
Item 1
30 Kg 50 Kg
20 Kg
10 Kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 4
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• Which variation of the Knapsack problem should be easy to solve?
•Any simple approach to solve the fractional Knapsack problem?
•Get the per unit value of the items and fill out the knapsack starting
with the item highest per unit value and go on in a descending order
•The total value is coming to be Rs. 240
•The strategy here is literally “greedy”
•It is also optimal in this case Total value =
60+100+4*20 =
4 Rs/kg Item 3 Rs. 240
5 Rs/kg Item 2
6 Rs/kg Item 1 20 kg
30 kg 50 Kg
20 kg 20 kg
10 kg
10 kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 5
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• What may come challenging sometimes is proving a greedy
technique indeed gives optimal solution
• For fractional knapsack its relatively easy
• Intuitively: For the first 10 kg space of the knapsack we are putting
the best possible option (item 1).
• For the next 20 kg space we going for the best possible option
available and so on Total value =
• Formal proof will come later 60+100+4*20 =
4 Rs/kg Item 3 Rs. 240
5 Rs/kg Item 2
6 Rs/kg Item 1 20 kg
30 kg 50 Kg
20 kg 20 kg
10 kg
10 kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 6
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• Lets see how this strategy works for 0-1 Knapsack problem
• But this is not the optimal solution. We can do better
• What is the optimal solution?
Total value =
60+100 =
4 Rs/kg Item 3 Rs. 160
5 Rs/kg Item 2
6 Rs/kg Item 1 0 kg
30 kg 50 Kg
20 kg 20 kg
10 kg
10 kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 7
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• Lets see how this strategy works for 0-1 Knapsack problem
• But this is not the optimal solution. We can do better
• What is the optimal solution?
• Full of item 3 and item 2
• It was easy for this example as the number of items are only 3
• In general, for items, you need to check combinations
Total value =
120+100 =
4 Rs/kg Item 3 Rs. 220
5 Rs/kg Item 2
6 Rs/kg Item 1 20 kg
30 kg 50 Kg
20 kg
10 kg 30 kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 8
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• The greedy strategy that worked for the fractional Knapsack
problem, did not work for the 0-1 Knapsack problem
• Lets try another greedy strategy
• Starting with the item highest total value and go on in a
descending order
• For this instance, it gives the optimal solution
• Any instance/example where this strategy fails? Total value =
120+100 =
4 Rs/kg Item 3 Rs. 220
5 Rs/kg Item 2
6 Rs/kg Item 1 20 kg
30 kg 50 Kg
20 kg
10 kg 30 kg
Rs. 60 Rs. 100 Rs. 120
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 9
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• Add a 4 item with more weight but with slightly high value
th
Item 3
Item 2 40 kg
Item 1
30 kg 50 Kg
20 kg
10 kg
Rs. 60 Rs. 100 Rs. 120 Rs. 130
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 10
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Knapsack Problem
• We have tried two different greedy strategies to solve 0-1
Knapsack problem. But none of them worked
• Do we have any greedy strategy that works for 0-1 Knapsack
problem?
• To the best of our knowledge there is no known greedy strategy
that works for 0-1 Knapsack problem
• Later we will see other approaches to solve a 0-1 Knapsack
problem Item 4
Item 3
Item 2 40 kg
Item 1
30 kg 50 Kg
20 kg
10 kg
Rs. 60 Rs. 100 Rs. 120 Rs. 130
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 11
image source: www.indiamart.com
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
1 2 4 2 5 8 9 11 13
8 5 7 3 9 10 11 14 16
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
1 2 4 2 5 8 9 11 13
8 5 7 3 9 10 11 14 16
1
2
3
4
5
6
7
8
9
1 2 4 2 5 8 9 11 13
8 5 7 3 9 10 11 14 16
1
2
3
4
5
6
7
8
9
Shortest Duration
1 2 3 4 5 6 7 8 9
1 2 4 2 5 8 9 11 13
8 5 7 3 9 10 11 14 16
7 3 3 1 4 2 2 3 3
❌ ❌ ✅ ✅ ❌ ✅ ❌ ✅ ❌
• So, our greedy solution is the activity subset
• Is it optimal?
• In this case – yes
• We can show that for this problem, we can at max choose 4 actions
• We are deferring the formal proof for later
• However, this greedy strategy may not be optimal for all instances
Earliest Start
• What about choosing by early start?
• Is it optimal?
• No – in general
Earliest Finish
• What about choosing by early finish?
• Is it optimal?
• Yes
1 2 3 4 5 6 7 8 9
1 2 4 2 5 8 9 11 13
8 5 7 3 9 10 11 14 16
7 3 3 1 4 2 2 3 3
❌ ❌ ✅ ✅ ❌ ✅ ❌ ✅ ❌
• So, our greedy solution is the activity subset
• Note: optimal solution is not unique. Another candidate
Alternate Strategy
• Given that ‘earliest finish’ strategy works, using symmetry what can
be an alternate strategy that will also work?
• Hint: Think why ‘earliest finish’ strategy works
Merge Pebbles
• We have piles of pebbles:
Merge Pebbles
• Lets try to merge the piles in the order they are given
12 7 8 15 4
42 4
27 15
19 8
Energy cost: 19 + 27 + 42 + 46 = 134
12 7 Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 29
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
46
42 4
19 23
Energy cost: 19 + 23 + 42 + 46 = 130
12 7 8 15 Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 30
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
11 8 12 15
Energy cost: 11 + 19 + 27 + 46 = 103
4 7 Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 31
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
19 8
11 8 12 15
12 7 Cost: 134
4 7 Cost: 103 Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 32
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
27 15
19 27
19 8
11 8 12 15
12 7 Cost: 134
4 7 Cost: 103 Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 33
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
46 +12 -7 46
19 27 24 22
11 8 12 15 16 8 7 15
4 7 4 12
Total cost: 4x3 + 7x2 + 8x2 + 12x3 + 15x2 = 108
Total cost: 4x3 + 7x3 + 8x2 + 12x2 + 15x2 = 103
Source: UC Riverside, CS141 course, Fall 2021
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms 34
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Huffman Codes
• How data is represented in computers?
• Using binary (0’s and 1’s) codes
• Fixed-size codes, e.g., ASCII
• A: 1000001 (65)
• B: 1000010 (66)
• Fixed size codes may not necessarily be the best way to
store/communicate data
• Any other ways?
Huffman Codes
• Variable size codes like Morse codes
• A:
• B:
• E:
• T:
•Invented in 1800s. Image source: https://tinyurl.com/4mncb26k
• We would like to find a binary code that encodes the file using as
few bits as possible, i.e., the compression is maximum
Prefix Codes
• No code should sit in front of any other code
• Termed as “Prefix code” [Though the book rightly says “Perhaps
prefix-free codes would be a better name”]
• Morse code is not “Prefix code” [Or called as Non-prefix code]
• Encoding means simply concatenate all the codes
Character Prefix code Non-prefix code
a 0 00
b 101 001
c 100 11
d 111 111
e 1101 01
f 1100 010
Prefix Codes
Character Prefix code Non-prefix code
a 0 00
b 101 001
c 100 11
d 111 111
e 1101 01
f 1100 010
• Decoding is unambiguous
• Example:
• Message: ‘DABA’
• Encoded message: 11101010
• Decoding “11101010” – greedily decode it!
25 30
0 1 0 1
12 13 14 16
100: c 101: b 0 1 111: d
1100: f 5 9 1101:•e is the depth (or height) of the leaf
which is also equal to the length of
the codeword associated with that
Aug 25, 30, 31, 2023
leaf
CS21203 / Algorithms - I | Greedy Algorithms 44
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Taken from wikipedia. Attribution to author: By The original uploader was Aris00 at English Wikipedia. - Transferred from
en.wikipedia to Commons. Transfer was stated to be made by User:Ddxc., CC BY-SA 3.0,
https://commons.wikimedia.org/w/index.php?curid=3177632
• Note that
• Repeat this procedure, called merge with new alphabet until an
alphabet with only one symbol is left
• The resulting tree is the Huffman tree giving Huffman code
20
0 1
5 15
Aug 25, 30, 31, 2023
c d
CS21203 / Algorithms - I | Greedy Algorithms 47
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
20 33
0 1 0 1
5 15 15 18
Aug 25, 30, 31, 2023
c d
CS21203 / Algorithms - I | Greedy Algorithms
b a 48
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
0 53 1
20 33
0 1 0 1
5 15 15 18
Aug 25, 30, 31, 2023
c d
CS21203 / Algorithms - I | Greedy Algorithms
b a 49
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
0 53 1 45
e
20 33
0 1 0 1
5 15 15 18
Aug 25, 30, 31, 2023
c d
CS21203 / Algorithms - I | Greedy Algorithms
b a 50
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
0 98 1
0 53 1 45
e
20 33
0 1 0 1
5 15 15 18
c d b a
𝑓 𝑓 𝑓 𝑓 𝑓 𝑓 𝑓
𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏 𝑥 𝑦 𝑎 𝑏
• The last case makes the lemma trivially true (as exchanging and
does not change anything on the objective function)
• So we will assume [Other wise that will make ]
)
[Rest of the nodes, as unchanged, get cancelled]
)
=
≥0 ≥0
[by, (4)]
[by, (3)] This is a contradiction that is optimal for . Thus must be
optimal for 58
Aug 25, 30, 31, 2023 CS21203 / Algorithms - I | Greedy Algorithms
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Tutorial Problems
• Given an infinite array in which the first elements are integers in
sorted order and the rest of the cells are filled with some special
symbol (say $). Assume we do not know the value. Give an
algorithm that takes an integer as input and finds a position in the
array containing , if the integer exists in the array in time
Tutorial Problems
• Given a sorted array of non-repeated integers A[1..n], check
whether there is an index for which . Give a divide-and-conquer
algorithm that runs in time
Tutorial Problems
• We are given two sorted arrays of size n. Give an algorithm for
finding the median element in the union of the two lists so that the
complexity is
Thank You