SlideShare a Scribd company logo
Revisited:
How To Multiply Two Numbers
2 X 2 = 5
Time complexity of
addition
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
+
*
*
*
*
*
*
T(n) = The amount of
time addition uses to
add two n-bit
numbers
We saw that T(n) was linear.
T(n) = Θ(n)
Time complexity of
school multiplication
T(n) = The amount of
time grade school
multiplication uses to
add two n-bit
numbers
We saw that T(n) was quadratic.
T(n) = Θ(n2
)
X
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
n2
addition
Addition is
linear time.
Is there a sub-linear time
method for addition?
Any addition algorithm takes Ω(n) time
Claim: Any algorithm for addition must read
all of the input bits
Proof: Suppose there is a mystery algorithm A
that does not examine each bit
Give A a pair of numbers. There must be some
unexamined bit position i in one of the
numbers
• If A is not correct on the inputs, we found a bug
• If A is correct, flip the bit at position i and give
A the new pair of numbers. A gives the same
answer as before, which is now wrong.
Any addition algorithm takes Ω(n) time
* * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
A did not
read this bit
at position i
Addition can’t be
improved upon by
more than a
constant factor.
Multiplication
Multiplication: Θ(n2
) time
Is there a clever algorithm to
multiply two numbers in linear
time?
Repetitive addition
int mult( int n, int m)
{
if( m==0 ) return 0;
return mult(n, m-1) + n;
}
What is the complexity of
this algorithm?
Peasant multiplication
x
1011
1001
------
1011
0000
0000
1011
-----------
1100011
Peasant multiplication
Why is it called the “Russian”
peasant’s algorithm?
Peasant multiplication
It’s also called the Egyptian
multiplication.
But if the number does not
fit the CPU?
Can we do something very
different than grade school
multiplication?
Divide And Conquer
An approach to faster algorithms:
1. DIVIDE a problem into smaller
subproblems
2. CONQUER them recursively
3. GLUE the answers together so as to obtain
the answer to the larger problem
Multiplication of 2 n-bit numbers
X =
Y =
a b
c d
X = a 2n/2
+ b Y = c 2n/2
+ d
n/2 bits
n/2 bits
n bits
X × Y = ac 2n
+ (ad + bc) 2n/2
+ bd
X
Y
Same thing for decimals
X =
Y =
a b
c d
X = a 10n/2
+ b Y = c 10n/2
+ d
n/2 digits
n/2 digits
n digits
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
Multiplying (Divide & Conquer style)
12345678 * 21394276
*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hence: 12*21 = 2*102
+ (1 + 4)101
+ 2 = 252
2 1 4 2
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hence: 12*21 = 2*102
+ (1 + 4)101
+ 2 = 252
2 1 4 2
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hence: 12*21 = 2*102
+ (1 + 4)101
+ 2 = 252
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1*3 1*9 2*3 2*9
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
242 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 9 6 18
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
*102
+ *101
+ *101
+ *1
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
242 468 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 9 6 18
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
*102
+ *101
+ *101
+ *1
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
252 468 714 1326
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
252 468 714 1326
*104
+ *102
+ *102
+ *1
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
1234*2139 1234*4276 5678*2139 5678*4276
12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
= 2639526
252 468 714 1326
*104
+ *102
+ *102
+ *1
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
2639526 1234*4276 5678*2139 5678*4276
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
2639526 5276584 12145242 24279128
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
2639526 5276584 12145242 24279128
*108
+ *104
+ *104
+ *1
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Multiplying (Divide & Conquer style)
12345678 * 21394276
2639526 5276584 12145242 24279128
*108
+ *104
+ *104
+ *1
= 264126842539128
X =
Y =
X × Y = ac 10n
+ (ad + bc) 10n/2
+ bd
a b
c d
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(a,c) Mult(a,d) Mult(b,c) Mult(b,d)
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(a,c)
Mult(a,d) Mult(b,c) Mult(b,d)
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(a,d) Mult(b,c) Mult(b,d)
ac
ac
MULT(X,Y):
X=a;b Y=c;d
Mult(a,d)
Mult(b,c) Mult(b,d)
ac
ac,
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(b,c) Mult(b,d)
ac
ad
ac, ad,
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(b,c)
Mult(b,d)
ac
ad
ac, ad,
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(b,d)
ac
ad bc
ac, ad,bc,
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
Mult(b,d)
ac
ad bc
ac, ad,bc,
Divide, Conquer, and Glue
Divide, Conquer, and Glue
MULT(X,Y):
X=a;b Y=c;d
ac
ad bc
bd
ac, ad,bc, bd
XY = ac2n
+(ad+bc)
2n/2
+ bd
Time required by MULT
T(n) = time taken by MULT on two n-bit
numbers
What is T(n)? What is its growth rate?
Big Question: Is it Θ(n2
)?
X=a;b Y=c;d
ac
ad bc
bd
XY = ac2n
+ (ad + bc)2n/2
+ bd
T(n/2) T(n/2) T(n/2) T(n/2)
Time required by MULT
X=a; b Y=c; d
ac
ad bc
bd
XY = ac2n
+ (ad + bc)2n/2
+ bd
T(n/2) T(n/2) T(n/2) T(n/2)
T(n) = 4 T(n/2) + (c1 n + c2)
Conquering
time
divide and
glue
Recurrence Relation
T(1) = c3
T(n) = 4 T(n/2) + c1 n + c2
What is the growth rate of T(n)?
Let’s keep it simple
T(1) = 1
T(n) = 4 T(n/2) + n
What is the growth rate of T(n)?
Guess: T(n) = 2n2
– n
Verify: T(1) = 1 and T(n) = 4 T(n/2) + n
2n2
– n = 4 [2(n/2)2
– n/2] + n
= 2n2
– 2n + n
= 2n2
– n
Technique:
Guess and Verify
Recursion Tree Representation
We visualize the recursion
as a tree, where each node
represents a recursive call.
The root is the initial call.
Leaves correspond to the
exit condition.
T(n) = 4 T(n/2) + n
T(n/2) T(n/2) T(n/2) T(n/2)
T(n)
T(n/4) T(n/4) T(n/4) T(n/4)
Recursion Tree
n/2 n/2 n/2 n/2
n
n/4 n/4 n/4 n/4
each node
reflects the
combining
step
Recursion Tree
n/2 n/2 n/2 n/2
n
n/4 n/4 n/4 n/4
n
2n
4n
write down
the work at
each level
= 1*n
= 2*n
= 4*n
= 2*i
n
= n*n
T(n)=n (1+2+4+8+ . . . +2h
), where h = log2n
T(n) = n(2n-1) = Θ(n2
)
Leve l i is th e sum of 4 i copies of n/2 i
1+ 1 + 1 + 1+ 1 + 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1 + 1 + 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1 + 1+ 1+ 1+ 1
. . . . . . . . . . . . . . . . . . . . . . . . . .
n/2 + n/2 + n/2 + n/2
n
n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/ 4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4
0
1
2
i
log n
2
1 + X
1 + X1
1
+ X
+ X2
2
+ X
+ X3
3
+ … + X
+ … + Xn-1
n-1
+ X
+ Xn
n
=
= X
Xn+1
n+1
- 1
- 1
X
X - 1
- 1
The Geometric Series
T(n)=n (1+2+4+8+ . . . +2h
), where h = log2n
T(n) = n(2n-1) = Θ(n2
)
Divide and Conquer MULT: Θ(n2
) time
Multiplication: Θ(n2
) time
All that work
for nothing!
Karatsuba, Anatolii Alexeevich (1937-)
In 1962 Karatsuba
had formulated the
first algorithm to
break n2
barrier!
Multiplication of 2 n-bit numbers
X = a 2n/2
+ b Y = c 2n/2
+ d
X × Y = ac 2n
+ (ad + bc) 2n/2
+ bd
z1 = (a + b) (c + d) = ac + ad + bc + bd
z2 = ac
z3 = bd
z4 = z2 – z3 = ac - bd
z5 = z1 – z2 – z3 = ad + bc
X × Y = z2 2n
+ z5 2n/2
+ z3
Karatsuba (1962)
T(1) = 1
T(n) = 3 T(n/2) + n
What is the growth rate of T(n)?
n
T(n/2) T(n/2) T(n/2)
T(n) =
n
T(n/2) T(n/2)
T(n) =
n/2
T(n/4)
T(n/4)
T(n/4)
n
T(n) =
n/2
T(n/4)
T(n/4)
T(n/4)
n/2
T(n/4)
T(n/4)
T(n/4)
n/2
T(n/4)
T(n/4)
T(n/4)
n
n/2 + n/2 + n/2
Level i is the sum of 3i
copies of n/2i
. . . . . . . . . . . . . . . . . . . . . . . . . .
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4
0
1
2
i
log n
2
= 1n
= 3/2n
= 9/4n
= (3/2)i
n
T(n) = n (1+3/2+(3/2)2
+ . . . + (3/2)log2 n
) = ??
Leve l i is th e sum of 3 i copies of n/2 i
1+ 1 + 1 + 1+ 1 + 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1 + 1 + 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1 + 1+ 1+ 1+ 1
. . . . . . . . . . . . . . . . . . . . . . . . . .
n/2 + n/2 + n/2
n
n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4
0
1
2
i
log n
2 = (3/2)log n
n
The Geometric Series
1 + X
1 + X1
1
+ X
+ X2
2
+ X
+ X3
3
+ … + X
+ … + Xk-1
k-1
+ X
+ Xk
k
=
= X
Xk+1
k+1
- 1
- 1
X
X - 1
- 1
We have: X = 3/2 k = log2 n
(3/2) × (3/2)log2 n
- 1
½
3 × (3log2 n
/2log2 n
) - 2
=
3 × (3log2 n
/n) - 2
=
Dramatic improvement for large n
T(n) = 3 nlog2 3
– 2n = Θ(n log2 3
) = Θ(n1.58…
)
A huge savings over Θ(n2
) when n gets large.
Recursion Tree Representation
f(n/b) f(n/b) f(n/b) f(n/b)
f(n)
f(n/b2
) f(n/b2
) f(n/b2
) f(n/b2
)
T(n) = a T(n/b) + f(n)
a nodes
Solve the following
recurrence
T(n) = p T(n/3) + n
where p>0.
Exercise
T(n) = p T(n/3) + n
Let p>3 be an arbitrary parameter.
Using the tree method, we obtain
T(n) = n(1 + p/3 + p2
/9 + …+ (p/3)h
)
where the tree height is log3n.
It follows
T(n) = Θ(nlog
3
p
)
Recurrence
T(n) = p T(n/3) + n
has the following
has the following
asymptotic solution
asymptotic solution
T(n) =
T(n) = Θ (n
n log
log
3
3 p
p
)
)
when p>3.
when p>3.
T(n) = p T(n/3) + n
Cases p =1, 2 and 3 should
Cases p =1, 2 and 3 should
be considered separately
be considered separately
If p =3, then
If p =3, then
T(n) =
T(n) = Θ (n log n)
n log n)
3-Way Multiplication
X = a1 2n/4
+ a2 2n/2
+ a3
Y = b1 2n/4
+ b2 2n/2
+ b3
T(1) = 1
T(n) = 9 T(n/3) + n
3-Way Multiplication
T(n) = p T(n/3) + n
T(n) = Θ(nlog
3
p
)
To get an improvement over
Karatsuba’s, we have to decrease
the number of multiplications to
at least 5.
Toom and Cook (1963, 1966)
T(1) = 1
T(n) = 5 T(n/3) + n
T(n) = Θ(n log3 5
) = Θ(n1.46…
)
3-Way Multiplication
X = a1 2n/4
+ a2 2n/2
+ a3
Y = b1 2n/4
+ b2 2n/2
+ b3
Is it possible to reduce the number of
multiplications to 5?
Multiplication Algorithms
Grade School n2
Karatsuba n1.58…
3-way n1.46…
FFT n log2
n
Schönhage and Strassen n logn loglogn

