Professional Documents
Culture Documents
This course.
■ Counting inversions, closest pair of points, order statistics, fast
matrix multiplication, fast integer multiplication, FFT.
O( 1 ) if N ≤ n 0
T(N) cN
T( N ) ≤ T ( N / 2 ) + T ( N / 2 ) + O ( N ) otherwise
1 4243 14243 123
solve left half solve right half combine
T(N/2) T(N/2) 2(cN/2)
cN log2N
5 6
⇒ T ( N ) ≤ cN log 2 N
Closeness metric.
■ My rank = { 1, 2, . . . , N }.
Proof by induction on N.
■ Your rank = { a1, a2, . . . , aN }.
■ Base case: N = 1.
■ Number of inversions between two preference lists.
■ Define n1 = n / 2 , n2 = n / 2.
■ Songs i and j inverted if i < j, but ai > aj
■ Induction step: assume true for 1, 2, . . . , N – 1.
T ( N ) ≤ T ( n1 ) + T ( n2 ) + cn Songs
n2 = n / 2
≤ cn1 log 2 n1 + cn2 log 2 n2 + cn
A B C D E
≤ 2 2 /2
log n Inversions
≤ cn1 log 2 n2 + cn2 log 2 n2 + cn
Me 1 2 3 4 5 {3, 2}, {4, 2}
= cn log 2 n2 + cn ⇒ log 2 n2 ≤ log 2 n − 1 You 1 3 4 2 5
≤ cn( log 2 n − 1 ) + cn
= cn log 2 n
7 Inversion 8
Counting Inversions Counting Inversions: Divide-and-Conquer
Brute-force solution. Divide-and-conquer.
■ Check all pairs i and j such that i < j.
■ Θ (N2) comparisons.
9 10
1 5 4 8 10 2 6 9 12 11 3 7 O(1) 1 5 4 8 10 2 6 9 12 11 3 7 O(1)
1 5 4 8 10 2 6 9 12 11 3 7 1 5 4 8 10 2 6 9 12 11 3 7 2T(N / 2)
5 blue-blue inversions 8 green-green inversions
11 12
Counting Inversions: Divide-and-Conquer Counting Inversions: Divide-and-Conquer
Divide-and-conquer. Divide-and-conquer.
■ Divide: separate list into two pieces. ■ Divide: separate list into two pieces.
■ Conquer: recursively count inversions in each half. ■ Conquer: recursively count inversions in each half.
■ Combine: count inversions where ai and aj are in different halves. ■ Combine: count inversions where ai and aj are in different halves.
■ Return sum of three quantities.
1 5 4 8 10 2 6 9 12 11 3 7 O(1) 1 5 4 8 10 2 6 9 12 11 3 7 O(1)
1 5 4 8 10 2 6 9 12 11 3 7 2T(N / 2) 1 5 4 8 10 2 6 9 12 11 3 7 2T(N / 2)
5 blue-blue inversions 8 green-green inversions 5 blue-blue inversions 8 green-green inversions
13 14
1 5 4 8 10 2 6 9 12 11 3 7 O(1) 1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 2T(N / 2)
1 2 4 5 8 10 3 6 7 9 11 12 O(N log N)
5 blue-blue inversions 8 green-green inversions
17 18
21
12
19 20
Closest Pair Closest Pair
Algorithm. Algorithm.
■ Divide: draw vertical line so that roughly N / 2 points on each side. ■ Divide: draw vertical line so that roughly N / 2 points on each side.
■ Conquer: find closest pair in each side recursively. ■ Conquer: find closest pair in each side recursively.
■ Combine: find closest pair with one point in each side. ■ Combine: find closest pair with one point in each side.
■ Return best of 3 solutions.
8 8
21 21
12 12
21 22
8
21
δ = min(12, 21)
12
23 24
Closest Pair Closest Pair
Key step: find closest pair with one point in each side. Key step: find closest pair with one point in each side.
■ Extra information: closest pair entirely in one side had distance δ. ■ Extra information: closest pair entirely in one side had distance δ.
■ Observation: only need to consider points S within δ of line. ■ Observation: only need to consider points S within δ of line.
■ Sort points in strip S by their y coordinate.
– suffices to compute distances for pairs within constant number
of positions of each other in sorted list!
5
21 4 21
2
1
δ 25 δ 26
28 δ/2 O(N) Scan in y order, and compute distance between each point
27
and next 11 neighbors.
26
O(N) If any of these distances is less than δ , update δ .
25
T (N ) = T ( N / 2 ) + T ( N / 2 ) + O (N )
1 1 0 1 0 1 0 1 0
⇒ T(N ) = O (N log N ) Application.
0 0 0 0 0 0 0 0 0
■ Cryptography.
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0
+ 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0
29 30
31 32
Karatsuba Multiplication: Analysis Matrix Multiplication
To multiply two N-digit integers: Given two N x N matrices A and B, compute C = AB.
■ Add two N/2 digit integers. N
■ Multiply three N/2-digit integers. cij = ∑ aik bkj
k =1
■ Subtract two N/2-digit integers, and shift to obtain result.
■ Brute force: Θ (N3) time.
Karatsuba-Ofman (1962). p = wy (wz + xy ) = r − p − q
O(N1.585) bit operations. q = xz 26 62 98 0 2 4 1 7 13
■
80 224 368 = 6 8 10 × 3 9 15
r = (w + x )( y + z ) 134 386 638 12 14 16 5 11 17
P1 = a × (g − h ) r = P5 + P4 − P2 + P6
P2 = (a + b ) × h s = P1 + P2
C11 C12 A11 A12 B11 B12 C11 = (A11 × B11 ) + (A12 × B21 ) = (c + d ) × e
= P3 t = P3 + P4
C21 C22 A21 A22 B21 B22 C12 = (A11 × B12 ) + (A12 × B22 ) = d × (f − e )
P4 u = P5 + P1 − P3 − P7
C21 = (A21 × B11 ) + (A22 × B21 ) P5 = (a + d ) × (e + h )
C22 = (A21 × B12 ) + (A22 × B22 )
P6 = ( b − d ) × (f + h )
P7 = (a − c ) × (e + g )
T(N ) =
142 43
8T (N / 2 ) +
1 4 44 24443
Θ (N 2 )
recursive calls add, form submatrices
⇒ T(N ) = Θ(N 3 )
■ 7 multiplications.
■ 18 = 10 + 8 additions and subtractions.
C11 C12 A11 A12 B11 B12 Two 3 x 3 matrices with only 21 scalar multiplications?
= ! Also impossible.
C21 C22 A21 A22 B21 B22 Θ (N log3 21) = O (N 2.77 )
39 40
Fast Select Fast Partition
Similar to quicksort, but throw away useless "half" at each iteration. FastPartition().
■ Select ith smallest element from a1, a2, . . . , aN. ■ Divide N elements into N/5 groups of 5 elements each, plus extra.
else if (i < k) 28 23 06 26 40 19 01 46 31 49 08
b[] ← all items of a[] less than x
return FastSelect(ith, k-1, b1, b2, ..., bk-1)
14 09 05 03 54 30 48 47 32 51 21
else if (i > k)
c[] ← all items of a[] greater than x 45 39 50 15 25 16 41 17 33 07
return FastSelect((i-k)th, N-k, c1, c2, ..., cN-k)
N = 54
41 42
29
14 10
9 38
05 37
03 02 55
12 18
01 24
17 34
20 35
04 36 14 9 05 03 02 12 01 17 20 04 36
22 44
10 52
06 11 53
25 12
16 13 43
24 20
31 04
07 27 22 10 06 11 25 16 13 24 31 07 27
28 23 06
38 26
15 40 19 01
18 46
43 31
32 49
35 08 28 23 38 15 40 19 18 43 32 35 08
14
29 09
39 05
50 03
26 54
53 30 48
41 47
46 32
33 51
49 21 29 39 50 26 53 30 41 46 33 49 21
45 39
44 50
52 15
37 25
54 16
53 41
48 17
47 33
34 07
51 45 44 52 37 54 53 48 47 34 51
43 44
Fast Selection and Fast Partition Fast Selection Analysis
FastPartition(). Crux of proof: at least 25% of elements thrown away at each step.
■ Divide N elements into N/5 groups of 5 elements each, plus extra. ■ At least 1/2 of 5 element medians ≤ x
■ Brute force sort each of the 5-element groups. – at least N / 5 / 2 = N / 10 medians ≤ x
FastSelect().
■ Call FastPartition(). Let x be partition element used, and let k
be its rank. 14 9 05 03 02 12 01 17 20 04 36
■ Call FastSelect() recursively to find ith smallest element. median of
medians 22 10 06 11 25 16 13 24 31 07 27
– return x if i = k
– return ith smallest on left side if i < k
28 23 38 15 40 19 18 43 32 35 08
– return (i-k)th smallest on right side if i > k
29 39 50 26 53 30 41 46 33 49 21
45 44 52 37 54 53 48 47 34 51
45 46
14 9 05 03 02 12 01 17 20 04 36 14 9 05 03 02 12 01 17 20 04 36
median of median of
medians 22 10 06 11 25 16 13 24 31 07 27 medians 22 10 06 11 25 16 13 24 31 07 27
28 23 38 15 40 19 18 43 32 35 08 28 23 38 15 40 19 18 43 32 35 08
29 39 50 26 53 30 41 46 33 49 21 29 39 50 26 53 30 41 46 33 49 21
45 44 52 37 54 53 48 47 34 51 45 44 52 37 54 53 48 47 34 51
47 48
Fast Selection Analysis Fast Selection Analysis
Crux of proof: at least 25% of elements thrown away at each step. Analysis of recurrence.
■ At least 1/2 of 5 element medians ≤ x
c if N < 50
– at least N / 5 / 2 = N / 10 medians ≤ x T(N ) ≤
T ( N / 5 ) + T (N − 3 N / 10 ) + cN otherwise
■ At least 3 N / 10 elements ≤ x.
■ At least 3 N / 10 elements ≥ x.
⇒ FastSelect() called recursively with at most N - 3 N / 10
elements in last step Claim: T(N) ≤ 20cN.
■ Base case: N < 50.
T(N ) ≤
1T (42
N 43
median of medians
144424443 142
/ 5 ) + T (N − 3 N / 10 ) +
recursive select
43
O (N )
insertion sort
■ Inductive step: assume true for 1, 2, . . . , N-1.
⇒ T (N ) = O (N ). T (N ) ≤ T ( N / 5 ) + T (N − 3 N / 10 ) + cN
≤ 20c N / 5 + 20c ( N − 3 N / 10 ) + cN For n ≥ 50,
≤ 20c (N / 5 ) + 20c (N ) − 20c (N / 4 ) + cN 3 N / 10 ≥ N / 4.
= 20cN
49 50
Quicksort.
■ Worst case O(N log N) if always partition on median.
■ Justifies practical variants: median-of-3, median-of-5.
51