Professional Documents
Culture Documents
Divide and Conquer 4up
Divide and Conquer 4up
Divide and Conquer 4up
Divide-and-Conquer
"Divide et impera" "Veni, vidi, vici" - Julius Caesar 100BC - 44BC
Break up problem into two pieces of equal size. Solve two sub-problems independently by recursion. Combine two results in overall solution in linear time.
Consequence.
s
Familiar example.
s
Mergesort.
This course.
s
Counting inversions, closest pair of points, order statistics, fast matrix multiplication, fast integer multiplication, FFT.
Orders of Magnitude
1 second 10 seconds 1.7 minutes 17 minutes 2.8 hours 1.1 days 1.6 weeks 3.8 months 3.1 years 3.1 decades 3.1 centuries forever age of universe Powers of 2 210 220 230 thousand million billion
4
Meters Per Second 10-10 10-8 10-6 10-4 10-2 1 102 104 106 108
Imperial Units 1.2 in / decade 1 ft / year 3.4 in / day 1.2 ft / hour 2 ft / minute 2.2 mi / hour 220 mi / hour 370 mi / min 620 mi / sec 62,000 mi / sec
Example Continental drift Hair growing Glacier Gastro-intestinal tract Ant Human walk Propeller airplane Space shuttle Earth in galactic orbit 1/3 speed of light
10 msec 1 second 1.7 minutes 2.8 hours 1.7 weeks 10,000 77,000 600,000 2.9 million 100
10 million 41 millennia Max size problem solved in one second minute hour day 920 3,600 14,000 41,000 1,000
T(N)
cN 2(cN/2)
T(N/2)
Implicitly assumes N is a power of 2. Implicitly assume T(N) O(1) for sufficiently small N.
T(N/4)
T(N/4)
T(N/4)
T(N/4) log2N
4(cN/4) ...
Solution.
s
T(N / 2k)
Any function satisfying above recurrence is O(N log2 N). Equivalently, O(logb N) for any b > 1. T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)
Analysis of Recurrence
if N = 1 0 T( N ) T ( N / 2 ) + T ( N / 2 ) + cN otherwise 14 4 2 3 { 14243 solve right half combine solve left half T ( N ) cN log 2 N
Proof by induction on N.
s s s
Counting Inversions
Web site tries to match your preferences with others on Internet. You rank N songs. Web site consults database to find people with similar rankings.
Closeness metric.
s
My rank = { 1, 2, . . . , N }. Your rank = { a1, a2, . . . , aN }. Number of inversions between two preference lists. Songs i and j inverted if i < j, but ai > aj Songs
T ( N ) T ( n1 ) + T ( n2 ) + cn cn1 log 2 n1 + cn2 log 2 n2 + cn cn1 log 2 n2 + cn2 log 2 n2 + cn = cn log 2 n2 + cn cn( log 2 n 1 ) + cn = cn log 2 n
n2
= n / 2 log n 2 2 /2
A Me You 1 1
B 2 3
C 3 4 Inversion
D 4 2
E 5 5
log 2 n2 log 2 n 1
Counting Inversions
Brute-force solution.
s
Asymptotically faster algorithm must compute total number without even looking at each inversion individually. 1 5 4 8 10 2 6 9 12 11 3 7
10
Divide: separate list into two pieces. Conquer: recursively count inversions in each half separately.
1 1
5 5
4 4
8 8
10 10
2 2
6 6
9 9
12 11 12 11
3 3
7 7
O(1)
1 1
5 5
4 4
8 8
10 10
2 2
6 6
9 9
12 11 12 11
3 3
7 7
O(1)
2T(N / 2)
5 blue-blue inversions
8 green-green inversions
11
12
Divide: separate list into two pieces. Conquer: recursively count inversions in each half. Combine: count inversions where ai and aj are in different halves.
Divide: separate list into two pieces. Conquer: recursively count inversions in each half. Combine: count inversions where ai and aj are in different halves. Return sum of three quantities.
1 1
5 5
4 4
8 8
10 10
2 2
6 6
9 9
12 11 12 11
3 3
7 7
O(1)
1 1
5 5
4 4
8 8
10 10
2 2
6 6
9 9
12 11 12 11
3 3
7 7
O(1)
2T(N / 2)
2T(N / 2)
5 blue-blue inversions
5 blue-blue inversions
9 blue-green inversions: {5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}
9 blue-green inversions: {5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7} Total = 5 + 8 + 9 = 22.
13
O(1)
14
Divide: separate list into two pieces. Conquer: recursively count inversions in each half. Combine: count inversions where ai and aj are in different halves. Return sum of three quantities.
Key idea: easy if each half is sorted. Sort each half. Count inversions.
1 1
5 5
4 4
8 8
10 10
2 2
6 6
9 9
12 11 12 11
3 3
7 7
O(1)
10
12 11
5 blue-blue inversions
9 blue-green inversions: {5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7} Total = 5 + 8 + 9 = 22.
9 blue-green inversions: 4 + 2 + 2 + 1 + 0 + 0
O(N)
T(N ) = T ( N / 2 ) + T ( N / 2 ) + O (N log N )
T(N ) = O (N log2 N )
16
Closest Pair
Given N points in the plane, find a pair that is closest together.
s
Assume each half is pre-sorted. Count inversions. Merge two sorted halves into sorted whole.
For concreteness, we assume Euclidean distances. Foundation of then-fledgling field of computational geometry. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control.
13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 2 3 7 10 11 14 16 17 18 19 23 25
T (N ) = T ( N / 2 ) + T ( N / 2 ) + O (N ) T(N ) = O (N log N )
17
18
Closest Pair
Algorithm.
s
Closest Pair
Algorithm.
s
Divide: draw vertical line so that roughly N / 2 points on each side. Conquer: find closest pair in each side recursively.
21
12
19
20
Closest Pair
Algorithm.
s
Closest Pair
Algorithm.
s
Divide: draw vertical line so that roughly N / 2 points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side.
Divide: draw vertical line so that roughly N / 2 points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions.
21
21
12
12
21
22
Closest Pair
Algorithm.
s
Closest Pair
Key step: find closest pair with one point in each side.
s
Divide: draw vertical line so that roughly N / 2 points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions.
21 = min(12, 21)
12
23
24
Closest Pair
Key step: find closest pair with one point in each side.
s
Closest Pair
Key step: find closest pair with one point in each side.
s
Extra information: closest pair entirely in one side had distance . Observation: only need to consider points S within of line.
Extra information: closest pair entirely in one side had distance . 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!
7 6
21 = min(12, 21)
5 4
21 = min(12, 21)
12
12
2 1
25
26
Closest Pair
S = list of points in the strip sorted by their y coordinate. Crucial fact: if p and q are in S, and if d(p, q) < , then they are within 11 positions of each other in S.
s
Closest Pair
= ClosestPair (p1, p2 , . . . , pN ) O(N log N)
39 31
Compute separation line x = xmed such that half the points have x coordinate less than xmed, and half are greater. 1 = ClosestPair(left half) 2 = ClosestPair(right half) = min (1 , 2 ) Delete all points further than from separation line. Sort remaining points in strip by y coordinate. Scan in y order, and compute distance between each point and next 11 neighbors. If any of these distances is less than , update .
2T(N / 2)
No two points lie in same box. Two points at least 2 rows apart have distance 2 / 2.
2 rows
/2
30 29 28
/2 /2
27
26 25
T(N ) = T ( N / 2 ) + T ( N / 2 ) + O (N log N )
T(N ) = O (N log2 N )
28
27
Closest Pair
Can we achieve O(N log N)?
s
Integer Arithmetic
Given two N-digit integers a and b, compute a + b.
s
Yes. Dont sort points in strip from scratch each time. Each recursive call should return two lists: all points sorted by y coordinate, and all points sorted by x coordinate. Sorting is accomplished by merging two already sorted lists.
1 1 0 1 0 1 0 1 * 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
T (N ) = T ( N / 2 ) + T ( N / 2 ) + O (N )
T(N ) = O (N log N )
Application.
s
Cryptography.
1 + 1
1 1 0 0
1 1 1 1
1 0 1 0
1 1 1 1
1 0 1 0
0 1 1 0
1 0 0 1 1 1 0
1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 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
Karatsuba Multiplication
To multiply two N-digit integers:
s
Multiply four N/2-digit integers. Add two N/2-digit integers, and shift to obtain result.
3 3
Add two N/2 digit integers. Multiply three N/2-digit integers. Subtract two N/2-digit integers, and shift to obtain result.
123,456 987,654 = (10 w + x ) (10 y + z ) = 106 (wy ) + 103 (wz + xy ) + 100 ( xz ) = 106 (121,401) + 103 (80,442 + 450,072 ) + 100 (298,224 )
w x y z
= 121,401,29 9,224
w x y z
= 106 ( p ) + 103 (r p q ) + 100 (q ) = 106 (121,401) + 103 (950,139 - 121,401 - 298,224 ) + 100 (298,224 ) 121,401,29 9,224
ab = (10N / 2 w + x )(10N / 2 y + z )
N is a power of 2
T(N ) = 4 ) 1T (N43) + 142N43 42 / 2 add,(shift recursive calls T(N ) = (N 2 )
p = wy (wz + xy ) = r p q q = xz r = (w + x )( y + z )
31
32
Matrix Multiplication
Given two N x N matrices A and B, compute C = AB.
Add two N/2 digit integers. Multiply three N/2-digit integers. Subtract two N/2-digit integers, and shift to obtain result.
s
Karatsuba-Ofman (1962).
s
p = wy (wz + xy ) = r p q q = xz r = (w + x )( y + z )
ab = (10N / 2 w + x )(10N / 2 y + z )
T(N ) T ( N / 2 T(N ) = O (N
( ) 142N43
L c L c L c M M O M L c c
c13 c23 c33
N3
3N NN
1N 2N
L a L a L a M M O M a L a
a13 a23 a33
N3
3N NN
1N 2N
L b L b L b M M O M b L b
b13 b23 b33
N3
3N NN
1N 2N
Divide: partition A and B into N/2 x N/2 blocks. Conquer: multiply 8 N/2 x N/2 recursively. Combine: add appropriate products using 4 matrix additions.
s a b e g = u c d f h
C11 C12 A11 A12 B11 B12 = C 21 C22 A21 A22 B21 B22
= = = =
(A11 B11 ) + (A12 B21 ) (A11 B12 ) + (A12 B22 ) (A21 B11 ) + (A22 B21 ) (A21 B12 ) + (A22 B22 )
T(N ) = (N 3 )
s
P1 P2 P3 P4 P5 P6 P7
= a (g h ) = (a + b ) h = (c + d ) e = d (f e ) = (a + d ) (e + h ) = ( b d ) (f + h ) = (a c ) (e + g )
r s t u
= P5 + P4 P2 + P6 = P1 + P2 = P3 + P4 = P5 + P1 P3 P7
T(N ) =
Beyond Strassen
Can you multiply two 2 x 2 matrices with only 7 scalar multiplications? ! Yes! Strassen (1969). (N log2 7) = O (N 2.81) Can you multiply two 2 x 2 matrix with only 6 scalar multiplications? ! Impossible (Hopcroft and Kerr, 1971). (N log2 6) = O (N 2.59 ) Two 3 x 3 matrices with only 21 scalar multiplications? ! Also impossible. (N log3 21) = O (N 2.77 ) Two 70 x 70 matrices with only 143,640 scalar multiplications? ! Yes! (Pan, 1980). (N log70 143640 ) = O (N 2.80 ) Decimal wars. ! December, 1979: O(N2.521813). ! January, 1980: O(N2.521801). Coppersmith-Winograd (1987): O(N2.376).
37 38
Divide: partition A and B into N/2 x N/2 blocks. Compute: 14 N/2 x N/2 matrices via 10 matrix add/subtract. Conquer: multiply 7 N/2 x N/2 recursively. Combine: 7 products into 4 terms using 8 matrix add/subtract.
C11 C12 A11 A12 B11 B12 = C 21 C22 A21 A22 B21 B22
Analysis.
s
Strassen in Practice?
Practical considerations.
s
Order Statistics
Given N linearly ordered elements, find ith smallest element.
s
Stop recursion around N = 100. Numerical stability. Harder to parallelize. Caching effects.
Minimum if i = 1. Maximum if i = N. Median: i = (N+1) / 2 if N is odd i = N/2 or i = N/2 + 1 Easy to do with O(N) comparisons if i or N i is a constant. Easy to do in general with O(N log2N) comparisons by sorting.
Yes. (Blum, Floyd, Pratt, Rivest, Tarjan, 1973) Cool and simple idea. Ahead of its time.
39
40
Fast Select
Similar to quicksort, but throw away useless "half" at each iteration.
s
Fast Partition
FastPartition().
s
Select
ith
smallest element from a1, a2, . . . , aN. FastSelect (ith, N, a1, a2, . . . , aN)
29 22 28 14 45
10 44 23 09 39
38 52 06 05 50
37 11 26 03 15
02 53 40 54 25
55 12 19 30 16
18 13 01 48 41
24 43 46 47 17
34 20 31 32 33
35 04 49 51 07
36 27 08 21
else if (i < k) b[] all items of a[] less than x return FastSelect(ith, k-1, b1, b2, ..., bk-1) else if (i > k) c[] all items of a[] greater than x return FastSelect((i-k)th, N-k, c1, c2, ..., cN-k)
N = 54
41 42
Fast Partition
FastPartition().
s
Fast Partition
FastPartition().
s
Divide N elements into N/5 groups of 5 elements each, plus extra. Brute force sort each of the 5-element groups.
Divide N elements into N/5 groups of 5 elements each, plus extra. Brute force sort each of the 5-element groups. Find x = "median of medians" using FastSelect() recursively.
14 29 22 28 29 14 45
10 9 10 44 23 39 09 44 39
05 38 06 52 38 06 50 05 52 50
03 37 11 15 26 26 03 37 15
02 25 53 40 53 54 54 25
12 55 16 12 19 30 53 16
01 18 13 18 01 41 48 48 41
17 24 24 43 43 46 46 47 47 17
20 34 31 20 32 31 33 32 34 33
04 35 07 04 35 49 49 51 51 07
36 27 08 21
14 22 28 29 45
9 10 23 39 44
05 06 38 50 52
03 11 15 26 37
02 25 40 53 54
12 16 19 30 53
01 13 18 41 48
17 24 43 46 47
20 31 32 33 34
04 07 35 49 51
36 27 08 21
43
44
Divide N elements into N/5 groups of 5 elements each, plus extra. Brute force sort each of the 5-element groups. Find x = "median of medians" using FastSelect() recursively.
FastSelect().
s
Call FastPartition(). Let x be partition element used, and let k be its rank. Call FastSelect() recursively to find
14 median of medians 22 28 29 45
9 10 23 39 44
05 06 38 50 52
03 11 15 26 37
02 25 40 53 54
12 16 19 30 53
01 13 18 41 48
17 24 43 46 47
20 31 32 33 34
04 07 35 49 51
36 27 08 21
ith smallest
element.
return x if i = k return ith smallest on left side if i < k return (i-k)th smallest on right side if i > k
45
46
At least 1/2 of 5 element medians x at least N / 5 / 2 = N / 10 medians x At least 3 N / 10 elements x. At least 3 N / 10 elements x.
14 median of medians 22 28 29 45
9 10 23 39 44
05 06 38 50 52
03 11 15 26 37
02 25 40 53 54
12 16 19 30 53
01 13 18 41 48
17 24 43 46 47
20 31 32 33 34
04 07 35 49 51
36 27 08 21 median of medians
14 22 28 29 45
9 10 23 39 44
05 06 38 50 52
03 11 15 26 37
02 25 40 53 54
12 16 19 30 53
01 13 18 41 48
17 24 43 46 47
20 31 32 33 34
04 07 35 49 51
36 27 08 21
47
48
At least 1/2 of 5 element medians x at least N / 5 / 2 = N / 10 medians x 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
T(N )
median of medians
Base case: N < 50. Inductive step: assume true for 1, 2, . . . , N-1.
insertion sort
T (N ) = O (N ).
T (N ) T ( N / 5 ) + T (N 3 N / 10 ) + cN
For n 50, 3 N / 10 N / 4.
49
50
Constant (currently) too large to be useful. Practical variant: choose random partition element. O(N) expected running time ala quicksort. Open problem: guaranteed O(N) with better constant.
Quicksort.
s
Worst case O(N log N) if always partition on median. Justifies practical variants: median-of-3, median-of-5.
51