Title: Administrative
1Administrative
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)
2Merge 2 sorted lists
MERGE INSTANCE 2 lists xi, yi such that
x1 ? x2 ? ? xn y1 ?
y2 ? ? ym SOLUTION ordered merge
3Merge 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
4Mergesort
MERGE-SORT(a,l,r) if l lt r then m ? ?
(lr)/2 ? MERGE-SORT(a,I,m)
MERGE-SORT(a,m1,r) MERGE(a,l,m,r)
5Mergesort
Running time?
6Mergesort
Running time?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth ?
7Mergesort
Running time?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth log n
8Mergesort
Time spent on merge?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth log n
9Mergesort
Time spent on merge?
O(n)
O(n)
O(n)
Depth log n
O(n.logn)
10Mergesort
recurrence T(n) T(n/2) ?(n) if ngt1
T(1) ?(1)
11Recurrences
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
12Recurrences
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
13Recurrences
T(n) ? T( ?n/2? ) T( ?n/2? ) c.n
T(n) ? 2 T( n/2 ) c.n
G(n) T(n2) G(n) T(n2) ? T(?n/2?1)
T(?n/2?1) c.n G(?n/2?-1)
G(?n/2?-1) c.n
14Recurrences
- 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 dlt1 and ngtn0
clogb a
15Karatsuba-Offman
a2n/2 a1 a0 b2n/2 b1 b0
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
16Karatsuba-Offman
a2n/2 a1 a0 b2n/2 b1 b0
Multiply(a,b,n) if n1 return ab 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 (R2R3) R4
17Karatsuba-Offman
Multiply(a,b,n) if n1 return ab 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 (R2R3) R4
Recurrence?
18Karatsuba-Offman
Multiply(a,b,n) if n1 return ab 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 (R2R3) R4
Recurrence?
T(n) 4T(n/2) O(n)
19Karatsuba-Offman
T(n) 4T(n/2) O(n)
T(n)O(n2)
20Karatsuba-Offman
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
Can compute in less than 4 multiplications?
21Karatsuba-Offman
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
Can compute using 3 multiplications
(a0a1)(b0b1) a0b0 (a1 b0 a0 b1) a1
b1
22Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b0,n/2) R3 ? Multiply(a1a0,b1b
0,n/21) R4 ? R3 R2 R1 return 2n
R1 2n/2 R3 R2
Recurrence?
23Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b0,n/2) R3 ? Multiply(a1a0,b1b
0,n/21) R4 ? R3 R2 R1 return 2n
R1 2n/2 R3 R2
Recurrence?
T(n) 3T(n/2) O(n)
24Recurrences
- 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 dlt1 and ngtn0
clogb a
T(n) 3 T(n/2) ?(n) T(n) 2T(n/2) ?(n.log
n)
25Karatsuba-Offman
T(n) 3T(n/2) O(n)
T(n)O(nC)
Clog2 3 ? 1.58
26Finding the minimum
min ? A1 for i from 2 to n do if Ailtmin
then min ? Ai
How many comparisons?
27Finding the minimum
How many comparisons?
comparison based algorithm
The only allowed operation is
comparing the elements
28Finding the minimum
29Finding the k-th smallest element
k ?n/2? MEDIAN
30Finding the k-th smallest element
31Finding the k-th smallest element
8
2
6
8
5
8
9
3
2
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
32Finding 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
33Finding 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
34Finding 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
35Finding 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
36Finding 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
37Finding the k-th smallest element
TIME ?(n)
1) sort each 5-tuple
38Finding 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
39Finding 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
40Finding 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
41Finding 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
42Finding 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
43Finding the k-th smallest element
8
2
6
8
5
8
9
3
2
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
44Finding the k-th smallest element
8
2
6
8
5
8
9
3
2
6
3
1
7
1
1
gtX
?X
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
At least 3n/10 elements in the array are ? X
45Finding the k-th smallest element
8
2
6
8
5
8
9
3
2
6
3
1
7
1
1
gtX
?X
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
Recurse, time ?
At least 3n/10 elements in the array are ? X
46Finding the k-th smallest element
8
2
6
8
5
8
9
3
2
6
3
1
7
1
1
gtX
?X
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
Recurse, time ? T(7n/10)
At least 3n/10 elements in the array are ? X
47Finding the k-th smallest element
6
3
1
8
7
2
6
1
8
5
8
9
1
3
2
?X
gtX
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
recurse
48Finding the k-th smallest element
6
3
1
8
7
2
6
1
8
5
8
9
1
3
2
?(n)
?X
gtX
?(n)
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
recurse
T(7n/10)
T(n/5)
49Finding the k-th smallest element
T(n) ? T(n/5) T(7n/10) O(n)
50Finding 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
51Why 5-tuples?
3
1
7
1
6
5
9
1
2
6
3
1
8
7
2
6
1
8
5
8
9
1
3
2
3
6
9
1
1
1
?(n)
7
5
2
?X
gtX
?(n)
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
recurse
1
1
1
3
5
2
7
6
9
1
1
1
3
5
2
7
6
9
52Why 5-tuples?
3
1
7
1
6
5
9
1
2
6
3
1
8
7
2
6
1
8
5
8
9
1
3
2
3
6
9
1
1
1
?(n)
7
5
2
?X
gtX
?(n)
6
3
1
3
2
2
1
1
8
5
8
9
6
8
7
recurse
1
1
1
3
5
2
7
6
9
T(2n/3)
T(n/3)
1
1
1
3
5
2
7
6
9
53Why 5-tuples?
T(n) T(n/3) T(2n/3) ?(n)
54Why 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