1 / 50

T(n) = 4 T(n/3) +  (n)

T(n) = 4 T(n/3) +  (n). T(n) = 2 T(n/2) +  (n). E[X i ] = n/(n-i) X=X 0 + … + X n/2-1 E[X]=  n/(n-i) =  (n). 1  n/(n-i)  2. for i  n/2. n/2-1. i=1. compute the median m of A check if m occurs in A more than n/2 times.  n/2 . m.  n/2  -1 n/2.  n/2   n/2.

motley
Download Presentation

T(n) = 4 T(n/3) +  (n)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. T(n) = 4 T(n/3) + (n)

  2. T(n) = 2 T(n/2) + (n)

  3. E[Xi] = n/(n-i) X=X0 + … + Xn/2-1 E[X]= n/(n-i) = (n) 1 n/(n-i)2 for in/2 n/2-1 i=1

  4. compute the median m of A • check if m occurs in A more than n/2 times n/2 m n/2 -1 n/2 n/2  n/2 If element occurs in A more then n/2 times then it must be a median.

  5. 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 y x n/3 n/3 n/3

  6. sort array A • 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)

  7. Reduces to merging k sorted lists problem (homework 2.1).

  8. 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 minb and maxb in the bucket 4. for each pair of neighboring non-empty buckets b,c compare the interval [maxb,minc] to the longest interval so far.

  9. Optimization problems Input: Output: Objective:

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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.

  23. 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.

  24. 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

  25. 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 [am,bm] does not intersect [ci,di] for i<m (since it does not intersect [ai,bi] for i<m). Moreover [am,bm] does not intersect [ci,di] for i>m (since bm dm and dm<ci for i>m). Hence we can replace [cm,dm] by [am,.bm], and obtain another optimal solution which contradicts our choice of O. Case m>k cannot happen, since Algorithm 3 would output more activities

  26. 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

  27. 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)

  28. Coin change problem coin values: 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) Theorem: For coin values 10,5,1 the Pay algorithm uses the minimal number of coins.

  29. Coin change problem coin values: 10,5,1 Theorem: For coin values 10,5,1 the Pay algorithm uses the minimal number of coins. 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.

  30. 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.

  31. 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)

  32. Coin change problem coin values: 4,3,1 6 = 4 + 1 + 1 6 = 3 + 3 Theorem: For coin values 4,3,1 the Pay algorithm uses the minimal number of coins.

  33. 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

  34. 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

  35. Huffman codes binary character code = assignment of binary strings to characters E.g. A = 0 B = 01 C = 11 …. variable-length code 011111111110…

  36. 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)

  37. Optimal prefix-code problem Input:alphabet, with frequencies Output:prefix code Objective:expected number of bits per character

  38. Optimal prefix-code problem A 60% B 20% C 10% D 10%

  39. Optimal prefix-code problem A 60% - 00 B 20% - 01 C 10% - 10 D 10% - 11 2bits per character

  40. Optimal prefix-code problem A 60% - 0 B 20% - 10 C 10% - 110 D 10% - 111 1.6 bits per character

  41. Optimal prefix-code problem Huffman ( [a1,f1],[a2,f2],…,[an,fn]) if n=1 then code[a1]  else let fi,fj be the 2 smallest f’s Huffman ( [ai,fi+fj],[a1,f1],…,[an,fn] ) code[aj]  code[ai] 0 code[ai]  code[ai] 1

  42. Optimal prefix-code problem Lemma 1: Let x,y be the symbols with frequencies fx < fy. Then in an optimal prefix code length(Cx)  length(Cy).

  43. 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 fx < fy. Then in an optimal prefix code length(Cx)  length(Cy).

  44. 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 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 fx < fy. Then in an optimal prefix code length(Cx)  length(Cy).

  45. Optimal prefix-code problem Theorem: the prefix code output by the Huffman algorithm is optimal. 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.

More Related