administrative sep. 20 (today) – hw1 due sep. 21 8am – problem session sep. 25 – hw3 (=quiz...
Post on 19-Dec-2015
213 views
TRANSCRIPT
AdministrativeSep. 20 (today) – HW1 dueSep. 21 8am – problem session
Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 dueSep. 28 8am – problem session
Oct. 2Oct. 4 – QUIZ #2
(pages 45-79 of DPV)
Merge 2 sorted listsMERGE
INSTANCE: 2 lists xi, yi such that x1 x2 … xn
y1 y2 … ym SOLUTION: ordered merge
1 i 1, j 12 while i n and j n do 3 if xi yj then 4 output xi, i i + 15 else 6 output yj, j j + 17 output remaining elements
Merge 2 sorted lists
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
[ … n/2 … ] [ … n/2 … ]
[ ... n … ]
[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]
Depth ?
Running time?
Mergesort
[ … n/2 … ] [ … n/2 … ]
[ ... n … ]
[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]
Depth = log n
Time spent on merge?
Mergesort
[ … n/2 … ] [ … n/2 … ]
[ ... n … ]
[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]
Depth = log n
Time spent on merge?
Mergesort
[ … n/2 … ] [ … n/2 … ]
[ ... n … ]
[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]
Depth = log n
O(n)
O(n)
O(n)
O(n.logn)
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(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 + b0Multiply(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-OffmanMultiply(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-OffmanMultiply(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-Offmanab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0
Can compute in less than 4 multiplications?
Karatsuba-Offmanab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 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-OffmanMultiply(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-OffmanMultiply(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)
Finding the minimum
How many comparisons?
comparison based algorithm:
The only allowed operation is comparing the elements
Finding the k-th smallest element
631
87
261
85
891
32
1) sort each 5-tuple
6 3 1 87 2 6 1 85 8 9 1 32
Finding the k-th smallest element
136
87
125
86
123
97
2) find median of the middle n/5 elements
TIME = ?
Finding the k-th smallest element
136
87
125
86
123
97
2) find median of the middle n/5 elements
TIME = T(n/5)
Finding the k-th smallest element
123
97
At least 3n/10 elements in the array are X
125
86
136
87
6 3 1 87 2 6 1 85 8 9 1 32
Finding the k-th smallest element
At least 3n/10 elements in the array are X
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87
X X
Finding the k-th smallest element
At least 3n/10 elements in the array are X
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87
X X
Recurse, time ?
Finding the k-th smallest element
At least 3n/10 elements in the array are X
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87
X X
Recurse, time T(7n/10)
Finding the k-th smallest element
631
87
261
85
891
32
6 3 1 87 2 6 1 85 8 9 1 32
136
87
125
86
123
97
136
87
125
86
123
97
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87X X
recurse
Finding the k-th smallest element
631
87
261
85
891
32
6 3 1 87 2 6 1 85 8 9 1 32
136
87
125
86
123
97
136
87
125
86
123
97
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87X X
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)
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?
317
615
912
3 1 7 16 5 9 1 2
137
156
129
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87X X
recurse
n)n)
137
156
129
Why 5-tuples?
317
615
912
3 1 7 16 5 9 1 2
137
156
129
6 3 1 87 2 6 1 85 8 9 1 32
63 1 32 2 1 1 85 8 9 6 87X X
recurse
n)n)
T(2n/3)137
156
129
T(n/3)