21. Dynamic Programming III FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, - - PowerPoint PPT Presentation

21 dynamic programming iii
SMART_READER_LITE
LIVE PREVIEW

21. Dynamic Programming III FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, - - PowerPoint PPT Presentation

21. Dynamic Programming III FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, Cormen et al, Kap. 15,35.5] 575 Approximation Let (0 , 1) given. Let I opt an optimal selection. No try to find a valid selection I with v i (1 ) v i


slide-1
SLIDE 1
  • 21. Dynamic Programming III

FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, Cormen et al, Kap. 15,35.5]

575

slide-2
SLIDE 2

Approximation

Let ε ∈ (0, 1) given. Let Iopt an optimal selection. No try to find a valid selection I with

  • i∈I

vi ≥ (1 − ε)

  • i∈Iopt

vi.

Sum of weights may not violate the weight limit.

576

slide-3
SLIDE 3

Different formulation of the algorithm

Before: weight limit w → maximal value v Reversed: value v → minimal weight w

⇒ alternative table g[i, v] provides the minimum weight with

a selection of the first i items (0 ≤ i ≤ n) that provide a value of exactly v (0 ≤ v ≤ n

i=1 vi).

577

slide-4
SLIDE 4

Computation

Initially

g[0, 0] ← 0 g[0, v] ← ∞ (Value v cannot be achieved with 0 items.).

Computation

g[i, v] ←

  • g[i − 1, v]

falls v < vi

min{g[i − 1, v], g[i − 1, v − vi] + wi}

sonst.

incrementally in i and for fixed i increasing in v. Solution can be found at largest index v with g[n, v] ≤ w.

578

slide-5
SLIDE 5

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

579

slide-6
SLIDE 6

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

579

slide-7
SLIDE 7

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

579

slide-8
SLIDE 8

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

579

slide-9
SLIDE 9

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

579

slide-10
SLIDE 10

Example

E = {(2, 3), (4, 5), (1, 1)} 1 2 3 4 5 6 7 8 9 ∅ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ (2, 3) ∞ ∞ 2 ∞ ∞ ∞ ∞ ∞ ∞ (4, 5) ∞ ∞ 2 ∞ 4 ∞ ∞ 6 ∞ (1, 1) 1 ∞ 2 3 4 5 ∞ 6 7 v i

Read out the solution: if g[i, v] = g[i − 1, v] then item i unused and continue with g[i − 1, v] otherwise used and continue with g[i − 1, b − vi] .

579

slide-11
SLIDE 11

The approximation trick

Pseduopolynomial run time gets polynmial if the number of occuring values can be bounded by a polynom of the input length. Let K > 0 be chosen appropriately. Replace values vi by “rounded values” ˜

vi = ⌊vi/K⌋ delivering a new input E′ = (wi, ˜ vi)i=1...n.

Apply the algorithm on the input E′ with the same weight limit W.

580

slide-12
SLIDE 12

Idea

Example K = 5 Values

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, . . . , 98, 99, 100 → 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, . . . , 19, 19, 20

Obviously less different values

581

slide-13
SLIDE 13

Properties of the new algorithm

Selection of items in E′ is also admissible in E. Weight remains unchanged! Run time of the algorithm is bounded by O(n2 · vmax/K) (vmax := max{vi|1 ≤ i ≤ n})

582

slide-14
SLIDE 14

How good is the approximation?

It holds that

vi − K ≤ K · vi K

  • = K · ˜

vi ≤ vi

Let I′

  • pt be an optimal solution of E′. Then

 

i∈Iopt

vi   − n · K

|Iopt|≤n

  • i∈Iopt

(vi − K) ≤

  • i∈Iopt

(K · ˜ vi) = K

  • i∈Iopt

˜ vi ≤

I′

  • ptoptimal

K

  • i∈I′
  • pt

˜ vi =

  • i∈I′
  • pt

K · ˜ vi ≤

  • i∈I′
  • pt

vi.

583

slide-15
SLIDE 15

Choice of K

Requirement:

  • i∈I′

vi ≥ (1 − ε)

  • i∈Iopt

vi.

Inequality from above:

  • i∈I′
  • pt

vi ≥  

i∈Iopt

vi   − n · K

thus: K = ε

  • i∈Iopt vi

n

.

584

slide-16
SLIDE 16

Choice of K

Choose K = ε

  • i∈Iopt vi

