540 likes | 566 Views
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
E N D
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 MERGE INSTANCE: 2 lists xi, yi such that x1 x2 … xn y1 y2 … ym SOLUTION: ordered merge
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
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)
Mergesort Running time?
Mergesort Running time? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth ?
Mergesort Running time? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth = log n
Mergesort Time spent on merge? [ ... n … ] [ … n/2 … ] [ … n/2 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ] Depth = log n
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)
Mergesort recurrence T(n)= T(n/2) + (n) if n>1 T(1)= (1)
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
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
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
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
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
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
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?
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)
Karatsuba-Offman T(n) = 4T(n/2) + O(n) T(n)=O(n2)
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?
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
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?
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)
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)
Karatsuba-Offman T(n) = 3T(n/2) + O(n) T(n)=O(nC) C=log2 3 1.58
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 k-th smallest element k = n/2 = MEDIAN
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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