73
Greedy Algorithms Antonio Carzaniga Faculty of Informatics University of Lugano January 26, 2007 c 2007 Antonio Carzaniga

Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Greedy Algorithms

Antonio Carzaniga

Faculty of InformaticsUniversity of Lugano

January 26, 2007

c© 2007 Antonio Carzaniga

Page 2: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Outline

Greedy strategy

Examples

Activity selection

Huffman coding

c© 2007 Antonio Carzaniga

Page 3: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

c© 2007 Antonio Carzaniga

Page 4: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

We naturally decompose the problem in a series of choices

c© 2007 Antonio Carzaniga

Page 5: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

We naturally decompose the problem in a series of choices

◮ at each point we have a partial solution A ⊆ T

c© 2007 Antonio Carzaniga

Page 6: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

We naturally decompose the problem in a series of choices

◮ at each point we have a partial solution A ⊆ T◮ we have a number of choices on how to extend A

c© 2007 Antonio Carzaniga

Page 7: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

We naturally decompose the problem in a series of choices

◮ at each point we have a partial solution A ⊆ T◮ we have a number of choices on how to extend A◮ we make a “greedy” choice by selecting the lightest edge that

does not violate the constraints of the MST problem

c© 2007 Antonio Carzaniga

Page 8: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Recap on MST Algorithms

Find the MST of G = (V ,E) with w : E → R

◮ find a T ⊆ E that is a minimum-weight spanning tree

We naturally decompose the problem in a series of choices

◮ at each point we have a partial solution A ⊆ T◮ we have a number of choices on how to extend A◮ we make a “greedy” choice by selecting the lightest edge that

does not violate the constraints of the MST problem

GENERIC-MST(G,w)

1 A← ∅2 while A is not a spanning tree3 do find a safe edge e = (u, v) � the lightest that. . .4 A← A∪ {e}

c© 2007 Antonio Carzaniga

Page 9: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Designing a Greedy Algorithm

c© 2007 Antonio Carzaniga

Page 10: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Designing a Greedy Algorithm

1. Cast the problem as one where

◮ we make a choice, and

◮ we are left with a subproblem

c© 2007 Antonio Carzaniga

Page 11: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Designing a Greedy Algorithm

1. Cast the problem as one where

◮ we make a choice, and

◮ we are left with a subproblem

2. Prove that there is always an optimal solution to the originalproblem that makes the greedy choice

◮ i.e., that the greedy choice always leads to an optimal solution

◮ not necessarily always the same one

c© 2007 Antonio Carzaniga

Page 12: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Designing a Greedy Algorithm

1. Cast the problem as one where

◮ we make a choice, and

◮ we are left with a subproblem

2. Prove that there is always an optimal solution to the originalproblem that makes the greedy choice

◮ i.e., that the greedy choice always leads to an optimal solution

◮ not necessarily always the same one

3. Prove that the remaining subproblem is such that

◮ combining the greedy choice with the optimal solution of thesubproblem, we obtain an optimal solution to the original problem

c© 2007 Antonio Carzaniga

Page 13: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

The Greedy-Choice Property

The first key ingredient of a greedy strategy is the following

greedy-choice property: one can always arrive at aglobally optimal solution by making a locally optimalchoice

c© 2007 Antonio Carzaniga

Page 14: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

The Greedy-Choice Property

The first key ingredient of a greedy strategy is the following

greedy-choice property: one can always arrive at aglobally optimal solution by making a locally optimalchoice

At every step, we consider only what is best in the currentproblem

◮ not considering the results of the subproblems

c© 2007 Antonio Carzaniga

Page 15: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Optimal Substructure

The second key ingredient of a greedy strategy is the following

optimal-substructure property: an optimal solution tothe problem contains within it optimal solutions tosubproblems

c© 2007 Antonio Carzaniga

Page 16: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Optimal Substructure

The second key ingredient of a greedy strategy is the following

