Professional Documents
Culture Documents
CS 3AC3
Ryszard Janicki
Acknowledgments: Material based on Algorithm Design by Jon Kleinberg and Éva Tardos (Chapter 5)
Divide-and-conquer.
Divide up problem into several subproblems.
Solve each subproblem recursively.
Combine solutions to subproblems into overall solution.
Most common usage.
Divide problem of size n into two subproblems of size n/2 in
linear time.
Solve two subproblems recursively.
Combine two solutions into overall solution in linear time.
Consequence.
Other method: Θ(n2 ).
Divide-and-conquer: Θ(n log n).
Divide-and-conquer Solution:
————————————————————————————
test(m)
1 IF m = 1 THEN
2 test ← YES
3 ELSE IF (m mod 2) = 0 THEN
4 test ← test(m/2)
5 ELSE test ← NO
————————————————————————————
Ryszard Janicki Divide and Conquer 3/36
————————————————————————————
test(m)
1 IF m = 1 THEN
2 test ← YES
3 ELSE IF (m mod 2) = 0 THEN
4 test ← test(m/2)
5 ELSE test ← NO
————————————————————————————
Times assign to each line:
1 1 → c1
2 1 → c2
3 1 → c1
4 1 → c2 + T (m/2)
5 1 → c2
————————————————————————————
RECURRENCE RELATION
c1 + c2 m=1
T (m) = 2c1 + c2 m > 1 and m is odd
2c1 + c2 + T (m/2) m is even
Some notation
c1 + c2 m=1
T (m) = 2c1 + c2 m > 1 and m is odd
2c1 + c2 + T (m/2) m is even
where c = 2c1 + c2 .
Note that for all m > 0, we have:
T (m) ≤ T ′ (m),
′ c m≥1
T (m) = ′
c + T (m/2) m > 1
————————————————————————————
T ′ (m) = c + T ′ (m/2) = c + c + T ′ (m/22 ) = 2c + T ′ (m/22 )
= 3c + T ′ (m/23 )
···
= kc + T ′ (m/2k )
···
= ⌈log m⌉c + T ′ ( 2⌈log
m
m⌉ )
{ Note T ′ ( 2⌈log
m ′ m
m⌉ ) is a constant, i.e. c0 = T ( 2⌈log m⌉ ).}
T (n)
n
T (n / 2) T (n / 2) T (n / 2) 3 (n / 2)
T (n / 4) T (n / 4) T (n / 4) T (n / 4) T (n / 4) T (n / 4) T (n / 4) T (n / 4) T (n / 4) 32 (n / 22)
log2 n 3i (n / 2i)
⋮
⋮
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) ... T (1) T (1) T (1) log2 n
3 (n / 2log2 n )
log2 n log2 3
3 =n
r1+log2 n 1
r=3/2 >1 T (n) = (1 + r + r2 + r3 + . . . + rlog2 n ) n = n = 3nlog2 3 2n
r 1 4
T (n) n
T (n / 2) T (n / 2) 2 (n / 2)
T (n / 4) T (n / 4) T (n / 4) T (n / 4) 22 (n / 22)
log2 n
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) 23 (n / 23)
⋮ ⋮
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) ... T (1) T (1) T (1) n (1)
log2 n
2 =n
Pf.
T (n / 4)
・No two points lie
T (n / 4) T (n / 4) 3 (n / 4) 5
in same ½ δ-by-½ δ box.
・Two points at least 2 rows apart
2 rows
have distance ≥ 2 (½ δ). ▪
T (n / 16) T (n / 16) T (n / 16) T (n / 16) T (n / 16) T (n / 16) T (n / 16) T (n / 16) T (n / 16) 32 (n / 42)5
log4 n 3i (n / 4i)5 i
⋮
Fact. Claim remains true if we replace 12 with 7.
⋮
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) ... T (1) T (1) T (1) 3log4 n (n/4log4 n )5
3log4 n = nlog4 3
1
r = 3 / 45 < 1 n5 ≤ T (n) ≤ (1 + r + r 2 + r 3 + … ) n5 ≤ n5
1 – r 6
Theorem
Suppose that T (n) is a function on the nonnegative integers that
satisfies the recurrence
n
T (n) = aT ( ) + f (n)
b
where n/b means either ⌊n/b⌋ or ⌈n/b⌉. Let k = logb a. Then,
Case 1. If f (n) = O(nk−ε ) for some constant ε > 0, then
T (n) = Θ(nk ).
Example
T (n) = 3T (n/2) + n.
a = 3, b = 2, f (n) = n, k = log2 3.
T (n) = Θ(nlog2 3 ).
Theorem
Suppose that T (n) is a function on the nonnegative integers that
satisfies the recurrence
n
T (n) = aT ( ) + f (n)
b
where n/b means either ⌊n/b⌋ or ⌈n/b⌉. Let k = logb a. Then,
Case 2. If f (n) = Θ(nk ), then T (n) = Θ(nk log n).
Example
T (n) = 2T (n/2) + Θ(n).
a = 2, b = 2, f (n) = 17n (f (n) = 175n, etc.), k = log2 2 = 1.
T (n) = Θ(n log n).
T (n) = 9T (n/3) + n
Hence: a = 9, b = 3, f (n) = n, so
nlogb a = nlog3 9 = n2 = Θ(n2 ).
Since f (n) = O(n) = O(nlog3 9−ε ), where ε = 1, we can apply
Case 1, i.e.
T (n) = Θ(n2 ).
T (n) = T (2n/3) + 1
Here: a = 1, b = 3/2, f (n) = 1, and
nlogb a = nlog3/2 1 = n0 = 1.
So it is Case 2 as f (n) = Θ(1) = Θ(nlogb a ), i.e.
for c = 3/4.
Hence we can apply Case 3, i.e.
21
8
21
12
27
21
12 δ = min(12, 21)
28
δ
5
4
21
12 δ = min(12, 21)
3
29
δ
Ryszard Janicki Divide and Conquer 24/36
How to find closest pair with one 39
point jin each sid
How toLetfind
Def. s be closest
i
pair
the point in the 2with
δ-strip,one point
with the in each
i smallest th side?
y-coordinate. 31
Pf.
Def. Let si be the point in the 2 δ-strip, with the ith sma
・
Definition
No two points lie in same ½ δ-by-½ δ box.
・
Claim. If | i – j | ≥ 12, then the distance
LetTwo
si be
between
points
sthe
at least in
point 2 rows
the apart
2δ-strip, with 12, then the distance
i and sj is at least δ. Claim. If | i – j | 2≥rows
⋮ ½δ
(½ δ). ▪
thehave
i th distance
smallest≥y2-coordinate.
39 j
between si and sj is at least29δ. 30 ½δ
31
Pf.
½δ
Claim 28
・ i 27
No two points lie in same Pf.
½ δ-by-½ δ box.
Fact. Claim remains true if we replace 12 with 7.
If ・
|i −
Two ≥ 12, at
j| points then
leastthe ・
distance
2 rows Nobetween
apart two points lie in same ½ δ-by-½ δ box. ½δ
≥ 2δ.(½ δ). ▪ ・Two points at least 2 rows apart 25 30
26
si and sj distance
have is at least 2 rows
Proof.
i 27 ⋮ 28 ½δ
Fact.No tworemains
Claim points true we replace 12 with 7.
lie inif same i
1 1 δ replaceδ12 with 7.
Fact. Claim remains true if we 30
2 δ-by- 2 δ box. 26
25
Two points at least 2 rows apart
have distance ≥ 2( 21 δ).
⋮
δ δ 30
Compute separation line L such that half the points O(n log n)
are on each side of the line.
δ1 ← CLOSEST-PAIR (points in left half).
2 T(n / 2)
δ2 ← CLOSEST-PAIR (points in right half).
δ ← min { δ1 , δ2 }.
Delete all points further than δ from line L. O(n)
Theorem
The divide-and-conquer algorithm for finding the closest pair of
points in the plane can be implemented in O(n log2 n) time.
Proof.
Θ(1) if n = 1
T (n) =
T (⌈n/2⌉) + T (⌊n/2⌋) + O(n log n) otherwise
1 1 0 1 0 1 0 1
+ 0 1 1 1 1 1 0 1
1 0 1 0 1 0 0 1 0
IF (n = 1)
RETURN x 𐄂 y.
ELSE
m ← ⎡ n / 2 ⎤.
a ← ⎣ x / 2m⎦; b ← x mod 2m.
c ← ⎣ y / 2m⎦; d ← y mod 2m.
e ← MULTIPLY(a, c, m).
f ← MULTIPLY(b, d, m).
g ← MULTIPLY(b, c, m).
h ← MULTIPLY(a, d, m).
RETURN 22m e + 2m (g + h) + f.
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Proposition
The divide-and-conquer multiplication algorithm requires Θ(n2 ) bit
operations to multiply two n-bit integers.
Proof.
Apply case 1 of the master theorem to the recurrence:
ac +2m (|{z}
xy = 22m |{z} bd − (a − b)(c − d)) + |{z}
ac + |{z} bd
| {z }
1 1 2 3 2
IF (n = 1)
RETURN x 𐄂 y.
ELSE
m ← ⎡ n / 2 ⎤.
a ← ⎣ x / 2m⎦; b ← x mod 2m.
c ← ⎣ y / 2m⎦; d ← y mod 2m.
e ← KARATSUBA-MULTIPLY(a, c, m).
f ← KARATSUBA-MULTIPLY(b, d, m).
g ← KARATSUBA-MULTIPLY(a – b, c – d, m).
RETURN 22m e + 2m (e + f – g) + f.
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Proposition
Karatsuba’s multiplication algorithm requires Θ(n1.585 ) bit
operations to multiply two n-bit integers.
Proof.
Apply case 1 of the master theorem to the recurrence: