CSC 282 – Algorithms Daniel Stefankovic – CSB 620 TA: Girts Folkmanis – CSB 614
Quiz – problem 1
Quiz – problem 2 45 l=1 r=2 m=1 B=5 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 ”
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 ”
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 ” B A B A[l..r]
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 ” B A B A[l..r] A[m]<B (B A B A[m+1..r])
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 ” B A B A[l..r] DONE
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 ” B A B A[l..r] DONE ?
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 ” B A B A[l..r] Still need to prove that the algorithm terminates.
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 ” B A B A[l..r] Q = r-l Q’ = r-( (l+r)/2 +1) Q’ = (l+r)/2 -l
Quiz – problem 3 PROBLEM: rotate an array in-place this type problem given on interviews in MSFT, GOOG homework
Recurrences T(n) T( n/2 ) + T( n/2 ) + c.n T(n)=O(n log n) T(n) 2 T( n/2 ) + c.n We “showed” : for
Recurrences 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 T(n) 2 T( n/2 ) + c.n
Recurrences T(n) 2 T( n/2 ) + c.n T(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
Recurrences useful guess – substitute (prove) or use “Master theorem” T(n) = a T(n/b) + f(n) If f(n) = O(n c- ) then T(n) = (n c ) If f(n) = (n c ) then T(n) = (n c.log n) If f(n) = (n c+ ) then T(n)= (f(n)) if a.f(n/b) d.f(n) for some d n 0 c=log b a
Recurrences T(n) = a T(n/b) + f(n) If f(n) = O(n c- ) then T(n) = (n c ) If f(n) = (n c ) then T(n) = (n c.log n) If f(n) = (n c+ ) then T(n)= (f(n)) if a.f(n/b) d.f(n) for some d n 0 c=log b a T(n) = 3 T(n/2) + (n) T(n) = 2T(n/2) + (n.log n)
Finding the minimum min A[1] for i from 2 to n do if A[i]<min then min A[i] How many comparisons?
Finding the minimum How many comparisons? comparison based algorithm: The only allowed operation is comparing the elements
Finding the minimum
Finding the k-th smallest element k = n/2 = MEDIAN
Finding the k-th smallest element
) sort each 5-tuple
Finding the k-th smallest element ) sort each 5-tuple
Finding the k-th smallest element ) sort each 5-tuple
Finding the k-th smallest element ) sort each 5-tuple
Finding the k-th smallest element ) sort each 5-tuple
Finding the k-th smallest element ) sort each 5-tuple TIME = ?
Finding the k-th smallest element ) sort each 5-tuple TIME = (n)
Finding the k-th smallest element ) find median of the middle n/5 elements TIME = ?
Finding the k-th smallest element ) find median of the middle n/5 elements TIME = T(n/5)
Finding the k-th smallest element At least ? Many elements in the array are X
Finding the k-th smallest element At least ? Many elements in the array are X
Finding the k-th smallest element At least 3n/10 elements in the array are X
Finding the k-th smallest element At least 3n/10 elements in the array are X
Finding the k-th smallest element At least 3n/10 elements in the array are X XX XX
Finding the k-th smallest element At least 3n/10 elements in the array are X XX XX Recurse, time ?
Finding the k-th smallest element At least 3n/10 elements in the array are X XX XX Recurse, time T(7n/10)
Finding the k-th smallest element XX XX recurse
Finding the k-th smallest element XX XX recurse n) T(n/5) n) T(7n/10)
Finding the k-th smallest element T(n) T(n/5) + T(7n/10) + O(n)
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
Why 5-tuples? XX XX recurse n)
Why 5-tuples? XX XX recurse n) T(2n/3) T(n/3)
Why 5-tuples? T(n) T(n/3) + T(2n/3) + (n)
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