







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Most widespread application of divide-and-conquer.^ n
Break up problem into two pieces of equal size. n^
Solve two sub-problems independently by recursion. n^
Combine two results in overall solution in linear time. Consequence.^ n
Brute force / naïve solution: N
n^
Divide-and-conquer: N log N. Familiar example.^ n
Mergesort. This course.^ n
Counting inversions, closest pair of points, order statistics, fastmatrix multiplication, fast integer multiplication, FFT.
3
Time tosolve aproblemof size
10,000100,000million 10 million
1.3 seconds22 minutes
15 days41 years 41 millennia
Run time (nanoseconds)
3
second
Max sizeproblemsolved
in one
minutehour
day
10 msec1 second 1.7 minutes2.8 hours1.7 weeks
2.9 million
2
0.4 msec6 msec78 msec 0.94 seconds11 seconds
1 million49 million2.4 trillion50 trillion
47 N log
0.048 msec0.48 msec
4.8 msec48 msec 0.48 seconds
21 million1.3 billion76 trillion 1,800 trillion
N multiplied by 10,time multiplied by
Meters Per
Second
10
2
1.2 in / decade
Imperial
Units 1 ft / year3.4 in / day 1.2 ft / hour2 ft / minute2.2 mi / hour220 mi / hour
Continental drift
Example Hair growing
Glacier
Gastro-intestinal tract
Ant
Human walk Propeller airplane
4 10
6 10
8
370 mi / min620 mi / sec 62,000 mi / sec
Space shuttle
Earth in galactic orbit
1/3 speed of light
Seconds
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10
Equivalent1 second 1.7 minutes17 minutes
2.8 hours1.1 days1.6 weeks3.8 months3.1 years 3.1 decades3.1 centuries
forever
21
age of universe
thousand
million
billion
10 seconds
Powers
of 2
5
A Useful Recurrence Relation
T(N) = worst case running time on input of size N.^ n
Note: O(1) is "standard" abuse of notation. Alternate informal form: T(N)
n^
Implicitly assumes N is a power of 2. n^
Implicitly assume T(N)
O(1) for sufficiently small N.
Solution.^ n
Any function satisfying above recurrence is
O(N log
2
n^
Equivalently, O(log
b^
N) for any b > 1.
(^
)^
^
(^
)
combine
half
right
solve
half left
solve
0
Analysis of Recurrence: Recursion Tree
Assuming N is a power of 2.
cN
k^ )
2(cN/2)4(cN/4) 2
k^
(cN / 2
k^ )
...... N/2 (2c)
otherwise
if
cN
N T
N
N
log
cN log
7
Analysis of Recurrence
Proof by induction on N.^ n
Base case: N = 1. n^
Define n
1
n / 2
, n
2
n / 2
n^
Induction step: assume true for 1, 2,... , N
^
(^
)^
^
(^
)^
^
2
combine
half
right
solve
half left
solve
^
^
^
^
^
^
^
^
^
2
2
2 2
2 2 2 2 2 1 2 2 2 1 2 1
2
^
^
^
^
^
2
2 2
log
2
2
n
Counting Inversions
Web site tries to match your preferences with others on Internet.^ n
You rank N songs. n^
Web site consults database to find people with similar rankings. Closeness metric.^ n
My rank = { 1, 2,... , N }. n^
Your rank = { a
, a 1
,... , a 2
N^
n^
Number of inversions between two preference lists. n^
Songs i and j inverted if i < j, but a
ai^
j
You
Me
Songs
Inversion
Inversions{3, 2}, {4, 2} Docsity.com
13
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.^ n
Divide: separate list into two pieces. n^
Conquer: recursively count inversions in each half. n^
Combine: count inversions where a
and ai^
are in different halves.j
5 blue-blue inversions
8 green-green inversions
9 blue-green inversions:{5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.^ n
Divide: separate list into two pieces. n^
Conquer: recursively count inversions in each half. n^
Combine: count inversions where a
and ai^
are in different halves.j
n^
Return sum of three quantities.
5 blue-blue inversions9 blue-green inversions:{5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}
8 green-green inversions
Total = 5 + 8 + 9 = 22.
15
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.^ n
Divide: separate list into two pieces. n^
Conquer: recursively count inversions in each half. n^
Combine: count inversions where a
and ai^
are in different halves.j
n^
Return sum of three quantities. 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.
5 blue-blue inversions
8 green-green inversions
Can we do this step insub-quadratic time?
9 blue-green inversions: 4 + 2 + 2 + 1 + 0 + 0
Counting Inversions: Good Combine
Combine: count inversions where a
and ai
are in different halves.j
n^
Key idea: easy if each half is sorted. n^
Sort each half. n^
Count inversions.
O(N log N)O(N)
(^
)^
(^
)^
log (
log (
2
Docsity.com
17
13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0
Counting Inversions: Better Combine
Combine: count inversions where a
and ai
are in different halves.j
n^
Assume each half is pre-sorted. n^
Count inversions. n^
Merge two sorted halves into sorted whole.
(^
)^
(^
)^
log (
Closest Pair
Given N points in the plane, find a pair that is closest together.^ n
For concreteness, we assume Euclidean distances. n^
Foundation of then-fledgling field of computational geometry. n^
Graphics, computer vision, geographic information systems,molecular modeling, air traffic control. Brute force solution.^ n
Check all pairs of points p and q. n^
2 ) comparisons.
One dimensional version (points on a line).^ n
O(N log N) easy. Assumption to make presentation cleaner.^ n
No two points have same x coordinate.
19
Closest Pair
Algorithm.^ n
Divide: draw vertical line so that roughly N / 2 points on each side.
Closest Pair
Algorithm.^ n
Divide: draw vertical line so that roughly N / 2 points on each side. n^
Conquer: find closest pair in each side recursively.
Docsity.com
25
Closest Pair
Key step: find closest pair with one point in each side.^ n
Extra information: closest pair entirely in one side had distance
δ
n^
Observation: only need to consider points S within
δ
of line.
δ
= min(12, 21)
δ
Closest Pair
Key step: find closest pair with one point in each side.^ n
Extra information: closest pair entirely in one side had distance
δ
n^
Observation: only need to consider points S within
δ
of line.
n^
Sort points in strip S by their y coordinate.^ –
suffices to compute distances for pairs within constant numberof positions of each other in sorted list!
δ
= min(12, 21)
δ
1
2
(^43)
5
6 7
27
Closest Pair
S = list of points in the strip sorted by their y coordinate.Crucial fact: if p and q are in S, and ifd(p, q) <
δ
, then they are within 11
positions of each other in S.^ n
No two points lie in same box. n^
Two points at least 2 rows aparthave distance
δ
δ
27
29
30
31
28
26
25
δ
δ^
2 rows
δ^
δ^
39
Closest Pair
Compute separation line x = x
med
such that half the points
have x coordinate less than x
med
, 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 pointand next 11 neighbors.If any of these distances is less than
δ
, update
δ
δ^
= ClosestPair (p
, p 1
2
,... , p
N^
O(N log N)
O(N log N)2T(N / 2)
(^
)^
(^
)^
log (
log (
2
Docsity.com
29
Closest Pair
Can we achieve O(N log N)?^ n
Yes. Don’t sort points in strip from scratch each time. n^
Each recursive call should return two lists: all points sorted by ycoordinate, and all points sorted by x coordinate. n^
Sorting is accomplished by merging two already sorted lists.
(^
)^
(^
)^
log (
Integer Arithmetic
Given two N-digit integers a and b, compute a + b.^ n
O(N) bit operations. Multiplication: given two N-digit integers a and b,compute ab.^ n
Brute force solution:
2 ) bit operations.
Application.^ n
Cryptography.
31
To multiply two N-digit integers:^ n
Multiply four N/2-digit integers. n^
Add two N/2-digit integers, and shift to obtain result.
Divide-and-Conquer Multiplication: First Attempt
0
3
6
0
3
6
3
3
xz
xy
wz
wy
z
y
x
w
w x y^ z
(^
)^
2
shift
add,
calls
recursive
(^2) /
(^2) /
z
y
x
w
ab
N
N
43
42 1
4
3
4
2
1
N is a power of 2
To multiply two N-digit integers:^ n
Add two N/2 digit integers. n^
Multiply three N/2-digit integers. n^
Subtract two N/2-digit integers, and shift to obtain result.
Karatsuba Multiplication
z
y x
w
r
xz
q
wy
p
q p r
xy
wz
0
3
6
0
3
6
0
3
6
3
3
q
q
p r
p
xz
xy
wz
wy
z
y
x
w
w x y z
Docsity.com
37
Matrix Multiplication: Strassen
Generalize to matrices.^ n
Divide: partition A and B into N/2 x N/2 blocks. n^
Compute: 14 N/2 x N/2 matrices via 10 matrix add/subtract. n^
Conquer: multiply 7 N/2 x N/2 recursively. n^
Combine: 7 products into 4 terms using 8 matrix add/subtract. Analysis.^ n
Assume N is a power of 2. n^
T(N) = # arithmetic operations.
(^
)^
(^81). 2
7
log
subtract
add,
2
calls
recursive
2
4 4 3
(^4) 4 2 1
4
3
4
2
1
22
21
12
11
22
21
12
11
22
21
12
11
Beyond Strassen
Can you multiply two 2 x 2 matrices with only 7 scalar multiplications?
Yes! Strassen (1969).
Can you multiply two 2 x 2 matrix with only 6 scalar multiplications?
Impossible (Hopcroft and Kerr, 1971).
Two 3 x 3 matrices with only 21 scalar multiplications?
Also impossible.
Two 70 x 70 matrices with only 143,640 scalar multiplications?
Yes! (Pan, 1980).
Decimal wars.
December, 1979: O(N
January, 1980:
Coppersmith-Winograd (1987): O(N
(^77). 2
21
log
3
143640
log
70
(^59). 2
6
log
2
(^81). 2
7
log
2
39
Strassen in Practice?
Practical considerations.^ n
Stop recursion around N = 100. n^
Numerical stability. n^
Harder to parallelize. n^
Caching effects.
Order Statistics
Given N linearly ordered elements, find i
th
smallest element.
n^
Minimum if i = 1. n^
Maximum if i = N. n^
Median:^ –
i = (N+1) / 2 if N is odd – i = N/2 or i = N/2 + 1
n^
Easy to do with O(N) comparisons if i or N
- i is a constant.
n^
Easy to do in general with O(N log
N) comparisons by sorting. 2
Can we do in worst-case O(N) comparisons?^ n
Yes. (Blum, Floyd, Pratt, Rivest, Tarjan, 1973) n^
Cool and simple idea. Ahead of its time. Assumption to make presentation cleaner.^ n
All items have distinct values.
Docsity.com
41
Fast Select
Similar to quicksort, but throw away useless "half" at each iteration.^ n
Select i
th
smallest element from a
, a 1
,... , a 2
x
FastPartition(N,
a
a
..., a
k
rank(x)
if
(i
k)
return
x
else
if (i
< k)
b[]
all items
of a[]
less
than
x
return
FastSelect(i
th
k-1, b
, b 1
..., b
k-
else
if (i
> k)
c[]
all items
of a[]
greater than x
return
FastSelect((i-k)
th
N-k,
c
c
, ..., c 2
N-k
FastSelect (i
th
, N, a
, a 1
,... , a 2
N
x = partition elementis kth smallest
Fast Partition
FastPartition()
n^
Divide N elements into
groups of 5 elements each, plus extra.
43
Fast Partition
FastPartition()
n^
Divide N elements into
groups of 5 elements each, plus extra.
n^
Brute force sort each of the 5-element groups.
Fast Partition
FastPartition()
n^
Divide N elements into
groups of 5 elements each, plus extra.
n^
Brute force sort each of the 5-element groups. n^
Find x = "median of medians" using
FastSelect()
recursively.
Docsity.com
49
Fast Selection Analysis
Crux of proof: at least 25% of elements thrown away at each step.^ n
At least 1/2 of 5 element medians
x
-^
at least
medians
x
n^
At least 3
elements
x.
n^
At least 3
elements
x.
FastSelect()
called recursively with at most N - 3
elements in last step
(^
)^
sort
insertion
select
recursive
medians of
median
43
4
2 1
4 4 4
3
4 4 4
2
1
43
42 1
Fast Selection Analysis
Analysis of recurrence.Claim: T(N)
20cN.
n^
Base case: N < 50. n^
Inductive step: assume true for 1, 2,... , N-1.
otherwise
if
cN
c
(^
)
cN
cN
N c N c N c
cN
c
c
cN
For n
51
Linear Time Median Finding Postmortem
Practical considerations.^ n
Constant (currently) too large to be useful. n^
Practical variant: choose random partition element.^ –
O(N) expected running time ala quicksort.
n^
Open problem: guaranteed O(N) with better constant. Quicksort.^ n
Worst case O(N log N) if always partition on median. n^
Justifies practical variants: median-of-3, median-of-5.
Docsity.com