1 / 54

Administrative

Administrative. Sep. 20 (today) – HW1 due Sep. 21 8am – problem session Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct. 2 Oct. 4 – QUIZ #2. (pages 45-79 of DPV). Merge 2 sorted lists. M ERGE INSTANCE: 2 lists x i , y i such that

cvidrio
Download Presentation

Administrative

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. Administrative Sep. 20 (today) – HW1 due Sep. 21 8am – problem session Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct. 2 Oct. 4 – QUIZ #2 (pages 45-79 of DPV)

  2. Merge 2 sorted lists MERGE INSTANCE: 2 lists xi, yi such that x1  x2  …  xn y1  y2  …  ym SOLUTION: ordered merge

  3. Merge 2 sorted lists 1 i  1, j  1 2 while i  n and j  n do 3 if xi  yj then 4 output xi, i  i + 15 else 6 output yj, j  j + 1 7 output remaining elements

  4. Mergesort MERGE-SORT(a,l,r) if l < r then m  (l+r)/2  MERGE-SORT(a,I,m) MERGE-SORT(a,m+1,r) MERGE(a,l,m,r)

  5. Mergesort Running time?

  6. Mergesort Running time? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth ?

  7. Mergesort Running time? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth = log n

  8. Mergesort Time spent on merge? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth = log n

  9. Mergesort [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Time spent on merge? O(n) O(n) O(n) Depth = log n O(n.logn)

  10. Mergesort recurrence T(n)= T(n/2) + (n) if n>1 T(1)= (1)

  11. Recurrences T(n)  T( n/2 ) + T( n/2 ) + c.n We “showed” : T(n)=O(n log n) for T(n)  2 T( n/2 ) + c.n

  12. Recurrences T(n)  2 T( n/2 ) + c.n T(1) = O(1) Proposition: T(n)  d.n.lg n Proof: T(n) 2 T( n/2 ) + c.n  2 d (n/2).lg (n/2) + c.n = d.n.( lg n – 1 ) + cn = d.n.lg n + (c-d).n  d.n.lg n

  13. Recurrences T(n)  T( n/2 ) + T( n/2 ) + c.n T(n)  2 T( n/2 ) + c.n G(n) = T(n+2) G(n) = T(n+2)  T(n/2+1) + T(n/2+1) + c.n = G(n/2-1) + G(n/2-1) + c.n

  14. Recurrences • useful • guess – substitute (prove) • or use “Master theorem” • T(n) = a T(n/b) + f(n) • If f(n) = O(nc-) then T(n) =(nc) • If f(n) = (nc) then T(n) =(nc.log n) • If f(n) = (nc+) then T(n)=(f(n)) • if a.f(n/b)  d.f(n) for some d<1 and n>n0 c=logb a

  15. Karatsuba-Offman a=2n/2 a1 + a0 b=2n/2 b1 + b0 ab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0

  16. Karatsuba-Offman a=2n/2 a1 + a0 b=2n/2 b1 + b0 Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4

  17. Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4 Recurrence?

  18. Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4 Recurrence? T(n) = 4T(n/2) + O(n)

  19. Karatsuba-Offman T(n) = 4T(n/2) + O(n) T(n)=O(n2)

  20. Karatsuba-Offman ab=(2n/2a1+a0)(2n/2b1+b0) = 2na1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0 Can compute in less than 4 multiplications?

  21. Karatsuba-Offman ab=(2n/2a1+a0)(2n/2b1+b0) = 2na1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0 Can compute using 3 multiplications: (a0+a1)(b0+b1) = a0b0 + (a1 b0 + a0 b1) + a1 b1

  22. Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b0,n/2) R3 Multiply(a1+a0,b1+b0,n/2+1) R4 R3 – R2 – R1 return 2n R1+ 2n/2 R3 + R2 Recurrence?

  23. Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b0,n/2) R3 Multiply(a1+a0,b1+b0,n/2+1) R4 R3 – R2 – R1 return 2n R1+ 2n/2 R3 + R2 Recurrence? T(n) = 3T(n/2) + O(n)

  24. Recurrences • T(n) = a T(n/b) + f(n) • If f(n) = O(nc-) then T(n) =(nc) • If f(n) = (nc) then T(n) =(nc.log n) • If f(n) = (nc+) then T(n)=(f(n)) • if a.f(n/b)  d.f(n) for some d<1 and n>n0 c=logb a T(n) = 3 T(n/2) + (n) T(n) = 2T(n/2) + (n.log n)

  25. Karatsuba-Offman T(n) = 3T(n/2) + O(n) T(n)=O(nC) C=log2 3  1.58

  26. Finding the minimum min  A[1] for i from 2 to n do if A[i]<min then min  A[i] How many comparisons?

  27. Finding the minimum How many comparisons? comparison based algorithm: The only allowed operation is comparing the elements

  28. Finding the minimum

  29. Finding the k-th smallest element k = n/2 = MEDIAN

  30. 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 Finding the k-th smallest element

  31. Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 1) sort each 5-tuple

  32. Finding the k-th smallest element 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 1) sort each 5-tuple

  33. Finding the k-th smallest element 1 2 8 3 6 9 6 1 1 7 5 2 8 8 3 1) sort each 5-tuple

  34. Finding the k-th smallest element 1 2 8 3 6 9 6 1 1 7 5 2 8 8 3 1) sort each 5-tuple

  35. Finding the k-th smallest element 1 1 8 3 2 9 6 5 1 7 6 2 8 8 3 1) sort each 5-tuple

  36. Finding the k-th smallest element TIME = ? 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9 1) sort each 5-tuple

  37. 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9 Finding the k-th smallest element TIME = (n) 1) sort each 5-tuple

  38. Finding the k-th smallest element 2) find median of the middle n/5 elements TIME = ? 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9

  39. Finding the k-th smallest element 2) find median of the middle n/5 elements TIME = T(n/5) 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9

  40. Finding the k-th smallest element At least ? Many elements in the array are  X 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9

  41. Finding the k-th smallest element At least ? Many elements in the array are  X 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8

  42. Finding the k-th smallest element At least 3n/10 elements in the array are  X 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8

  43. Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8 At least 3n/10 elements in the array are  X

  44. Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 At least 3n/10 elements in the array are  X

  45. Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 Recurse, time ? At least 3n/10 elements in the array are  X

  46. Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 Recurse, time  T(7n/10) At least 3n/10 elements in the array are  X

  47. 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 1 6 1 1 2 8 1 1 3 3 3 6 2 2 2 2 9 6 1 6 1 5 5 3 3 1 7 7 7 5 6 6 7 7 2 8 8 8 8 8 8 9 9 3 Finding the k-th smallest element 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 X >X 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse

  48. 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 6 1 1 1 2 8 1 1 3 3 3 2 2 6 2 2 9 1 6 6 5 5 1 3 1 3 7 7 7 6 5 6 7 2 7 8 8 8 8 8 8 9 3 9 Finding the k-th smallest element 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 (n) X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse T(7n/10) T(n/5)

  49. Finding the k-th smallest element T(n)  T(n/5) + T(7n/10) + O(n)

  50. Finding the k-th smallest element T(n)  T(n/5) + T(7n/10) + O(n) T(n)  d.n Induction step: T(n)  T(n/5) + T(7n/10) + O(n)  d.(n/5) + d.(7n/10) + O(n)  d.n + (O(n) – dn/10)  d.n

More Related