optimal-substructure property: an optimal solution tothe problem contains within it optimal solutions tosubproblems

It is natural to prove this by induction

◮ if the solution to the subproblem is optimal, then combining thegreedy choice with that solution yields an optimal solution

c© 2007 Antonio Carzaniga

Page 17: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem

c© 2007 Antonio Carzaniga

Page 18: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

c© 2007 Antonio Carzaniga

Page 19: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem

c© 2007 Antonio Carzaniga

Page 20: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem◮ greedy choice: pick xi such that v(xi) = maxx∈X v(x)

◮ subproblem: X ′ = X − {xi}, k ′ = k − 1 (same value function v )

c© 2007 Antonio Carzaniga

Page 21: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem◮ greedy choice: pick xi such that v(xi) = maxx∈X v(x)

◮ subproblem: X ′ = X − {xi}, k ′ = k − 1 (same value function v )

Greedy-choice property

c© 2007 Antonio Carzaniga

Page 22: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem◮ greedy choice: pick xi such that v(xi) = maxx∈X v(x)

◮ subproblem: X ′ = X − {xi}, k ′ = k − 1 (same value function v )

Greedy-choice property◮ if v(xi) = maxx∈X v(x), then there is a globally optimal solution A

that contains xi

c© 2007 Antonio Carzaniga

Page 23: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem◮ greedy choice: pick xi such that v(xi) = maxx∈X v(x)

◮ subproblem: X ′ = X − {xi}, k ′ = k − 1 (same value function v )

Greedy-choice property◮ if v(xi) = maxx∈X v(x), then there is a globally optimal solution A

that contains xi

Optimal-substructure property

c© 2007 Antonio Carzaniga

Page 24: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

The absolutely trivial gift-selection problem◮ out of a set X = {x1, x2, . . . , xn} of valuable objects, where v(xi) is

the value of xi◮ you are given, as a gift, a set S of k objects of your choice◮ how do you maximize the total value of your gifts?

Decomposition: choice plus subproblem◮ greedy choice: pick xi such that v(xi) = maxx∈X v(x)

◮ subproblem: X ′ = X − {xi}, k ′ = k − 1 (same value function v )

Greedy-choice property◮ if v(xi) = maxx∈X v(x), then there is a globally optimal solution A

that contains xi

Optimal-substructure property◮ if v(xi) = maxx∈X v(x) and A′ is an optimal solution for

X ′ = X − {xi}, then A′ ⊂ Ac© 2007 Antonio Carzaniga

Page 25: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Making Change

My favorite pasta lunch typically costs Fr. 13.20; I usually paywith a Fr. 20 bill, and get Fr. 6.80 of change

c© 2007 Antonio Carzaniga

Page 26: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Making Change

My favorite pasta lunch typically costs Fr. 13.20; I usually paywith a Fr. 20 bill, and get Fr. 6.80 of change

Question: how can I get the least amount of coins?

(The following denominations are available: 5, 1, 0.5, 0.2, 0.1)

c© 2007 Antonio Carzaniga

Page 27: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Making Change

My favorite pasta lunch typically costs Fr. 13.20; I usually paywith a Fr. 20 bill, and get Fr. 6.80 of change

Question: how can I get the least amount of coins?

(The following denominations are available: 5, 1, 0.5, 0.2, 0.1)

Solution: 5 + 1 + 0.5 + 0.2 + 0.1 = 6.8

c© 2007 Antonio Carzaniga

Page 28: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Making Change

My favorite pasta lunch typically costs Fr. 13.20; I usually paywith a Fr. 20 bill, and get Fr. 6.80 of change

Question: how can I get the least amount of coins?

(The following denominations are available: 5, 1, 0.5, 0.2, 0.1)

Solution: 5 + 1 + 0.5 + 0.2 + 0.1 = 6.8

Is this a greedy problem?

Suppose you are in the US and need to make $6.80 of change;suppose that the available denominations are $5, $1, $0.25,$0.1, and $.01 (you are out of “nickels”)

