1 / 45

CSC 282 – Algorithms

CSC 282 – Algorithms. Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu www.cs.rochester.edu/~stefanko/Teaching/06CS282. Quiz – problem 1. Quiz – problem 2. 1 l  1, r  n 2 while l < r do

rfahey
Download Presentation

CSC 282 – Algorithms

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. CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu www.cs.rochester.edu/~stefanko/Teaching/06CS282

  2. Quiz – problem 1

  3. Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” r=2 m=1 B=5 4 5 l=1

  4. Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ”

  5. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ”

  6. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” A[m]<B  (B A  B A[m+1..r])

  7. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” DONE

  8. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” DONE ?

  9. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” Still need to prove that the algorithm terminates.

  10. Quiz – problem 2 B A  B A[l..r] 1 l  1, r  n 2 while l < r do 3 m   (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” Q = r-l Q’ = r-((l+r)/2 +1) Q’ = (l+r)/2 -l

  11. Quiz – problem 3 PROBLEM: rotate an array in-place this type problem given on interviews in MSFT, GOOG homework

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

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

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

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

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

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

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

  19. Finding the minimum

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  42. Why 5-tuples? 3 1 7 6 1 5 9 1 2 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 3 6 9 1 1 1 (n) 7 5 2 X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse 1 1 1 3 5 2 7 6 9 1 1 1 3 5 2 7 6 9

  43. Why 5-tuples? 3 1 7 6 1 5 9 1 2 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 3 6 9 1 1 1 (n) 7 5 2 X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse 1 1 1 3 5 2 7 6 9 T(2n/3) T(n/3) 1 1 1 3 5 2 7 6 9

  44. Why 5-tuples? T(n) = T(n/3) + T(2n/3) + (n)

  45. Why 5-tuples? T(n) = T(n/3) + T(2n/3) + (n) T(n)  c.n.ln n Induction step: T(n) = T(n/3) + T(2n/3) + (n)  c.(n/3).ln (n/3) + c.(2n/3).ln (2n/3) + (n)  c.n.ln n - c.n.((1/3)ln 3+(2/3)ln 3/2)+(n) c.n.ln n

More Related