Download presentation
Presentation is loading. Please wait.
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 M ERGE INSTANCE: 2 lists x i, y i such that x 1 x 2 … x n y 1 y 2 … y m SOLUTION: ordered merge
3
1 i 1, j 1 2 while i n and j n do 3 if x i y j then 4 output x i, i i + 1 5 else 6 output y j, j j + 1 7 output remaining elements Merge 2 sorted lists
4
M ERGE -S ORT (a,l,r) if l < r then m (l+r)/2 M ERGE -S ORT (a,I,m) M ERGE -S ORT (a,m+1,r) M ERGE (a,l,m,r) Mergesort
5
Running time? Mergesort
6
Running time? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth ?
7
Running time? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n
8
Time spent on merge? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n
9
Time spent on merge? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n O(n) O(n.logn)
10
recurrence T(n)= T(n/2) + (n) if n>1 T(1)= (1) Mergesort
11
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
12
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
13
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
14
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
15
Karatsuba-Offman a=2 n/2 a 1 + a 0 b=2 n/2 b 1 + b 0 ab=(2 n/2 a 1 +a 0 )(2 n/2 b 1 +b 0 ) = 2 n a 1 b 1 + 2 n/2 (a 1 b 0 + a 0 b 1 ) + a 0 b 0
16
Karatsuba-Offman a=2 n/2 a 1 + a 0 b=2 n/2 b 1 + b 0 Multiply(a,b,n) if n=1 return a*b else R 1 Multiply(a 1,b 1,n/2) R 2 Multiply(a 0,b 1,n/2) R 3 Multiply(a 1,b 0,n/2) R 4 Multiply(a 0,b 0,n/2) return 2 n R 1 + 2 n/2 (R 2 +R 3 ) + R 4
17
Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R 1 Multiply(a 1,b 1,n/2) R 2 Multiply(a 0,b 1,n/2) R 3 Multiply(a 1,b 0,n/2) R 4 Multiply(a 0,b 0,n/2) return 2 n R 1 + 2 n/2 (R 2 +R 3 ) + R 4 Recurrence?
18
Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R 1 Multiply(a 1,b 1,n/2) R 2 Multiply(a 0,b 1,n/2) R 3 Multiply(a 1,b 0,n/2) R 4 Multiply(a 0,b 0,n/2) return 2 n R 1 + 2 n/2 (R 2 +R 3 ) + R 4 Recurrence? T(n) = 4T(n/2) + O(n)
19
Karatsuba-Offman T(n) = 4T(n/2) + O(n) T(n)=O(n 2 )
20
Karatsuba-Offman ab=(2 n/2 a 1 +a 0 )(2 n/2 b 1 +b 0 ) = 2 n a 1 b 1 + 2 n/2 (a 1 b 0 + a 0 b 1 ) + a 0 b 0 Can compute in less than 4 multiplications?
21
Karatsuba-Offman ab=(2 n/2 a 1 +a 0 )(2 n/2 b 1 +b 0 ) = 2 n a 1 b 1 + 2 n/2 (a 1 b 0 + a 0 b 1 ) + a 0 b 0 Can compute using 3 multiplications: (a 0 +a 1 )(b 0 +b 1 ) = a 0 b 0 + (a 1 b 0 + a 0 b 1 ) + a 1 b 1
22
Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R 1 Multiply(a 1,b 1,n/2) R 2 Multiply(a 0,b 0,n/2) R 3 Multiply(a 1 +a 0,b 1 +b 0,n/2+1) R 4 R 3 – R 2 – R 1 return 2 n R 1 + 2 n/2 R 3 + R 2 Recurrence?
23
Karatsuba-Offman Multiply(a,b,n) if n=1 return a*b else R 1 Multiply(a 1,b 1,n/2) R 2 Multiply(a 0,b 0,n/2) R 3 Multiply(a 1 +a 0,b 1 +b 0,n/2+1) R 4 R 3 – R 2 – R 1 return 2 n R 1 + 2 n/2 R 3 + R 2 Recurrence? T(n) = 3T(n/2) + O(n)
24
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)
25
Karatsuba-Offman T(n) = 3T(n/2) + O(n) T(n)=O(n C ) C=log 2 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
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
31
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
32
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
33
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
34
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
35
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
36
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 = ?
37
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)
38
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 = ?
39
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)
40
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
41
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
42
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
43
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
44
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
45
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 ?
46
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)
47
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
48
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)
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
51
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
52
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)
53
Why 5-tuples? T(n) T(n/3) + T(2n/3) + (n)
54
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.