c© 2007 Antonio Carzaniga

Page 29: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Making Change

My favorite pasta lunch typically costs Fr. 13.20; I usually paywith a Fr. 20 bill, and get Fr. 6.80 of change

Question: how can I get the least amount of coins?

(The following denominations are available: 5, 1, 0.5, 0.2, 0.1)

Solution: 5 + 1 + 0.5 + 0.2 + 0.1 = 6.8

Is this a greedy problem?

Suppose you are in the US and need to make $6.80 of change;suppose that the available denominations are $5, $1, $0.25,$0.1, and $.01 (you are out of “nickels”)

Greedy: 5 + 1 + 3× 0.25 + 5× 0.01 = 6.8 (10 coins/bills)

Optimal: 5 + 1 + 2× 0.25 + 3× 0.1 = 6.8 (7 coins/bills)

c© 2007 Antonio Carzaniga

Page 30: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry

Problem: choose which items to take to maximize the total valueof the robbery

c© 2007 Antonio Carzaniga

Page 31: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry

Problem: choose which items to take to maximize the total valueof the robbery

Is this a greedy problem?

c© 2007 Antonio Carzaniga

Page 32: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry

Problem: choose which items to take to maximize the total valueof the robbery

Is this a greedy problem?

No

c© 2007 Antonio Carzaniga

Page 33: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Fractional Knapsack Problem

c© 2007 Antonio Carzaniga

Page 34: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Fractional Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry◮ the thief may take any fraction of an item (with the corresponding

proportional value)

Problem: choose which items, or fractions of items to take tomaximize the total value of the robbery

c© 2007 Antonio Carzaniga

Page 35: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Fractional Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry◮ the thief may take any fraction of an item (with the corresponding

proportional value)

Problem: choose which items, or fractions of items to take tomaximize the total value of the robbery

Is this a greedy problem?

c© 2007 Antonio Carzaniga

Page 36: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Fractional Knapsack Problem

A thief robbing a store finds n items

◮ vi is the value of item i◮ wi is the weight of item i◮ W is the maximum weight that the thief can carry◮ the thief may take any fraction of an item (with the corresponding

proportional value)

Problem: choose which items, or fractions of items to take tomaximize the total value of the robbery

Is this a greedy problem?

Yes

c© 2007 Antonio Carzaniga

Page 37: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Activity-Selection Problem

A conference room is shared among different activities

◮ S = {a1, a2, . . . , an} is the set of proposed activities◮ activity ai has a start time si and a finish time fi◮ so, two activities ai and aj are compatible if either fi ≤ sj or fj ≤ si

Problem: find the largest set of compatible

Example

activity 1 2 3 4 5 6 7 8 9 10 11start 8 0 2 3 5 1 5 3 12 6 8

finish 12 6 13 5 7 4 9 8 14 10 11

Is there a greedy solution for this problem?

c© 2007 Antonio Carzaniga

Page 38: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Activity-Selection Problem (2)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

123456789

1011

c© 2007 Antonio Carzaniga

Page 39: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Activity-Selection Problem (3)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

1

2

3

4

5

6

78

9

1011

c© 2007 Antonio Carzaniga

Page 40: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Activity-Selection Problem (3)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

1

2

3

4

5

6

78

9

1011

c© 2007 Antonio Carzaniga

Page 41: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding

Suppose you have a large sequence S of the six characters: ‘a’,‘b’, ‘c’, ‘d’, ‘e’, and ‘f’

◮ e.g., n = |S| = 109

What is the most efficient way to store that sequence?

c© 2007 Antonio Carzaniga

Page 42: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding

Suppose you have a large sequence S of the six characters: ‘a’,‘b’, ‘c’, ‘d’, ‘e’, and ‘f’

◮ e.g., n = |S| = 109

What is the most efficient way to store that sequence?