More Related Content

PDF
Solution Manual : Chapter - 05 Integration
PDF
Question bank Engineering Mathematics- ii
DOCX
Maths 301 key_sem_1_2007_2008
PDF
Mathematical Modelling of Electro-Mechanical System in Matlab
PDF
Mock cat solutions paper no 1
PDF
Sample question paper 2 with solution
PPTX
EPCA_MODULE-2.pptx
PPT
2014 st josephs geelong spec maths
Solution Manual : Chapter - 05 Integration
Question bank Engineering Mathematics- ii
Maths 301 key_sem_1_2007_2008
Mathematical Modelling of Electro-Mechanical System in Matlab
Mock cat solutions paper no 1
Sample question paper 2 with solution
EPCA_MODULE-2.pptx
2014 st josephs geelong spec maths

Similar to L6_Large_number_multi.ppt large number multiplication (20)

PDF
1 to hop
PDF
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
PDF
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
PDF
Solutions manual for calculus an applied approach brief international metric ...
PDF
Factoring perfect-square-trinomials
PPTX
perfect square trinomial
PDF
Integration techniques
PDF
Calculo purcell 9 ed solucionario
PDF
51541 0131469657 ism-0
PDF
Calculus Early Transcendentals 10th Edition Anton Solutions Manual
PPTX
Strategic Intervention Materials
PDF
Integer_Functions .pdf
PDF
Week 8- Integration (Intro) Part 7 Summary.pdf
PDF
Week 8- Integration (Intro) Part 7 Summary.pdf
PPTX
Q1-W1-Factoring Polynomials.pptx
PPT
Factoring 15.3 and 15.4 Grouping and Trial and Error
PPTX
sim-140907230908-phpapp01.pptx
DOCX
Sim math 9 factoring
DOCX
Question 1 1. Evaluate using integration by parts. l.docx
PDF
Cuaderno+de+integrales
1 to hop
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions manual for calculus an applied approach brief international metric ...
Factoring perfect-square-trinomials
perfect square trinomial
Integration techniques
Calculo purcell 9 ed solucionario
51541 0131469657 ism-0
Calculus Early Transcendentals 10th Edition Anton Solutions Manual
Strategic Intervention Materials
Integer_Functions .pdf
Week 8- Integration (Intro) Part 7 Summary.pdf
Week 8- Integration (Intro) Part 7 Summary.pdf
Q1-W1-Factoring Polynomials.pptx
Factoring 15.3 and 15.4 Grouping and Trial and Error
sim-140907230908-phpapp01.pptx
Sim math 9 factoring
Question 1 1. Evaluate using integration by parts. l.docx
Cuaderno+de+integrales
Ad