n

. The optimal sum is unknown. Therefore we choose K′ = εvmax

n .34

It holds that vmax ≤

i∈Iopt vi and thus K′ ≤ K and the

approximation is even slightly better. The run time of the algorithm is bounded by

O(n2 · vmax/K′) = O(n2 · vmax/(ε · vmax/n)) = O(n3/ε).

34We can assume that items i with wi > W have been removed in the first place. 585

slide-17
SLIDE 17

FPTAS

Such a family of algorithms is called an approximation scheme: the choice of ε controls both running time and approximation quality. The runtime O(n3/ε) is a polynom in n and in 1

ε. The scheme is

therefore also called a FPTAS - Fully Polynomial Time Approximation Scheme

586

slide-18
SLIDE 18
  • 22. Greedy Algorithms

Fractional Knapsack Problem, Huffman Coding [Cormen et al, Kap. 16.1, 16.3]

587

slide-19
SLIDE 19

The Fractional Knapsack Problem

set of n ∈ ◆ items {1, . . . , n} Each item i has value vi ∈ ◆ and weight wi ∈ ◆. The maximum weight is given as W ∈ ◆. Input is denoted as E = (vi, wi)i=1,...,n. Wanted: Fractions 0 ≤ qi ≤ 1 (1 ≤ i ≤ n) that maximise the sum

n

i=1 qi · vi under n i=1 qi · wi ≤ W.

588

slide-20
SLIDE 20

Greedy heuristics

Sort the items decreasingly by value per weight vi/wi. Assumption vi/wi ≥ vi+1/wi+1 Let j = max{0 ≤ k ≤ n : k

i=1 wi ≤ W}. Set

qi = 1 for all 1 ≤ i ≤ j. qj+1 = W−j

i=1 wi

wj+1

.

qi = 0 for all i > j + 1.

That is fast: Θ(n log n) for sorting and Θ(n) for the computation of the qi.

589

slide-21
SLIDE 21

Correctness

Assumption: optimal solution (ri) (1 ≤ i ≤ n). The knapsack is full:

i ri · wi = i qi · wi = W.

Consider k: smallest i with ri = qi Definition of greedy: qk > rk. Let

x = qk − rk > 0.

Construct a new solution (r′

i): r′ i = ri∀i < k. r′ k = qk. Remove

weight n

i=k+1 δi = x · wk from items k + 1 to n. This works because

n

i=k ri · wi = n i=k qi · wi.

590

slide-22
SLIDE 22

Correctness

n

  • i=k

r′

ivi = rkvk + xwk

vk wk +

n

  • i=k+1

(riwi − δi) vi wi ≥ rkvk + xwk vk wk +

n

  • i=k+1

riwi vi wi − δi vk wk = rkvk + xwk vk wk − xwk vk wk +

n

  • i=k+1

riwi vi wi =

n

  • i=k

rivi.

Thus (r′

i) is also optimal. Iterative application of this idea generates

the solution (qi).

591

slide-23
SLIDE 23

Huffman-Codes

Goal: memory-efficient saving of a sequence of characters using a binary code with code words..

592

slide-24
SLIDE 24

Huffman-Codes

Goal: memory-efficient saving of a sequence of characters using a binary code with code words.. Example File consisting of 100.000 characters from the alphabet {a, . . . , f}.

a b c d e f Frequency (Thousands) 45 13 12 16 9 5 Code word with fix length 000 001 010 011 100 101 Code word variable length 101 100 111 1101 1100

592

slide-25
SLIDE 25

Huffman-Codes

Goal: memory-efficient saving of a sequence of characters using a binary code with code words.. Example File consisting of 100.000 characters from the alphabet {a, . . . , f}.

a b c d e f Frequency (Thousands) 45 13 12 16 9 5 Code word with fix length 000 001 010 011 100 101 Code word variable length 101 100 111 1101 1100

File size (code with fix length): 300.000 bits. File size (code with variable length): 224.000 bits.

592

slide-26
SLIDE 26

Huffman-Codes

Consider prefix-codes: no code word can start with a different codeword.

593

slide-27
SLIDE 27

Huffman-Codes

Consider prefix-codes: no code word can start with a different codeword. Prefix codes can, compared with other codes, achieve the optimal data compression (without proof here).

593

slide-28
SLIDE 28

Huffman-Codes

