Professional Documents
Culture Documents
COMP3121/9101
2. DIVIDE-AND-CONQUER
COMP3121/3821/9101/9801 1 / 52
An old puzzle
Problem
We are given 27 coins of the same denomination; we know that one of
them is counterfeit and that it is lighter than the others. Find the
counterfeit coin by weighing coins on a pan balance only three times.
Hint
You can reduce the search space by a third in one weighing!
COMP3121/3821/9101/9801 2 / 52
An old puzzle
Solution
Divide the coins into three groups of nine, say A, B and C.
Weigh group A against group B.
If one group is lighter than the other, it contains the counterfeit
coin.
If instead both groups have equal weight, then group C contains the
counterfeit coin!
Repeat with three groups of three, then three groups of one.
COMP3121/3821/9101/9801 3 / 52
Divide and Conquer
COMP3121/3821/9101/9801 4 / 52
Counting the number of inversions
Assume that you have m users ranking the same set of n movies.
You want to determine for any two users A and B how similar
their tastes are (for example, in order to make a recommender
system).
For the ith movie on B 0 s list we can now look at the position (i.e.,
index) of that movie on A0 s list, denoted by a(i).
COMP3121/3821/9101/9801 5 / 52
Counting the number of inversions
A good measure of how different these two users are, is the total
number of inversions, i.e., total number of pairs of movies i, j such
that movie i precedes movie j on B 0 s list but movie j is higher up
on A0 s list than the movie i.
COMP3121/3821/9101/9801 6 / 52
Counting the number of inversions
B 1 2 3 4 5 6 7 8 9 10 11 12
A 1 5 9 12 7 10 3 4 6 8 2 11
a(9) = 3 a(4) = 8
a(7) = 5 a(2) = 11
COMP3121/3821/9101/9801 7 / 52
Counting the number of inversions
For example 1 and 2 do not form an inversion because a(1) < a(2)
(a(1) = 1 and a(2) = 11 because a(1) is on the first and a(2) is on
the 11th place in A);
However, for example 4 and 7 do form an inversion because
a(7) < a(4) (a(7) = 5 because seven is on the fifth place in A and
a(4) = 8)
COMP3121/3821/9101/9801 8 / 52
Counting the number of inversions
We now show that this can be done in a much more efficient way,
in time O(n log n), by applying a Divide-And-Conquer strategy.
COMP3121/3821/9101/9801 9 / 52
Counting the number of inversions
We split the array A into two (approximately) equal parts
Atop = A[1 . . . bn/2c] and Abottom = A[bn/2c + 1 . . . n].
Note that the total number of inversions in array A is equal to the
sum of the number of inversions I(Atop ) in Atop (such as 9 and 7)
plus the number of inversions I(Abottom ) in Abottom (such as 4 and
2) plus the number of inversions I(Atop , Abottom ) across the two
halves (such as 7 and 4).
1 9 7 3 4 6 8 2 5 A
Atop Abottom
COMP3121/3821/9101/9801 10 / 52
Counting the number of inversions
We now recursively sort arrays Atop and Abottom also obtaining in the process
the number of inversions I(Atop ) in the sub-array Atop and the number of
inversions I(Abottom ) in the sub-array Abottom .
We now merge the two sorted arrays Atop and Abottom while counting the
number of inversions I(Atop , Abottom ) which are across the two sub-arrays.
When the next smallest element among all elements in both arrays is an
element in Abottom , such an element clearly is in an inversion with all the
remaining elements in Atop and we add the total number of elements remaining
in Atop to the current value of the number of inversions across Atop and
Abottom .
1 11 9 12 7 10 3 4 6 8 2 5 A
a1=1 a9=3 a7=5 a4=8 a2=11
Atop Abottom
Atop
1 7 9 10 11 12
1 Abottom
2 33 4 5 6 8
COMP3121/3821/9101/9801 11 / 52
Counting the number of inversions
COMP3121/3821/9101/9801 12 / 52
Basics revisited: how do we add two numbers?
C C C C C carry
X X X X X first integer
+ X X X X X second integer
-----------
X X X X X X result
COMP3121/3821/9101/9801 13 / 52
Basics revisited: how do we multiply two numbers?
COMP3121/3821/9101/9801 15 / 52
1: function Mult(A, B)
2: if |A| = |B| = 1 then return AB
3: else
4: A1 ← MoreSignificantPart(A);
5: A0 ← LessSignificantPart(A);
6: B1 ← MoreSignificantPart(B);
7: B0 ← LessSignificantPart(B);
8: X ← Mult(A0 , B0 );
9: Y ← Mult(A0 , B1 );
10: Z ← Mult(A1 , B0 );
11: W ← Mult(A1 , B1 );
12: return W 2n + (Y + Z) 2n/2 + X
13: end if
14: end function
COMP3121/3821/9101/9801 16 / 52
How many steps does this algorithm take?
COMP3121/3821/9101/9801 17 / 52
Can we do multiplication faster than O(n2 )?
Claim: if T (n) satisfies
n
T (n) = 4T + cn (3)
2
then
T (n) = n2 (c + 1) − c n
T (n) = n2 (c + 1) − c n = Θ(n2 )
Take again our two input numbers A and B, and split them into two
halves:
n
A = A1 2 2 + A0 XX . . . X} XX
| {z | {z. . . X}
n n n
B = B1 2 2 + B0
2 2
• AB can now be calculated as follows:
n
AB = A1 B1 2n + (A1 B0 + A0 B1 )2 2 + A0 B0
n
= A1 B1 2n + ((A1 + A0 )(B1 + B0 ) − A1 B1 − A0 B0 )2 2 + A0 B0
COMP3121/3821/9101/9801 20 / 52
• Thus, the algorithm will look like this:
1: function Mult(A, B)
2: if |A| = |B| = 1 then return AB
3: else
4: A1 ← MoreSignificantPart(A);
5: A0 ← LessSignificantPart(A);
6: B1 ← MoreSignificantPart(B);
7: B0 ← LessSignificantPart(B);
8: U ← A0 + A1 ;
9: V ← B0 + B1 ;
10: X ← Mult(A0 , B0 );
11: W ← Mult(A1 , B1 );
12: Y ← Mult(U, V);
13: return W 2n + (Y − X − W ) 2n/2 + X
14: end if
15: end function
• How fast is this algorithm?
COMP3121/3821/9101/9801 21 / 52
How many multiplications does this take? (addition is in linear time!)
We need A1 B1 , A0 B0 and (A1 + A0 )(B1 + B0 ); thus
n
T (n) = 3 T + cn
2
COMP3121/3821/9101/9801 22 / 52
The Karatsuba trick
Clearly, the run time T (n) satisfies the recurrence
n
T (n) = 3 T + cn
2
and this implies (by replacing n with n/2)
n n n
T = 3T +c
2 22 2
and by replacing n with n/22
n n n
T = 3T +c
22 23 22
...
n n n
So we get T (n) = 3 T +c n = 3 3T
+ c + cn
2 22 2
| {z } | {z }
2 n 3n 2 n n 3n
=3 T +c + c n = 3 3T +c 2+c + cn
22 2 23 2 2
| {z } | {z }
32 n 32 n
3 n 3n 3 n n 3n
=3 T 3
+c 2 + c + c n = 3 3T 4
+c 3+c 2 +c + cn = ...
2 2 2 2 2 2 2
| {z } | {z }
COMP3121/3821/9101/9801 23 / 52
The Karatsuba trick
n n n n
T (n) = 3 T +c n = 3 3T 2
+c +c n = 32 T +c 3n
2 + cn
2 2 2 22
| {z } | {z } | {z }
2 n n 3
2
= 3 3T + c 2 + c 3n
2 + cn = 3 T
n
23
+ c 322n + c 3n
2 + cn
23 2
| {z }
n 2
= 33 T +c n 322 + 23 + 1
23
| {z }
3 n n 2
= 3 3T 4
+ c 3 + c n 322 + 32 + 1
2 2
| {z }
3 2
4
= 3 T 24 + c n 23 + 232 + 32 + 1
n 3
...
32
3 blog2 nc−1
= 3blog2 nc T n 3
+ cn + ... + 22
+ +1
b2log2 n c 2 2
3 log2 n −1
3 log2 n
≈ 3log2 n T (1) + c n 2
= 3log2 n T (1) + 2c n
3 −1 2 −1
2
COMP3121/3821/9101/9801 24 / 52
The Karatsuba trick
So we got
log2 n !
log2 n 3
T (n) ≈ 3 T (1) + 2c n −1
2
COMP3121/3821/9101/9801 25 / 52
Some Important Questions:
2
n n 3n
3
n 32 n 3n
= 3 3T +c +c + cn = 3 T +c +c + cn
23 22 2 23 22 2
n 3 2 3
3
= 3 T + cn + + 1 =
23 22 2
3
n n 32 3
= 3 3T +c + cn + + 1 =
24 23 22 2
n 3 3 32 3
4
= 3 T + cn + + + 1 =
24 23 22 2
...
3 blog
2 nc−1 32
!
blog2 nc n 3
= 3 T + cn + ... + + + 1
b2log2 n c 2 22 2
3 log2 n − 1
log2 n 2
≈ 3 T (1) + cn
3 −1
2
3 log n !
log2 n 2
= 3 T (1) + 2cn −1
2
COMP3121/3821/9101/9801 26 / 52
Recurrences
size of instance = n
COMP3121/3821/9101/9801 29 / 52
Some recurrences can be solved explicitly, but this tends to be
tricky.
COMP3121/3821/9101/9801 30 / 52
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f (n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f (n) = O(nlogb a−ε ) for some ε > 0, then T (n) = Θ(nlogb a );
2 If f (n) = Θ(nlogb a ), then T (n) = Θ(nlogb a log2 n);
3 If f (n) = Ω(nlogb a+ε ) for some ε > 0, and for some c < 1 and some n0 ,
a f (n/b) ≤ c f (n)
(But often the proof of the Master Theorem can be tweaked to obtain
the asymptotic of the solution T (n) in such a case when the Master
Theorem does not apply; an example is T (n) = 2T (n/2) + n log n).
COMP3121/3821/9101/9801 31 / 52
Master Theorem - a remark
logb n = c log2 n;
1
log2 n = logb n;
c
Thus, logb n = Θ(log2 n) and also log2 n = Θ(logb n).
COMP3121/3821/9101/9801 33 / 52
Master Theorem - Examples
COMP3121/3821/9101/9801 34 / 52
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n;
then nlogb a = nlog4 3 < n0.8 ;
thus f (n) = n = Ω(n0.8+ε ) for any ε < 0.2.
Also, af (n/b) = 3f (n/4)= 3/4 n < c n = cf (n) for c = .9 < 1.
and so on . . ., we get
(1)
z }|
{
n n n
T (n) = a T +f (n) = a a T 2
+f + f (n)
b b b
| {z } | {z }
(2L) (2R)
2 n n n
n
+ f (n) = a2 n
=a T 2
+a f b aT +f + af b + f (n)
b b3 b2
| {z } | {z }
(3L) (3R)
3 n
+a2 f bn2 + a f n
=a T 3 b + f (n) = . . .
b
| {z }
COMP3121/3821/9101/9801 36 / 52
Master Theorem Proof: (optional material)
Continuing in this way logb n − 1 many times we get ...
n n n
T (n) = a3 T 3 +a2 f 2 + a f + f (n) =
| {zb } b b
= ...
n n
= ablogb nc T blog nc + ablogb nc−1 f blog nc−1 + . . .
b b n b n
b
3 n 2
+ a f 3 + a f 2 + af + f (n)
b b b
blogb nc−1
logb n
n X n
≈a T + ai f
blogb n i=0
bi
COMP3121/3821/9101/9801 38 / 52
Master Theorem Proof: (optional material)
Case 1 - continued:
blogb nc−1
!
ε blogb nc
X n
logb a−ε (b ) −1
ai f i = O n
i=0
b bε − 1
ε
bblogb nc −1
logb a−ε
= O n
bε − 1
nε − 1
= O nlogb a−ε ε
b −1
log a
n b − nlogb a−ε
=O
bε − 1
= O nlogb a
blogb nc−1 n
X
Since we had: T (n) ≈ nlogb a T (1) + ai f we get:
i=0
bi
T (n) ≈ nlogb a T (1) + O nlogb a
= Θ nlogb a
COMP3121/3821/9101/9801 39 / 52
Master Theorem Proof: (optional material)
Case 2: f (m) = Θ(mlogb a )
blogb nc−1 n blogb nc−1 n logb a
X X
ai f = ai Θ
i=0
bi i=0
bi
blogb nc−1 n logb a
X i
= Θ a
i=0
bi
blogb nc−1 i
X a
= Θ nlogb a
i=0
(bi )logb a
blogb nc−1
X a i
= Θ nlogb a
i=0
blogb a
blogb nc−1
X
= Θ nlogb a 1
i=0
= Θ nlogb a blogb nc
COMP3121/3821/9101/9801 40 / 52
Master Theorem Proof: (optional material)
Case 2 (continued):
Thus,
we get:
T (n) ≈ nlogb a T (1) + Θ nlogb a log2 n
= Θ nlogb a log2 n
COMP3121/3821/9101/9801 41 / 52
Master Theorem Proof: (optional material)
Case 3: f (m) = Ω(mlogb a+ε ) and a f (n/b) ≤ c f (n) for some 0 < c < 1.
c
We get by substitution: f (n/b) ≤ f (n)
a
c
f (n/b2 ) ≤ f (n/b)
a
c
f (n/b3 ) ≤ f (n/b2 )
a
...
c
f (n/bi ) ≤ f (n/bi−1 )
a
By chaining these inequalities we get
c c c c2
f (n/b2 ) ≤ f (n/b) ≤ · f (n) = 2 f (n)
a | {z } a |a {z } a
c c c2 c3
f (n/b3 ) ≤ f (n/b2 ) ≤ · 2 f (n) = 3 f (n)
a | {z } a |a {z } a
...
c c ci−1 ci
f (n/bi ) ≤ f (n/bi−1 ) ≤ · i−1 f (n) = i f (n)
a | {z } a |a {z } a
COMP3121/3821/9101/9801 42 / 52
Master Theorem Proof: (optional material)
Case 3 (continued):
ci
We got f (n/bi ) ≤ f (n)
ai
Thus,
blogb nc−1 blogb nc−1 ∞
X n X ci X f (n)
ai f ≤ ai f (n) < f (n) ci =
i=0
bi i=0
ai i=0
1 −c
Example: Let us estimate the asymptotic growth rate of T (n) which satisfies
Note: we have seen that the Master Theorem does NOT apply, but the technique
used in its proof still works! So let us just unwind the recurrence and sum up the
logarithmic overheads.
COMP3121/3821/9101/9801 44 / 52
n
T (n) = 2 T +n log n
| {z2 }
z }| {
n n n
= 2 2T + log + n log n
22 2 2
n
= 22 T 2
+n log n2 + n log n
2
| {z }
z }| {
2 n n n
=2 2T + 2 log 2 + n log n2 + n log n
23 2 2
n
= 23 T 3
+n log 2n2 + n log n2 + n log n
| {z2 }
...
= 2log n T 2log n
+ n log 2lognn−1 + ... + n log 2n2 + n log n
n 2
+ n log n
= Θ n(log n)2 .
COMP3121/3821/9101/9801 45 / 52
Deterministic Linear Time Algorithm for Order Statistic
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
Take the collection of all b n5 c middle elements of each group (i.e.,
the medians of each group of five).
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
COMP3121/3821/9101/9801 48 / 52
Deterministic Linear Time Algorithm for Order Statistic
What have we accomplished by such a choice of the pivot?
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
smaller than the pivot larger than the pivot smaller or equal to the pivot larger or equal to the pivot
Let us show that T (n) < 11Cn for all n. Assume that this is true
for all k < n and let us prove it is true for n as well.
Note: this is a proof using the following form of induction:
ϕ(0) & (∀n)((∀k < n)ϕ(k) → ϕ(n)) → (∀n)ϕ(n).
Thus, assume T (n/5) < 11C · n/5 and T (7n/10) < 11C · 7n/10;
then
n 7n
T (n) ≤ T (n/5) + T (7n/10) + Cn < 11C · + 11C · + Cn
5 10
Cn
= 109 < 11C · n
10
Hint: Try proving it by induction. Find a way for reducing the case
with n + 1 petrol stations to the case with only n petrol stations by
choosing suitably a petrol station to remove, pouring its petrol to the
preceding petrol station.
COMP3121/3821/9101/9801 51 / 52
That’s All, Folks!!
COMP3121/3821/9101/9801 52 / 52