Greedy Algorithms Chapter 16 1 Optimization Problems A class of - - PowerPoint PPT Presentation

β–Ά
greedy algorithms
SMART_READER_LITE
LIVE PREVIEW

Greedy Algorithms Chapter 16 1 Optimization Problems A class of - - PowerPoint PPT Presentation

Greedy Algorithms Chapter 16 1 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


slide-1
SLIDE 1

Greedy Algorithms

Chapter 16

1

slide-2
SLIDE 2

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

  • ptimize (i.e., maximize or minimize) some
  • bjective function

2

slide-3
SLIDE 3

Example: Bin Packing

10

2 3 4 5 6

3

slide-4
SLIDE 4

Example: Bin Packing

8

2 3 4 5 6

4

slide-5
SLIDE 5

Example: Bin Packing

4

2 3 4 5 6

5

slide-6
SLIDE 6

Example: Bin Packing

1

2 3 4 5 6

6

slide-7
SLIDE 7

Example: Bin Packing

1

2 3 4 5 6

5

7

slide-8
SLIDE 8

Example: Bin Packing

1

2 3 4 5 6

5 4

Number of bins = 3

8

slide-9
SLIDE 9

Example: Bin Packing

2 3 4 5 6

(Optimal Solution) Number of bins = 2

9

slide-10
SLIDE 10

The Greedy Method

Applied to optimization problems Adds items to the solution one-by-one Builds up towards the final solution No backtracking Not necessarily optimal!

10

slide-11
SLIDE 11

Activity Selection

a.k.a. Task Scheduling

11

slide-12
SLIDE 12

Activity Selection Problem

Given a set of activities 𝑇 = {𝑏1, 𝑏2, … , π‘π‘œ} where each activity 𝑗 has a start time 𝑑𝑗 and a finish time 𝑔

𝑗, where 0 ≀ 𝑑𝑗 < 𝑔 𝑗 < ∞. An

activity 𝑏𝑗 happens in the half-open time interval [𝑑𝑗, 𝑔

𝑗). Two activities are said to be

compatible if they do not overlap. The problem is to find a maximum-size compatible subset, i.e., a one with the maximum number of activities.

12

slide-13
SLIDE 13

Example of Activity Selection

13

slide-14
SLIDE 14

A Solution

14

slide-15
SLIDE 15

A Better Solution

15

slide-16
SLIDE 16

An Optimal (Best) Solution

16

slide-17
SLIDE 17

Another Optimal Solution

17

slide-18
SLIDE 18

β€œGreedy” Strategies

Longest first Shortest first Early start first Early finish first …

18

slide-19
SLIDE 19

Early Finish Greedy Strategy

  • 1. Sort activities by finish time
  • 2. Schedule the first activity
  • 3. Remove all incompatible activities
  • 4. If there are more activities, repeat 2

19

slide-20
SLIDE 20

Early Finish

20

slide-21
SLIDE 21

Early Finish

21

slide-22
SLIDE 22

Early Finish

22

slide-23
SLIDE 23

Early Finish

23

slide-24
SLIDE 24

Early Finish

24

slide-25
SLIDE 25

Early Finish

25

slide-26
SLIDE 26

Early Finish

26

slide-27
SLIDE 27

Early Finish

27

slide-28
SLIDE 28

Optimality of the Greedy Choice

To prove optimality of the greedy choice, we have to prove the following two properties

  • 1. Greedy Choice: The greedy choice is part
  • f the answer
  • 2. Optimal Substructure: The optimal solution

to the big problem contains the optimal solution to the sub-problem

28

slide-29
SLIDE 29

Greedy Choice

Let 𝐡 βŠ† 𝑇 be an optimal solution. Let π‘π‘˜ be the first element in 𝐡 and 𝑏𝑛 be the first element in 𝑇. We want to prove that 𝑏𝑛 is part of an

  • ptimal solution.

If 𝑏𝑛 = π‘π‘˜ then we are done Otherwise, we prove that there is another

  • ptimal solution 𝐡` = 𝐡 βˆ’ π‘π‘˜ βˆͺ 𝑏𝑛

Is 𝐡` a solution? yes Is 𝐡` optimal? yes

29

slide-30
SLIDE 30

Optimal Substructure

We want to prove that, if 𝐡 βŠ† 𝑇 is an optimal solution to 𝑇, then 𝐡 βˆ’ 𝑏𝑛 is an optimal solution to S` = 𝑏𝑗: 𝑏𝑗 ∈ 𝑇 ∧ 𝑑𝑗 > 𝑔

𝑛