First approach: compact fixed-width encoding

c© 2007 Antonio Carzaniga

Page 43: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding

Suppose you have a large sequence S of the six characters: ‘a’,‘b’, ‘c’, ‘d’, ‘e’, and ‘f’

◮ e.g., n = |S| = 109

What is the most efficient way to store that sequence?

First approach: compact fixed-width encoding

◮ 6 symbols require 3 bits per symbol

c© 2007 Antonio Carzaniga

Page 44: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding

Suppose you have a large sequence S of the six characters: ‘a’,‘b’, ‘c’, ‘d’, ‘e’, and ‘f’

◮ e.g., n = |S| = 109

What is the most efficient way to store that sequence?

First approach: compact fixed-width encoding

◮ 6 symbols require 3 bits per symbol

◮ 3× 109/8 = 3.75× 108 (a bit less than 400Mb)

c© 2007 Antonio Carzaniga

Page 45: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding

Suppose you have a large sequence S of the six characters: ‘a’,‘b’, ‘c’, ‘d’, ‘e’, and ‘f’

◮ e.g., n = |S| = 109

What is the most efficient way to store that sequence?

First approach: compact fixed-width encoding

◮ 6 symbols require 3 bits per symbol

◮ 3× 109/8 = 3.75× 108 (a bit less than 400Mb)

Can we do better?

c© 2007 Antonio Carzaniga

Page 46: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding (2)

c© 2007 Antonio Carzaniga

Page 47: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding (2)

Consider the following encoding table:

symbol codea 000b 001c 010d 011e 100f 101

c© 2007 Antonio Carzaniga

Page 48: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Coding (2)

Consider the following encoding table:

symbol codea 000b 001c 010d 011e 100f 101

Observation: the encoding of ‘e’ and ‘f’ is a bit redundant

◮ the second bit does not help us in distinguishing ‘e’ from ‘f’◮ in other words, if the first (most significant) bit is 1, then the

second bit gives us no information, so it can be removed

c© 2007 Antonio Carzaniga

Page 49: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Idea

c© 2007 Antonio Carzaniga

Page 50: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Idea

Variable-length code

symbol codea 000b 001c 010d 011e 10f 11

Encoding and decoding are well-defined and unambiguous

c© 2007 Antonio Carzaniga

Page 51: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Idea

Variable-length code

symbol codea 000b 001c 010d 011e 10f 11

Encoding and decoding are well-defined and unambiguous

How much space do we save?

c© 2007 Antonio Carzaniga

Page 52: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Idea

Variable-length code

symbol codea 000b 001c 010d 011e 10f 11

Encoding and decoding are well-defined and unambiguous

How much space do we save?◮ not knowing the frequency of ‘e’ and ‘f’, we can’t tell exactly

c© 2007 Antonio Carzaniga

Page 53: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Idea

Variable-length code

symbol codea 000b 001c 010d 011e 10f 11

Encoding and decoding are well-defined and unambiguous

How much space do we save?◮ not knowing the frequency of ‘e’ and ‘f’, we can’t tell exactly

Given the frequencies fa, fb , fc , . . . of all the symbols in S

M = 3n(fa + fb + fc + fd ) + 2n(fe + ff )c© 2007 Antonio Carzaniga

Page 54: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition

c© 2007 Antonio Carzaniga

Page 55: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition

Given a set of symbols C and a frequency function f : C → [0, 1]

Find a code E : C → {0, 1}∗ such that

c© 2007 Antonio Carzaniga

Page 56: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition

Given a set of symbols C and a frequency function f : C → [0, 1]

Find a code E : C → {0, 1}∗ such that

E is a prefix code

◮ no codeword E(c1) is the prefix of another codeword E(c2)

c© 2007 Antonio Carzaniga

Page 57: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition

Given a set of symbols C and a frequency function f : C → [0, 1]

Find a code E : C → {0, 1}∗ such that