Consider prefix-codes: no code word can start with a different codeword. Prefix codes can, compared with other codes, achieve the optimal data compression (without proof here). Encoding: concatenation of the code words without stop character (difference to morsing).

affe → 0 · 1100 · 1100 · 1101 → 0110011001101

593

slide-29
SLIDE 29

Huffman-Codes

Consider prefix-codes: no code word can start with a different codeword. Prefix codes can, compared with other codes, achieve the optimal data compression (without proof here). Encoding: concatenation of the code words without stop character (difference to morsing).

affe → 0 · 1100 · 1100 · 1101 → 0110011001101

Decoding simple because prefixcode

0110011001101 → 0 · 1100 · 1100 · 1101 → affe

593

slide-30
SLIDE 30

Code trees

100 86 58

a:45 b:13

28

c:12 d:16

14 14

e:9 f:5 1 1 1 1 1

Code words with fixed length

100

a:45

55 25

c:12 b:13

30 14

f:5 e:9 d:16 1 1 1 1 1

Code words with variable length

594

slide-31
SLIDE 31

Properties of the Code Trees

An optimal coding of a file is alway represented by a complete binary tree: every inner node has two children.

595

slide-32
SLIDE 32

Properties of the Code Trees

An optimal coding of a file is alway represented by a complete binary tree: every inner node has two children. Let C be the set of all code words, f(c) the frequency of a codeword c and dT(c) the depth of a code word in tree T. Define the cost of a tree as

B(T) =

  • c∈C

f(c) · dT(c).

(cost = number bits of the encoded file)

595

slide-33
SLIDE 33

Properties of the Code Trees

An optimal coding of a file is alway represented by a complete binary tree: every inner node has two children. Let C be the set of all code words, f(c) the frequency of a codeword c and dT(c) the depth of a code word in tree T. Define the cost of a tree as

B(T) =

  • c∈C

f(c) · dT(c).

(cost = number bits of the encoded file) In the following a code tree is called optimal when it minimizes the costs.

595

slide-34
SLIDE 34

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

slide-35
SLIDE 35

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

14

slide-36
SLIDE 36

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

14 25

slide-37
SLIDE 37

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

14 25 30

slide-38
SLIDE 38

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

14 25 30 55

slide-39
SLIDE 39

Algorithm Idea

Tree construction bottom up Start with the set C of code words Replace iteriatively the two nodes with smallest frequency by a new parent node. a:45 b:13 c:12 d:16 e:9 f:5

14 25 30 55 100

596

slide-40
SLIDE 40

Algorithm Huffman(C)

Input : code words c ∈ C Output : Root of an optimal code tree n ← |C| Q ← C for i = 1 to n − 1 do allocate a new node z z.left ← ExtractMin(Q) // extract word with minimal frequency. z.right ← ExtractMin(Q) z.freq ← z.left.freq + z.right.freq Insert(Q, z) return ExtractMin(Q)

597

slide-41
SLIDE 41

Analyse

Use a heap: build Heap in O(n). Extract-Min in O(log n) for n

  • Elements. Yields a runtime of O(n log n).

598

slide-42
SLIDE 42

The greedy approach is correct

Theorem Let x, y be two symbols with smallest frequencies in C and let T ′(C′) be an optimal code tree to the alphabet C′ = C − {x, y} + {z} with a new symbol z with f(z) = f(x) + f(y). Then the tree T(C) that is constructed from T ′(C′) by replacing the node z by an inner node with children x and y is an optimal code tree for the alphabet C.

599

slide-43
SLIDE 43

Proof

It holds that f(x) · dT(x) + f(y) · dT(y) =

(f(x) + f(y)) · (dT ′(z) + 1) = f(z) · dT ′(x) + f(x) + f(y). Thus B(T ′) = B(T) − f(x) − f(y).

Assumption: T is not optimal. Then there is an optimal tree T ′′ with

B(T ′′) < B(T). We assume that x and y are brothers in T ′′. Let T ′′′

be the tree where the inner node with children x and y is replaced by

  • z. Then it holds that

B(T ′′′) = B(T ′′) − f(x) − f(y) < B(T) − f(x) − f(y) = B(T ′).

Contradiction to the optimality of T ′. The assumption that x and y are brothers in T ′′ can be justified because a swap of elements with smallest frequency to the lowest level of the tree can at most decrease the value of B.

600