CS-E3190 Lect03 PDF
CS-E3190 Lect03 PDF
CS-E3190
Pekka Orponen
Department of Computer Science
Aalto University
Autumn 2017
Lecture 3: Basic arithmetic; divide-and-conquer I
CE-3190 PAT
Autumn 2017
2/27
3.1 Basic arithmetic: bases and logs
CE-3190 PAT
Autumn 2017
3/27
Logs in algorithms
1
This is actually closer to ln N, but within a constant factor anyway.
CE-3190 PAT
Autumn 2017
4/27
Basic arithmetic: addition
Lemma The sum of any three single-digit numbers is at most
two digits long.
Proof. For all bases b with b 2, we have
3(b 1) (b + 1)(b 1) = (b 1)b + (b 1), which proves
the claim.
Binary addition with carry:
Carry: 1 1 1 1
1 1 0 1 0 1 (53)
1 0 0 0 1 1 (35)
1 0 1 1 0 0 0 (88)
CE-3190 PAT
Autumn 2017
5/27
Complexity of the algorithm
CE-3190 PAT
Autumn 2017
6/27
Basic arithmetic: grade-school multiplication
1 1 0 1 (13)
1 0 1 1 (11)
Carry: 1 1 1 1
1 1 0 1 1x(1 times 1101)
1 1 0 1 1x(10 times 1101)
0 0 0 0 0x(100 times 1101)
+ 1 1 0 1 1x(1000 times 1101)
1 0 0 0 1 1 1 1 (binary 143)
CE-3190 PAT
Autumn 2017
7/27
Complexity of the algorithm
CE-3190 PAT
Autumn 2017
8/27
Al Khwarizmis strikeout algorithm
y x
11 13
5 26 (halve y , double x)
2 52 (strike out rows with even y )
1 104
143 (answer)
Same as binary multiplication! (Note: halve shift right, double
shift left)
How long does this computation take?
I n recursive calls (= rows), since y is n bits long
I at each call: one halving, remainder even or odd, doubling,
and possibly one addition of integers of length n, for a total
of O(n) bit operations
I A total of O(n2 ) bit operations
Can we do better?
CE-3190 PAT
Autumn 2017
9/27
Multiplication la Franaise (1/2)
2(x b y2 c) if y is even
x y =
x + 2(x b y2 c) if y is odd
13 11 = 13 + 2 (13 5)
= 13 + 26 5
= 13 + 26 + 2 (26 2)
= 13 + 26 + 2 52
= 13 + 26 + 104
CE-3190 PAT
Autumn 2017
10/27
Multiplication la Franaise (2/2)
Algorithm 1: Multiplication la Franaise
1 function multiply (x, y );
Input: Two n-bit integers x and y , where y 0
Output: The product x y
2 if y = 0 then
3 return 0
4 else
5 set z = multiply(x, b y2 c)
6 end
7 if y is even then
8 return 2z
9 else
10 return x + 2z
11 end
Complexity again O(n2 ).
CE-3190 PAT
Autumn 2017
11/27
Division
Algorithm 2: Division
1 function divide (x, y );
Input: Two n-bit integers x and y , where y 1
Output: The quotient and remainder of x divided by y
2 if x = 0 then
3 return (q, r ) = (0, 0)
4 else
5 set (q, r ) = divide(b x2 c, y );
6 q = 2 q, r = 2 r ;
7 if x is odd then
8 r =r +1
9 end
10 if r y then
11 r = r y, q = q + 1
12 end
13 return (q, r )
14 end
CE-3190 PAT
Autumn 2017
12/27
3.2 The divide-and-conquer paradigm
Recall the merge sort algorithm: dramatic improvement to
algorithm complexity (from O(n2 ) by O(n log n)) by recursive
partitioning:
CE-3190 PAT
Autumn 2017
13/27
Large integer multiplication
I Let us try to apply the partitioning idea to the problem of
multiplying two n-bit integers X and Y .
I Partition each of X and Y into two n/2-bit numbers and
perform multiplication first recursively between those.
(Assume for simplicity n = 2k , k = 0, 1, 2, . . . .)
I More precisely, partition as follows:
n
X = A 22 + B X : A B
n
Y = C 22 + D Y : C D
| {z } | {z }
n n
2 2
CE-3190 PAT
Autumn 2017
14/27
I Denote: T (n) = number of elementary bit operations
needed to multiply two n-bit numbers following this rule.
I Computation involves four multiplications of n2 -bit numbers,
by recursion, and three additions of n-bit numbers, which
can each be done in O(n) time.
I We thus obtain the following recurrence equation for T (n):
(
T (1) = c1
T (n) = 4T ( n2 ) + c2 n, n = 2k , k = 0, 1, 2, . . .
CE-3190 PAT
Autumn 2017
15/27
I Karatsuba & Ofman (1962): product XY can be obtained
from A, B, C, D with just three multiplications instead of
four:
n
XY = AC 2n + [(A B)(D C) + AC + BD] 2 2 + BD
CE-3190 PAT
Autumn 2017
16/27
Let us solve the equations by naive unwinding:
T (n) = 3T ( n2 ) + c2 n
= 3(3T ( n4 ) + c2 n2 ) + c2 n
= 32 T ( 2n2 ) + c2 n( 32 + 1)
...
= 3i T ( 2ni ) + c2 n(( 23 )i1 + ( 32 )i2 + + 1)
...
= 3k T ( 2nk ) + c2 n(( 23 )k 1 + ( 32 )k 2 + + 1)
( 23 )k 1
= 3k c1 + c2 n ( 23 1)
= 3log2 n c1 + 2c2 n(( 23 )log2 n 1)
= nlog2 3 c1 + 2c2 (nlog2 3 n)
= (c1 + 2c2 )nlog2 3 2c2 n
Thus the repeated application of the Karatsuba-Ofman trick at
all levels of the recursion leads to an O(nlog2 3 ) = O(n1.59... )
algorithm. This is an order-of-growth improvement over the
simple O(n2 ) method.
CE-3190 PAT
Autumn 2017
17/27
Additional notes
1. Because of the constant factors involved, the grade school
method is actually more efficient than the Karatsuba-Ofman
algorithm up to about 500-bit numbers. On the other hand,
numbers of this size and bigger are being used routinely in e.g.
cryptographic applications.
2. In principle, the multiplication of very large numbers can be done
in time O(n log2 n log2 log2 n) by an algorithm of Schnhage and
Strassen, applying Fourier transform techniques. It is an open
problem whether multiplication can be done in time O(n).
3. A note on implementation: in practice the partitioning of numbers
in the Karatsuba-Ofman technique should of course not be
continued all the way to single bit numbers, but only to the level
of the wordlength of the underlying hardware. At this point the
multiplications can be completed by basic machine operations.
CE-3190 PAT
Autumn 2017
18/27
Divide-and-conquer, or problem partitioning
CE-3190 PAT
Autumn 2017
19/27
A master theorem for recurrences
The solution T (n) to recurrence equation
(
T (1) = c1 > 0,
T (n) = aT ( bn ) + cnd , for n = bk , k = 1, 2, . . .
satisfies:
T (n) = O(nd ), if d > logb a and c 6= 0,
T (n) = O(nd log n), if d = logb a and c 6= 0,
T (n) = O(nlogb a ), if d < logb a or c = 0.
CE-3190 PAT
Autumn 2017
20/27
Solving recurrences
Master theorem (weak variant)
Suppose that a > 0, b > 1, C 0, d 0 are
constants such that
T (1) C
T (n) aT (n/b) + Cnd , n = bj , j = 1, 2, . . .
Then,
d
O(n ) if d > logb a;
T (n) = O(n log n) if d = logb a;
d
O(nlogb a if d < logb a.
CE-3190 PAT
Autumn 2017
21/27
Proof (1/2) Consider the recursion tree and
the time used on each node
Take the sum of times
& simplify
T (n) aT (n/b) + Cnd
T (n)
Cnd
CE-3190 PAT
Autumn 2017
22/27
Proof (2/2)
Level (j) Size (n) #Nodes/level Time/node
0 n 1 Cnd
1 nb 1 a Cnd b d
2 nb 2 a2 Cnd b 2d
.. .. .. .. .. .. ..
. . . . . . .
j
j nb aj Cnd b jd ...
.. .. .. .. .. .. .. ..
. . . . . . . .
logb n 1 alogb n C ...
d
O(n )
logb n if bd > a;
j
T (n) Cnd a/bd = O(nd log n) if bd = a;
j=0 O(nlogb a if bd < a.
CE-3190 PAT
Autumn 2017
23/27
Matrix multiplication
CE-3190 PAT
Autumn 2017
24/27
Strassens algorithm (1/2)
Idea: The product of two 2 2 matrices can be computed with
just seven multiplications instead of the obvious eight.
Assume for simplicity that n = 2k for some k 1 and denote:
A11 A12 } n2
A n
A22 } 2 B11 B 12
A = |{z}21
|{z} B=
n n
B21 B22
2 2
CE-3190 PAT
Autumn 2017
26/27
Additional notes
CE-3190 PAT
Autumn 2017
27/27