Download presentation
Presentation is loading. Please wait.
1
T(n) = 4 T(n/3) + (n)
2
T(n) = 2 T(n/2) + (n)
3
E[X i ] = n/(n-i) X=X 0 + … + X n/2-1 E[X]= n/(n-i) = (n) i=1 n/2-1 1 n/(n-i) 2 for i n/2
5
1. compute the median m of A 2. check if m occurs in A more than n/2 times m n/2 n/2 n/2 n/2 n/2 If element occurs in A more then n/2 times then it must be a median.
7
1.compute the n/3 -rd and 2n/3 rd smallest elements x and y 2. check if x or y occurs in A more than n/3 times x y n/3
9
1. sort array A 2. for each b B, using binary search check whether X-b is in A Step 1 – O(n log n) Step 2 – n * O(log n) = O(n log n)
11
Reduces to merging k sorted lists problem (homework 2.1).
13
1. Find MIN and MAX of A 2. divide the interval [MIN,MAX] into n+1 buckets 3. for each non-empty bucket b, compute min b and max b in the bucket 4. for each pair of neighboring non-empty buckets b,c compare the interval [max b,min c ] to the longest interval so far.
14
Optimization problems Input: Output: Objective:
15
Activity selection problem Input: list of time-intervals L Output: a non-overlapping subset S of the intervals Objective: maximize |S| 3,7 2,4 5,8 6,9 1,11 10,12 0,3
16
Activity selection problem Input: list of time-intervals L Output: a non-overlapping subset S of the intervals Objective: maximize |S| 3,7 2,4 5,8 6,9 1,11 10,12 0,3 Answer = 3
17
Activity selection problem Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity a 3. remove all activities conflicting with a 4. repeat
18
Activity selection problem Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity a 3. remove all activities conflicting with a 4. repeat
19
Activity selection problem Algorithm 1: 1. sort the activities by the starting time 2. pick the first activity a 3. remove all activities conflicting with a 4. repeat
20
Activity selection problem Algorithm 2: 1. sort the activities by length 2. pick the shortest activity a 3. remove all activities conflicting with a 4. repeat
21
Activity selection problem Algorithm 2: 1. sort the activities by length 2. pick the shortest activity a 3. remove all activities conflicting with a 4. repeat
22
Activity selection problem Algorithm 2: 1. sort the activities by length 2. pick the shortest activity a 3. remove all activities conflicting with a 4. repeat
23
Activity selection problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
24
Activity selection problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
25
Activity selection problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
26
Activity selection problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity which ends first 3. remove all activities conflicting with a 4. repeat
27
Activity selection problem Algorithm 3: 1. sort the activities by ending time 2. pick the activity a which ends first 3. remove all activities conflicting with a 4. repeat Theorem: Algorithm 3 gives an optimal solution to the activity selection problem.
28
Activity selection problem Theorem: Algorithm 3 gives an optimal solution to the activity selection problem. Proof idea: Any solution can be “morphed” to the solution given by the Algorithm 3.
29
Activity selection problem Proof idea: Any solution can be “morphed” to the solution given by the Algorithm 3. Proof: W.l.o.g., all solutions below are sorted by the ending time. Let A=[a_1,b_1],…,[a_k,b_k] be the solution output by the Algorithm 3. Let O=[c_1,d_1],…,[c_l,d_l] be an optimal solution, which maximizes the length of the prefix common with A. A O
30
Activity selection problem Proof: W.l.o.g., all solutions below are sorted by the ending time. Let A=[a_1,b_1],…,[a_k,b_k] be the solution output by the Algorithm 3. Let O=[c_1,d_1],…,[c_l,d_l] be an optimal solution, which maximizes the length of the prefix common with A. A O Let m-1 be the length of the common prefix of A and O. Assume m k. Then [a m,b m ] does not intersect [c i,d i ] for i<m (since it does not intersect [a i,b i ] for i<m). Moreover [a m,b m ] does not intersect [c i,d i ] for i>m (since b m d m and d m <c i for i>m). Hence we can replace [c m,d m ] by [a m,.b m ], and obtain another optimal solution which contradicts our choice of O. Case m>k cannot happen, since Algorithm 3 would output more activities
31
Coin change problem Input: a number P (price) Output: a way of paying P using bills, coins Objective: minimize the number of bills, coins used 8.37 = 5 + 1 + 1 + 1 + 0.25 + 0.10 + 0.01 + 0.01 8 bills/coins used
32
Coin change problem For simplicity assume that the coin values are: 10,5,1 Pay(N) if N 10 then output(10), Pay(N-10) elif N 5 then output(5), Pay(N-5) elif N 1 then output(1), Pay(N-1)
33
Coin change problem Pay(N) if N 10 then output(10), Pay(N-10) elif N 5 then output(5), Pay(N-5) elif N 1 then output(1), Pay(N-1) coin values: 10,5,1 Theorem: For coin values 10,5,1 the Pay algorithm uses the minimal number of coins.
34
Coin change problem Theorem: For coin values 10,5,1 the Pay algorithm uses the minimal number of coins. coin values: 10,5,1 Proof: Let N be the amount to be paid. Let the optimal solution be P=A*10 + B*5 + C. Clearly B 1 (otherwise we can decrease B by 2 and increase A by 1, improving the solution). Similarly C 4. Let the solution given by Pay be P=a*10 + b*5 + c. Clearly b 1 (otherwise the algorithm would output 10 instead of 5). Similarly c 4.
35
Coin change problem coin values: 10,5,1 Proof: Let N be the amount to be paid. Let the optimal solution be P=A*10 + B*5 + C. Clearly B 1 (otherwise we can decrease B by 2 and increase A by 1, improving the solution). Similarly C 4. Let the solution given by Pay be P=a*10 + b*5 + c. Clearly b 1 (otherwise the algorithm would output 10 instead of 5). Similarly c 4. From 0 C 4 and P=(2A+B)*5+C we have C=P mod 5. Similarly c=P mod 5, and hence c=C. Let Q=(P-C)/5. From 0 B 1 and Q = 2A + B we have B=Q mod 2. Similarly b=Q mod 2, and hence b=B. Thus a=A, b=B, c=C, i.e., the solution given by Pay is optimal.
36
Coin change problem For simplicity assume that the coin values are: 4,3,1 Pay(N) if N 4 then output(4), Pay(N-4) elif N 3 then output(3), Pay(N-3) elif N 1 then output(1), Pay(N-1)
37
Coin change problem 6 = 4 + 1 + 1 6 = 3 + 3 coin values: 4,3,1 Theorem: For coin values 4,3,1 the Pay algorithm uses the minimal number of coins.
38
Huffman codes binary character code = assignment of binary strings to characters e.g. ASCII code A = 01000001 B = 01000010 C = 01000011 …. fixed-length code 01000001010000100100001101000001
39
Huffman codes binary character code = assignment of binary strings to characters e.g. ASCII code A = 01000001 B = 01000010 C = 01000011 …. fixed-length code 01000001 01000010 01000011 01000001
40
Huffman codes binary character code = assignment of binary strings to characters E.g. A = 0 B = 01 C = 11 …. variable-length code 011111111110…
41
Huffman codes binary character code = assignment of binary strings to characters DEFINITION: a binary character code is prefix-free, if no code-word is a prefix of another code-word A = 01 B = 101 C = 1011 …. NOT prefix-free (B is a prefix of C)
42
Optimal prefix-code problem Input: alphabet, with frequencies Output: prefix code Objective: expected number of bits per character 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%
43
Optimal prefix-code problem A 60% B 20% C 10% D 10%
44
Optimal prefix-code problem A 60% - 00 B 20% - 01 C 10% - 10 D 10% - 11 2bits per character
45
Optimal prefix-code problem A 60% - 0 B 20% - 10 C 10% - 110 D 10% - 111 1.6 bits per character
46
Optimal prefix-code problem Huffman ( [a 1,f 1 ],[a 2,f 2 ],…,[a n,f n ]) if n=1 then code[a 1 ] else let f i,f j be the 2 smallest f’s Huffman ( [a i,f i +f j ],[a 1,f 1 ],…,[a n,f n ] ) code[a j ] code[a i ] 0 code[a i ] code[a i ] 1
47
Optimal prefix-code problem Lemma 1: Let x,y be the symbols with frequencies f x < f y. Then in an optimal prefix code length(C x ) length(C y ).
48
Optimal prefix-code problem Lemma 2: Let C = R 0 be a longest codeword in an optimal code. Then R 1 is also a codeword. Lemma 1: Let x,y be the symbols with frequencies f x < f y. Then in an optimal prefix code length(C x ) length(C y ).
49
Optimal prefix-code problem Lemma 3: 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 1: 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 2: Let C = R 0 be a longest codeword in an optimal code. Then R 1 is also a codeword.
50
Optimal prefix-code problem Lemma 3: 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. Theorem: the prefix code output by the Huffman algorithm is optimal.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.