Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset.

Similar presentations


Presentation on theme: "Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset."— Presentation transcript:

1 Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset of items with total weight · W maximize cost Version 1: Items are divisible. KNAPSACK a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset of items with total weight · W maximize cost KNAPSACK Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset of items with total weight · W maximize cost KNAPSACK Version 1: Items are divisible. Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset of items with total weight · W maximize cost KNAPSACK

2 KNAPSACK – divisible: a greedy solution KNAPSACK-DIVISIBLE(n,c,w,W) 1. sort items in decreasing order of c i /w i 2. i = 1 3. currentW = 0 4. while (currentW + w i < W) { 5. take item of weight w i and cost c i 6. currentW += w i 7. i++ 8. } 9. take W-currentW portion of item i Correctness: Running time:

3 KNAPSACK – indivisible Version 2: Items are indivisible. Does previous algorithm work for this version of KNAPSACK?

4 KNAPSACK – indivisible: a dyn-prog solution The heart of the algorithm: S[k][v] =

5 KNAPSACK – indivisible: a dyn-prog solution maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] =

6 KNAPSACK – indivisible: a dyn-prog solution maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = KNAPSACK-INDIVISIBLE(n,c,w,W) 1. init S[0][v]=0 for every v=0,…,W 2. init S[k][0]=0 for every k=0,…,n 3. for k=1 to n do 4. for v=1 to W do 5. S[k][v] = S[k-1][v] 6. if (w k · v) and (S[k-1][v-w k ]+c k > S[k][v]) then 7. S[k][v] = S[k-1][v-w k ]+c k 8. RETURN S[n][W]

7 KNAPSACK – indivisible: a dyn-prog solution maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = Running time: KNAPSACK-INDIVISIBLE(n,c,w,W) 1. init S[0][v]=0 for every v=0,…,W 2. init S[k][0]=0 for every k=0,…,n 3. for v=1 to W do 4. for k=1 to n do 5. S[k][v] = S[k-1][v] 6. if (w k · v) and (S[k-1][v-w k ]+c k > S[k][v]) then 7. S[k][v] = S[k-1][v-w k ]+c k 8. RETURN S[n][W]

8 KNAPSACK – indivisible: a dyn-prog solution maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = How to output a solution ? KNAPSACK-INDIVISIBLE(n,c,w,W) 1. init S[0][v]=0 for every v=0,…,W 2. init S[k][0]=0 for every k=0,…,n 3. for v=1 to W do 4. for k=1 to n do 5. S[k][v] = S[k-1][v] 6. if (w k · v) and (S[k-1][v-w k ]+c k > S[k][v]) then 7. S[k][v] = S[k-1][v-w k ]+c k 8. RETURN S[n][W]

9 Problem: Huffman Coding Def: binary character code = assignment of binary strings to characters e.g. ASCII code A = 01000001 B = 01000010 C = 01000011 … fixed-length code How to decode: ? 01000001010000100100001101000001

10 Problem: Huffman Coding e.g. code A = 0 B = 10 C = 11 … variable-length code How to decode: ? 0101001111 Def: binary character code = assignment of binary strings to characters

11 Problem: Huffman Coding e.g. code A = 0 B = 10 C = 11 … How to decode: ? 0101001111 Def: A code is prefix-free if no codeword is a prefix of another codeword. variable-length code Def: binary character code = assignment of binary strings to characters

12 Problem: Huffman Coding Def: A code is prefix-free if no codeword is a prefix of another codeword. variable-length code Def: binary character code = assignment of binary strings to characters e.g. another code A = 1 B = 10 C = 11 … How to decode: ? 10101111

13 Problem: Huffman Coding Def: Huffman coding is an optimal prefix-free code. E11.1607% A8.4966% R7.5809% I7.5448% O7.1635% T6.9509% N6.6544% S5.7351% L5.4893% C4.5388% U3.6308% D3.3844% P3.1671% M3.0129% H3.0034% G2.4705% B2.0720% F1.8121% Y1.7779% W1.2899% K1.1016% V1.0074% X0.2902% Z0.2722% J0.1965% Q0.1962% Optimization problems - Input: - Output: - Objective:

14 an alphabet with frequencies a prefix-free code minimize expected number of bits per character Problem: Huffman Coding Def: Huffman coding is an optimal prefix-free code. E11.1607% A8.4966% R7.5809% I7.5448% O7.1635% T6.9509% N6.6544% S5.7351% L5.4893% C4.5388% U3.6308% D3.3844% P3.1671% M3.0129% H3.0034% G2.4705% B2.0720% F1.8121% Y1.7779% W1.2899% K1.1016% V1.0074% X0.2902% Z0.2722% J0.1965% Q0.1962% Huffman coding - Input: - Output: - Objective:

15 Problem: Huffman Coding A60% B20% C10% D10% an alphabet with frequencies a prefix-free code minimize expected number of bits per character Huffman coding - Input: - Output: - Objective: Example:Is fixed-width coding optimal ?

16 Problem: Huffman Coding A60% B20% C10% D10% an alphabet with frequencies a prefix-free code minimize expected number of bits per character Huffman coding - Input: - Output: - Objective: Example:Is fixed-width coding optimal ? NO, exists a prefix-free code using 1.6 bits per character !

17 Problem: Huffman Coding an alphabet with frequencies a prefix-free code minimize expected number of bits per character Huffman coding - Input: - Output: - Objective: Huffman ( [a 1,f 1 ],[a 2,f 2 ],…,[a n,f n ]) 1. if n=1 then 2. code[a 1 ]  “” 3. else 4. let f i,f j be the 2 smallest f’s 5. Huffman ( [a i,f i +f j ],[a 1,f 1 ],…,[a n,f n ] ) omits a i,a j 6. code[a j ]  code[a i ] + “0” 7. code[a i ]  code[a i ] + “1”

18 Problem: Huffman Coding Let x,y be the symbols with frequencies f x > f y. Then in an optimal prefix code length(C x )  length(C y ). Lemma 1:

19 Problem: Huffman Coding Let x,y be the symbols with frequencies f x > f y. Then in an optimal prefix code length(C x )  length(C y ). Lemma 1: If w is a longest codeword in an optimal code then there exists another codeword of the same length. Lemma 2:

20 Problem: Huffman Coding Let x,y be the symbols with frequencies f x > f y. Then in an optimal prefix code length(C x )  length(C y ). Lemma 1: Let x,y be the symbols with the smallest frequencies. Then there exists an optimal prefix code such that the codewords for x and y differ only in the last bit. Lemma 3: If w is a longest codeword in an optimal code then there exists another codeword of the same length. Lemma 2:

21 Problem: Huffman Coding Let x,y be the symbols with frequencies f x > f y. Then in an optimal prefix code length(C x )  length(C y ). Lemma 1: Let x,y be the symbols with the smallest frequencies. Then there exists an optimal prefix code such that the codewords for x and y differ only in the last bit. Lemma 3: The prefix code output by the Huffman algorithm is optimal. Theorem: If w is a longest codeword in an optimal code then there exists another codeword of the same length. Lemma 2:


Download ppt "Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset."

Similar presentations


Ads by Google