E is a prefix code

◮ no codeword E(c1) is the prefix of another codeword E(c2)

The total size (in bits) required to encode a sequence S

B(S) = ∑c∈C

f (c)|E(c)|

is minimal

c© 2007 Antonio Carzaniga

Page 58: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition (2)

c© 2007 Antonio Carzaniga

Page 59: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition (2)

E : C → {0, 1}∗ defines binary strings, so we can represent Eas a binary tree T

c© 2007 Antonio Carzaniga

Page 60: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition (2)

E : C → {0, 1}∗ defines binary strings, so we can represent Eas a binary tree T

sym. freq. codea 45% 000b 13% 001c 12% 010d 16% 011e 9% 10f 5% 11

c© 2007 Antonio Carzaniga

Page 61: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition (2)

E : C → {0, 1}∗ defines binary strings, so we can represent Eas a binary tree T

sym. freq. codea 45% 000b 13% 001c 12% 010d 16% 011e 9% 10f 5% 11

10

e:9

0

f:5

10 1

100

1486

2858

b:13

1

a:45

0

d:16

1

c:12

0

◮ leaves represent symbols; internal nodes are prefixes◮ the code of a symbol c is the path from the root to c◮ the weight f (v) of a node v is the frequency of its code/prefix

c© 2007 Antonio Carzaniga

Page 62: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Problem Definition (2)

E : C → {0, 1}∗ defines binary strings, so we can represent Eas a binary tree T

sym. freq. codea 45% 000b 13% 001c 12% 010d 16% 011e 9% 10f 5% 11

10

e:9

0

f:5

10 1

100

1486

2858

b:13

1

a:45

0

d:16

1

c:12

0

◮ leaves represent symbols; internal nodes are prefixes◮ the code of a symbol c is the path from the root to c◮ the weight f (v) of a node v is the frequency of its code/prefix

B(S) = n ∑c∈leaves(T )

f (c) depth(c) = n ∑v∈T

f (v)

c© 2007 Antonio Carzaniga

Page 63: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Algorithm

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

c© 2007 Antonio Carzaniga

Page 64: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Algorithm

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

We build the code bottom-up

c© 2007 Antonio Carzaniga

Page 65: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Huffman Algorithm

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

We build the code bottom-up

Each time we make the “greedy” choice of merging the two leastfrequent nodes (symbols or prefixes)

c© 2007 Antonio Carzaniga

Page 66: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

c© 2007 Antonio Carzaniga

Page 67: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

c© 2007 Antonio Carzaniga

Page 68: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

140 1

c© 2007 Antonio Carzaniga

Page 69: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

140 1250 1

c© 2007 Antonio Carzaniga

Page 70: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

140 1250 1

300

1

c© 2007 Antonio Carzaniga

Page 71: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

140 1250 1

300

1

550

1

c© 2007 Antonio Carzaniga

Page 72: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45%b 13%c 12%d 16%e 9%f 5%

a:45 b:13 c:12 d:16 e:9 f:5

140 1250 1

300

1

550

1

100

0

1

c© 2007 Antonio Carzaniga

Page 73: Antonio Carzaniga · Designing a Greedy Algorithm 1. Cast the problem as one where we make a choice, and we are left with a subproblem 2. Prove that there is always an optimal solution

Example

HUFFMAN(C)

1 n← |C|2 Q ← C3 for i ← 1 to n− 14 do create a new node z5 left(z)← EXTRACT-MIN(Q)6 right(z)← EXTRACT-MIN(Q)7 f (z)← f (left(z)) + f (right(z))8 INSERT(Q, z)9 return EXTRACT-MIN(Q)

sym. freq. codea 45% 0b 13% 100c 12% 101d 16% 110e 9% 1110f 5% 1111

a:45 b:13 c:12 d:16 e:9 f:5

140 1250 1

300

1

550

1

100

0

1

c© 2007 Antonio Carzaniga