Download presentation
Presentation is loading. Please wait.
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 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 ”
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 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]
6
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])
7
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
8
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 ?
9
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.
10
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
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 T(n)=O(n log n) T(n) 2 T( n/2 ) + c.n We “showed” : for
13
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
14
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
15
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
16
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)
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
Finding the k-th smallest element 6 3 1 8 7 2 6 1 8 5 8 9 1 3 2 631 8 7 26 1 8589 1 32
22
6 3 1 8 7 2 6 1 8 5 8 9 1 3 2 1) sort each 5-tuple 631 8 7 26 1 8589 1 32
23
Finding the k-th smallest element 6 3 1 8 7 2 6 1 8 5 8 9 1 3 2 1) sort each 5-tuple
24
Finding the k-th smallest element 1 3 6 8 7 2 6 1 8 5 8 9 1 3 2 1) sort each 5-tuple
25
Finding the k-th smallest element 1 3 6 8 7 2 6 1 8 5 8 9 1 3 2 1) sort each 5-tuple
26
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 8 9 1 3 2 1) sort each 5-tuple
27
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 1) sort each 5-tuple TIME = ?
28
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 1) sort each 5-tuple TIME = (n)
29
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 2) find median of the middle n/5 elements TIME = ?
30
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 2) find median of the middle n/5 elements TIME = T(n/5)
31
Finding the k-th smallest element 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 At least ? Many elements in the array are X
32
Finding the k-th smallest element 1 2 3 9 7 At least ? Many elements in the array are X 1 2 5 8 6 1 3 6 8 7
33
Finding the k-th smallest element 1 2 3 9 7 At least 3n/10 elements in the array are X 1 2 5 8 6 1 3 6 8 7
34
Finding the k-th smallest element 1 2 3 9 7 At least 3n/10 elements in the array are X 1 2 5 8 6 1 3 6 8 7 631 8 7 26 1 8589 1 32
35
Finding the k-th smallest element At least 3n/10 elements in the array are X 631 8 7 26 1 8589 1 32 631 3 2 21 1 8589 6 87 XX XX
36
Finding the k-th smallest element At least 3n/10 elements in the array are X 631 8 7 26 1 8589 1 32 631 3 2 21 1 8589 6 87 XX XX Recurse, time ?
37
Finding the k-th smallest element At least 3n/10 elements in the array are X 631 8 7 26 1 8589 1 32 631 3 2 21 1 8589 6 87 XX XX Recurse, time T(7n/10)
38
Finding the k-th smallest element 6 3 1 8 7 2 6 1 8 5 8 9 1 3 2 631 8 7 26 1 8589 1 32 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 63 1 87 2 618 58 913 2 63 1 32 2 1 1 85 8 968 7 XX XX recurse
39
Finding the k-th smallest element 6 3 1 8 7 2 6 1 8 5 8 9 1 3 2 631 8 7 26 1 8589 1 32 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 1 3 6 8 7 1 2 5 8 6 1 2 3 9 7 63 1 87 2 618 58 913 2 63 1 32 2 1 1 85 8 968 7 XX XX recurse n) T(n/5) n) T(7n/10)
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 317165912 1 3 7 1 5 6 1 2 9 63 1 87 2 618 58 913 2 63 1 32 2 1 1 85 8 968 7 XX XX recurse n) 1 3 7 1 5 6 1 2 9
43
Why 5-tuples? 3 1 7 6 1 5 9 1 2 317165912 1 3 7 1 5 6 1 2 9 63 1 87 2 618 58 913 2 63 1 32 2 1 1 85 8 968 7 XX XX recurse n) T(2n/3) 1 3 7 1 5 6 1 2 9 T(n/3)
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.