Recently uploaded (20)

PPTX
Current and future trends in Computer Vision.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Artificial Intelligence
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Project quality management in manufacturing
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Construction Project Organization Group 2.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Current and future trends in Computer Vision.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Artificial Intelligence
Fundamentals of safety and accident prevention -final (1).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Project quality management in manufacturing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
Construction Project Organization Group 2.pptx
additive manufacturing of ss316l using mig welding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CH1 Production IntroductoryConcepts.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

L6_Large_number_multi.ppt large number multiplication

  • 1. Revisited: How To Multiply Two Numbers 2 X 2 = 5
  • 2. Time complexity of addition * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * * * * * T(n) = The amount of time addition uses to add two n-bit numbers We saw that T(n) was linear. T(n) = Θ(n)
  • 3. Time complexity of school multiplication T(n) = The amount of time grade school multiplication uses to add two n-bit numbers We saw that T(n) was quadratic. T(n) = Θ(n2 ) X * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * n2
  • 4. addition Addition is linear time. Is there a sub-linear time method for addition?
  • 5. Any addition algorithm takes Ω(n) time Claim: Any algorithm for addition must read all of the input bits Proof: Suppose there is a mystery algorithm A that does not examine each bit Give A a pair of numbers. There must be some unexamined bit position i in one of the numbers
  • 6. • If A is not correct on the inputs, we found a bug • If A is correct, flip the bit at position i and give A the new pair of numbers. A gives the same answer as before, which is now wrong. Any addition algorithm takes Ω(n) time * * * * * * * * * * * * * * * * * * * * * * * * * * * * A did not read this bit at position i
  • 7. Addition can’t be improved upon by more than a constant factor.
  • 8. Multiplication Multiplication: Θ(n2 ) time Is there a clever algorithm to multiply two numbers in linear time?
  • 9. Repetitive addition int mult( int n, int m) { if( m==0 ) return 0; return mult(n, m-1) + n; } What is the complexity of this algorithm?
  • 11. Peasant multiplication Why is it called the “Russian” peasant’s algorithm?
  • 12. Peasant multiplication It’s also called the Egyptian multiplication.
  • 13. But if the number does not fit the CPU? Can we do something very different than grade school multiplication?
  • 14. Divide And Conquer An approach to faster algorithms: 1. DIVIDE a problem into smaller subproblems 2. CONQUER them recursively 3. GLUE the answers together so as to obtain the answer to the larger problem
  • 15. Multiplication of 2 n-bit numbers X = Y = a b c d X = a 2n/2 + b Y = c 2n/2 + d n/2 bits n/2 bits n bits X × Y = ac 2n + (ad + bc) 2n/2 + bd X Y
  • 16. Same thing for decimals X = Y = a b c d X = a 10n/2 + b Y = c 10n/2 + d n/2 digits n/2 digits n digits X × Y = ac 10n + (ad + bc) 10n/2 + bd
  • 17. Multiplying (Divide & Conquer style) 12345678 * 21394276 *4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 18. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 19. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 20. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 21. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 22. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 23. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 24. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 25. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 26. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 27. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 28. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 29. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 2 1 4 2 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 30. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 2 1 4 2 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 31. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 252 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 32. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 252 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1*3 1*9 2*3 2*9 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 33. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 242 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 9 6 18 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d *102 + *101 + *101 + *1
  • 34. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 242 468 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 9 6 18 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d *102 + *101 + *101 + *1
  • 35. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 252 468 714 1326 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 36. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 252 468 714 1326 *104 + *102 + *102 + *1 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 37. Multiplying (Divide & Conquer style) 12345678 * 21394276 1234*2139 1234*4276 5678*2139 5678*4276 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx = 2639526 252 468 714 1326 *104 + *102 + *102 + *1 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 38. Multiplying (Divide & Conquer style) 12345678 * 21394276 2639526 1234*4276 5678*2139 5678*4276 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 39. Multiplying (Divide & Conquer style) 12345678 * 21394276 2639526 5276584 12145242 24279128 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 40. Multiplying (Divide & Conquer style) 12345678 * 21394276 2639526 5276584 12145242 24279128 *108 + *104 + *104 + *1 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 41. Multiplying (Divide & Conquer style) 12345678 * 21394276 2639526 5276584 12145242 24279128 *108 + *104 + *104 + *1 = 264126842539128 X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a b c d
  • 42. Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d
  • 43. Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult(a,c) Mult(a,d) Mult(b,c) Mult(b,d)
  • 44. Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult(a,c) Mult(a,d) Mult(b,c) Mult(b,d)
  • 45. Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult(a,d) Mult(b,c) Mult(b,d) ac ac
  • 49. MULT(X,Y): X=a;b Y=c;d Mult(b,d) ac ad bc ac, ad,bc, Divide, Conquer, and Glue
  • 50. MULT(X,Y): X=a;b Y=c;d Mult(b,d) ac ad bc ac, ad,bc, Divide, Conquer, and Glue
  • 51. Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d ac ad bc bd ac, ad,bc, bd XY = ac2n +(ad+bc) 2n/2 + bd
  • 52. Time required by MULT T(n) = time taken by MULT on two n-bit numbers What is T(n)? What is its growth rate? Big Question: Is it Θ(n2 )?
  • 53. X=a;b Y=c;d ac ad bc bd XY = ac2n + (ad + bc)2n/2 + bd T(n/2) T(n/2) T(n/2) T(n/2) Time required by MULT
  • 54. X=a; b Y=c; d ac ad bc bd XY = ac2n + (ad + bc)2n/2 + bd T(n/2) T(n/2) T(n/2) T(n/2) T(n) = 4 T(n/2) + (c1 n + c2) Conquering time divide and glue
  • 55. Recurrence Relation T(1) = c3 T(n) = 4 T(n/2) + c1 n + c2 What is the growth rate of T(n)?
  • 56. Let’s keep it simple T(1) = 1 T(n) = 4 T(n/2) + n What is the growth rate of T(n)?
  • 57. Guess: T(n) = 2n2 – n Verify: T(1) = 1 and T(n) = 4 T(n/2) + n 2n2 – n = 4 [2(n/2)2 – n/2] + n = 2n2 – 2n + n = 2n2 – n Technique: Guess and Verify
  • 58. Recursion Tree Representation We visualize the recursion as a tree, where each node represents a recursive call. The root is the initial call. Leaves correspond to the exit condition.
  • 59. T(n) = 4 T(n/2) + n T(n/2) T(n/2) T(n/2) T(n/2) T(n) T(n/4) T(n/4) T(n/4) T(n/4)
  • 60. Recursion Tree n/2 n/2 n/2 n/2 n n/4 n/4 n/4 n/4 each node reflects the combining step
  • 61. Recursion Tree n/2 n/2 n/2 n/2 n n/4 n/4 n/4 n/4 n 2n 4n write down the work at each level
  • 62. = 1*n = 2*n = 4*n = 2*i n = n*n T(n)=n (1+2+4+8+ . . . +2h ), where h = log2n T(n) = n(2n-1) = Θ(n2 ) Leve l i is th e sum of 4 i copies of n/2 i 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1 + 1 + 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1 + 1+ 1+ 1+ 1 . . . . . . . . . . . . . . . . . . . . . . . . . . n/2 + n/2 + n/2 + n/2 n n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/ 4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 0 1 2 i log n 2
  • 63. 1 + X 1 + X1 1 + X + X2 2 + X + X3 3 + … + X + … + Xn-1 n-1 + X + Xn n = = X Xn+1 n+1 - 1 - 1 X X - 1 - 1 The Geometric Series T(n)=n (1+2+4+8+ . . . +2h ), where h = log2n T(n) = n(2n-1) = Θ(n2 )
  • 64. Divide and Conquer MULT: Θ(n2 ) time Multiplication: Θ(n2 ) time All that work for nothing!
  • 65. Karatsuba, Anatolii Alexeevich (1937-) In 1962 Karatsuba had formulated the first algorithm to break n2 barrier!
  • 66. Multiplication of 2 n-bit numbers X = a 2n/2 + b Y = c 2n/2 + d X × Y = ac 2n + (ad + bc) 2n/2 + bd z1 = (a + b) (c + d) = ac + ad + bc + bd z2 = ac z3 = bd z4 = z2 – z3 = ac - bd z5 = z1 – z2 – z3 = ad + bc X × Y = z2 2n + z5 2n/2 + z3
  • 67. Karatsuba (1962) T(1) = 1 T(n) = 3 T(n/2) + n What is the growth rate of T(n)?
  • 71. n n/2 + n/2 + n/2 Level i is the sum of 3i copies of n/2i . . . . . . . . . . . . . . . . . . . . . . . . . . 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 0 1 2 i log n 2
  • 72. = 1n = 3/2n = 9/4n = (3/2)i n T(n) = n (1+3/2+(3/2)2 + . . . + (3/2)log2 n ) = ?? Leve l i is th e sum of 3 i copies of n/2 i 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1 + 1 + 1+ 1+ 1+ 1+ 1 + 1+ 1+ 1 + 1+ 1 + 1 + 1+ 1 + 1+ 1+ 1 + 1+ 1+ 1+ 1 . . . . . . . . . . . . . . . . . . . . . . . . . . n/2 + n/2 + n/2 n n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 0 1 2 i log n 2 = (3/2)log n n
  • 73. The Geometric Series 1 + X 1 + X1 1 + X + X2 2 + X + X3 3 + … + X + … + Xk-1 k-1 + X + Xk k = = X Xk+1 k+1 - 1 - 1 X X - 1 - 1 We have: X = 3/2 k = log2 n (3/2) × (3/2)log2 n - 1 ½ 3 × (3log2 n /2log2 n ) - 2 = 3 × (3log2 n /n) - 2 =
  • 74. Dramatic improvement for large n T(n) = 3 nlog2 3 – 2n = Θ(n log2 3 ) = Θ(n1.58… ) A huge savings over Θ(n2 ) when n gets large.
  • 75. Recursion Tree Representation f(n/b) f(n/b) f(n/b) f(n/b) f(n) f(n/b2 ) f(n/b2 ) f(n/b2 ) f(n/b2 ) T(n) = a T(n/b) + f(n) a nodes
  • 76. Solve the following recurrence T(n) = p T(n/3) + n where p>0. Exercise
  • 77. T(n) = p T(n/3) + n Let p>3 be an arbitrary parameter. Using the tree method, we obtain T(n) = n(1 + p/3 + p2 /9 + …+ (p/3)h ) where the tree height is log3n. It follows T(n) = Θ(nlog 3 p )
  • 78. Recurrence T(n) = p T(n/3) + n has the following has the following asymptotic solution asymptotic solution T(n) = T(n) = Θ (n n log log 3 3 p p ) ) when p>3. when p>3.
  • 79. T(n) = p T(n/3) + n Cases p =1, 2 and 3 should Cases p =1, 2 and 3 should be considered separately be considered separately If p =3, then If p =3, then T(n) = T(n) = Θ (n log n) n log n)
  • 80. 3-Way Multiplication X = a1 2n/4 + a2 2n/2 + a3 Y = b1 2n/4 + b2 2n/2 + b3 T(1) = 1 T(n) = 9 T(n/3) + n
  • 81. 3-Way Multiplication T(n) = p T(n/3) + n T(n) = Θ(nlog 3 p ) To get an improvement over Karatsuba’s, we have to decrease the number of multiplications to at least 5.
  • 82. Toom and Cook (1963, 1966) T(1) = 1 T(n) = 5 T(n/3) + n T(n) = Θ(n log3 5 ) = Θ(n1.46… )
  • 83. 3-Way Multiplication X = a1 2n/4 + a2 2n/2 + a3 Y = b1 2n/4 + b2 2n/2 + b3 Is it possible to reduce the number of multiplications to 5?
  • 84. Multiplication Algorithms Grade School n2 Karatsuba n1.58… 3-way n1.46… FFT n log2 n Schönhage and Strassen n logn loglogn