Proof by contradiction Assume that 𝐡 βˆ’ 𝑏𝑛 is not optimal Then, there is another solution 𝐢 to 𝑇` such that 𝐢 > 𝐡 βˆ’ 𝑏𝑛 β‡’ |𝐢| β‰₯ |𝐡| If this is the case, then the subset 𝐡` = 𝑏𝑛 βˆͺ 𝐢 is also a solution to 𝑇 𝐡` > 𝐢 β‡’ |𝐡`| > 𝐡 , which means that 𝐡 is not optimal which is a contradiction

30

slide-31
SLIDE 31

Knapsack Problem

31

slide-32
SLIDE 32

0-1 Knapsack Problem

$4,500 15LBs $1,500 3LBs $800 2LBs $3,000 10LBs $4,000 20LBs



45LBs

32

slide-33
SLIDE 33

0-1 Knapsack Problem

$4,500 15LBs $1,500 3LBs $800 2LBs $3,000 10LBs $4,000 20LBs



45LBs

33

slide-34
SLIDE 34

0-1 Knapsack Problem

$4,500 15LBs $1,500 3LBs $800 2LBs $3,000 10LBs $4,000 20LBs



45LBs

$300/LB $500/LB $400/LB $200/LB $300/LB

34

slide-35
SLIDE 35

0-1 Knapsack Problem

$4,500 15LBs $1,500 3LBs $800 2LBs $3,000 10LBs $4,000 20LBs



45LBs

$300/LB $500/LB $400/LB $200/LB $300/LB

35

Total value = $9,800

slide-36
SLIDE 36

0-1 Knapsack Problem

$4,500 15LBs $1,500 3LBs $800 2LBs $3,000 10LBs $4,000 20LBs



45LBs

$300/LB $500/LB $400/LB $200/LB $300/LB

36

Total value = $11,500

There isn’t a known solution to the 0-1 Knapsack problem using a greedy algorithm

slide-37
SLIDE 37

Fractional Knapsack Problem

$4,500 15 oz $1,500 3 oz $800 2 oz $3,000 10oz $4,000 20oz



45 oz

$300/oz $500/oz $400/oz $200/oz $300/oz

37

slide-38
SLIDE 38

Problem Formulation

Given a set 𝑇[1. . π‘œ] of items where each item 𝑗 has a weight π‘₯𝑗 and a value 𝑀𝑗, we would like to find the amount 𝑦𝑗 of each item that we can take to maximize the total value π‘Š = ෍

𝑗=1 π‘œ

𝑀𝑗 𝑦𝑗 π‘₯𝑗 Under the following two constraints

0 ≀ 𝑦𝑗 ≀ π‘₯𝑗 σ𝑗=1

π‘œ

𝑦𝑗 ≀ 𝑋

38

slide-39
SLIDE 39

Pseudo-code

Fractional-Knapsack

Compute the value-per-weight 𝑧𝑗 =

𝑀𝑗 π‘₯𝑗 for all items

Sort items by 𝑧𝑗 Set 𝑀 = 0 While (𝑀 < 𝑋)

Select the item π‘˜ with the highest π‘§π‘˜ Set π‘¦π‘˜ = π‘π‘—π‘œ 𝑋 βˆ’ 𝑀, π‘₯

π‘˜

Remove item π‘˜ Set 𝑀 = 𝑀 + π‘¦π‘˜

Set all remaining 𝑦𝑗’s to 0

39

slide-40
SLIDE 40

Greedy-choice Property

Given the set 𝑇 ordered by the value-per- weight (𝑧), taking as much as possible π‘¦π‘˜ from the item π‘˜ with the highest value-per- weight will lead to an optimal solution Assume there is an optimal solution π‘Œβ€² where we take less amount of item π‘˜, say π‘¦π‘˜β€² < π‘¦π‘˜. We prove that there is another solution π‘Œβ€²β€² where we take π‘¦π‘˜ of item π‘˜ and get a similar

  • r a higher total value π‘Š

40

slide-41
SLIDE 41

Greedy-choice Property

Since π‘¦π‘˜β€² < π‘¦π‘˜, there must be another item 𝑙 which was taken in an amount that accounts for the difference, i.e., 𝑦′𝑙 We create another solution π‘Œβ€²β€² by doing the following changes in π‘Œβ€² Reduce the amount of item 𝑙 by a value 𝑨 𝑦𝑙

β€²β€² = 𝑦𝑙 β€² βˆ’ 𝑨

Increase the amount of item π‘˜ by a value 𝑨 π‘¦π‘˜

β€²β€² = π‘¦π‘˜ β€² + 𝑨 = π‘¦π‘˜ (π‘¦π‘˜ is the greedy choice)

41

slide-42
SLIDE 42

Greedy Choice Property

Οƒ 𝑦𝑗

β€² = Οƒ 𝑦𝑗 β€²β€² β‡’ (both are valid solutions)

π‘Šβ€² = Οƒ 𝑦𝑗

′𝑧𝑗, π‘Šβ€²β€² = Οƒ 𝑦𝑗 ′′𝑧𝑗

π‘Šβ€²β€² βˆ’ π‘Šβ€² = π‘§π‘˜π‘¦π‘˜

β€²β€² + 𝑧𝑙𝑦𝑙 β€²β€² βˆ’ π‘§π‘˜π‘¦π‘˜ β€² + 𝑧𝑙𝑦𝑙 β€²

(All other items are the same) π‘Šβ€²β€² βˆ’ π‘Šβ€² = π‘§π‘˜ π‘¦π‘˜

β€²β€² βˆ’ π‘¦π‘˜ β€² βˆ’ 𝑧𝑙 𝑦𝑙 β€² βˆ’ 𝑦𝑙 β€²β€²

But, xj

β€²β€² βˆ’ π‘¦π‘˜ β€² = 𝑦𝑙 β€² βˆ’ 𝑦𝑙 β€²β€² = 𝑨

π‘Šβ€²β€² βˆ’ π‘Šβ€² = π‘§π‘˜ βˆ’ 𝑧𝑙 𝑨 Since π‘§π‘˜ β‰₯ 𝑧𝑙 and 𝑨 = π‘¦π‘˜

β€²β€² βˆ’ π‘¦π‘˜ > 0

π‘Šβ€²β€² β‰₯ π‘Šβ€², hence, π‘Œβ€²β€² is also optimal

42

slide-43
SLIDE 43

Greedy Choice Illustration

𝑇 π‘˜ Ordered by 𝑧 π‘Œβ€² π‘˜ π‘₯

π‘˜

π‘¦π‘˜

β€²

𝑦𝑙

β€²

π‘Œβ€²β€² π‘˜ π‘¦π‘˜

β€²β€² = π‘¦π‘˜

π‘˜ 𝑦𝑙

β€²β€²

Input Optimal answer that does not have the greedy choice Optimal answer that has the greedy choice

43

slide-44
SLIDE 44

Optimal Sub-structure

Given the problem 𝑇 with an optimal solution π‘Œ, we want to prove that the solution π‘Œ` = π‘Œ βˆ’ π‘¦π‘˜ is optimal to the problem 𝑇` after removing the item π‘˜ and updating the capacity 𝑋` = 𝑋 βˆ’ π‘¦π‘˜ Proof by contradiction Assume that π‘Œ` is not optimal to 𝑇` There is another solution π‘Œ`` to 𝑇` that has a higher total value π‘Š`` > π‘Š` This means we can have a better solution to the problem 𝑇 which is impossible because π‘Œ is

  • ptimal

44

slide-45
SLIDE 45

Optimal substructure

𝑇 π‘˜ π‘₯

π‘˜

Input Ordered by 𝑧 π‘Œ π‘˜ Optimal answer for 𝑇 𝑇′ Reduced problem π‘Œβ€² Should be an

  • ptimal answer for

𝑇′

45

slide-46
SLIDE 46

Optimal Substructure

Input problem 𝑇 with an optimal answer π‘Œ of total value π‘Š π‘Œ` = π‘Œ βˆ’ π‘¦π‘˜ , π‘Š` = π‘Š βˆ’ π‘¦π‘˜π‘§π‘˜ Assume π‘Œ`` is an optimal solution to 𝑇` 𝑋`` = σ𝑦``𝑗 ≀ 𝑋 βˆ’ π‘¦π‘˜ and π‘Š`` > π‘Š` π‘Œ`` + π‘¦π‘˜ is a valid solution to 𝑇 because 𝑋`` + π‘¦π‘˜ ≀ 𝑋 π‘Š`` = π‘Š`` + π‘¦π‘˜π‘§π‘˜ > π‘Šβ€² + π‘¦π‘˜π‘§π‘˜ > V This is a contradiction with the assumption that π‘Œ is an optimal solution for 𝑇

46

slide-47
SLIDE 47

Huffman Codes

47

slide-48
SLIDE 48

Encoding

How data is represented? Fixed-size codes, e.g., ASCII

A: 1000001 B: 1000010

Variable-size codes, e.g., Morse Codes

A: ●▬ B: ▬●●● E: ● T: β–¬

48

slide-49
SLIDE 49

Example: Morse Code

49

slide-50
SLIDE 50

Prefix Codes

No code is allowed to be a prefix of another code To encode, simply concatenate all the codes Decoding does not entail any ambiguity Example:

Message β€˜JAVA’ a = β€œ0”, j = β€œ11”, v = β€œ10” Encoded message β€œ110100” Decoding β€œ110100”

51

slide-51
SLIDE 51

Trie

We can use a trie to find prefix codes the characters are stored at the external nodes a left child (edge) means 0 a right child (edge) means 1

A = 010 B = 11 C = 00 D = 10 R = 011 D B C R 1 1 1 1 A

52

slide-52
SLIDE 52

Example of Decoding

encoded text: 01011011010000101001011011010 text: ABRACADABRA

ASCII: 88 bits Our encoding: 29 bits

A = 010 B = 11 C = 00 D = 10 R = 011 D B C R 1 1 1 1 A

53

slide-53
SLIDE 53

Another Encoding

Message: β€˜ABRACADABRA’ Encoded message: β€˜001011000100001100101100’ Length: 24 bits

B R A D 1 1 1 1 C

54

slide-54
SLIDE 54

Optimal Encoding Problem

Given a set 𝐷 of π‘œ characters, for each character c ∈ 𝐷. Let c. π‘”π‘ π‘“π‘Ÿ be the frequency

  • f 𝑑 in the file. We would like to find a prefix

encoding for each 𝑑 ∈ 𝐷 with a length π‘’π‘ˆ 𝑑 such that we minimize the total cost 𝐢 = ෍

π‘‘βˆˆπ·

𝑑. π‘”π‘ π‘“π‘Ÿ Γ— π‘’π‘ˆ 𝑑 Solution: Huffman Codes

55

slide-55
SLIDE 55

Example

A,5 B,2 C,1 D,1 R,2

β€œABRACADABRA”

56

slide-56
SLIDE 56

Example

A,5 B,2 C,1 D,1 R,2 2

β€œABRACADABRA”

57

slide-57
SLIDE 57

Example

A,5 B,2 C,1 D,1 R,2 2 4

β€œABRACADABRA”

58

slide-58
SLIDE 58

Example

A,5 B,2 C,1 D,1 R,2 2 4 6

β€œABRACADABRA”

59

slide-59
SLIDE 59

Example

A,5 B,2

β€œABRACADABRA”

C,1 D,1 R,2 2 4 6 11

60

slide-60
SLIDE 60

Example

A,5 B,2

β€œABRACADABRA”

C,1 D,1 R,2 2 4 6 11

1 1 1 1

61

slide-61
SLIDE 61

Example

A,5 B,2

β€œABRACADABRA”

C,1 D,1 R,2 2 4 6 11

1 1 1 1

A=0 B=10 C=1100 D=1101 R=111

62

slide-62
SLIDE 62

Encoding

A,5 B,2 C,1 D,1 R,2 2 4 6 11

1 1 1 1

A=0 B=10 C=1100 D=1101 R=111

β€œABRACADABRA” 0 10 111 0 1100 0 1101 0 10 111 0 Length= 23 Optimal!

63

slide-63
SLIDE 63

Encoding

A,5 R,2 C,1 D,1 B,2 2 4 6 11

1 1 1 1

A=0 R=10 C=1100 D=1101 B=111

β€œABRACADABRA” 0 111 10 0 1100 0 1101 0 111 10 0 Length= 23 Optimal!

64

slide-64
SLIDE 64

Construction of Huffman Tree

Huffman(C)

n=|C| Q=C for i = 1 to n-1

allocate a new node z z.left = x = Extract-Min(Q) z.right = y = Extract-Min(Q) z.freq = x.freq + y.freq Insert(Q, z)

return Extract-Min(Q) // Root of the tree

Θ log π‘œ Θ log π‘œ n Θ π‘œ log π‘œ

Note: Can also be done in linear time

π‘ˆ π‘œ = Θ π‘œ log π‘œ π‘ˆ π‘œ = Θ π‘œ log π‘œ + 𝑁

65

slide-65
SLIDE 65

Optimality of Huffman Codes

Greedy-choice

The greedy choice yields an optimal solution.

Optimal sub-structure

The optimal solution for the bigger problem contains the optimal solution of the sub-problem.

Detailed proof in the textbook

66

slide-66
SLIDE 66

Greedy Choice

Greedy choice: Choose the characters 𝑦 and 𝑧 with the highest frequencies and merge them under one internal node We need to prove that the optimal tree has 𝑦 and 𝑧 as siblings under one common node Assume there is an optimal tree π‘ˆ where 𝑦 and 𝑧 are not siblings In the same tree, the two siblings at the deepest level are other characters 𝑏 and 𝑐 Assume 𝑦. π‘”π‘ π‘“π‘Ÿ ≀ 𝑧. π‘”π‘ π‘“π‘Ÿ and 𝑏. π‘”π‘ π‘“π‘Ÿ ≀ 𝑐. π‘”π‘ π‘“π‘Ÿ

67

slide-67
SLIDE 67

Greedy Choice

𝑧 𝑦

𝑏 𝑐 π‘ˆ

𝑧 𝑏

𝑦 𝑐 π‘ˆ`

𝑐 𝑏

𝑦 𝑧 π‘ˆ``

68