L16 - Karatsuba Algorithm

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

KARATSUBA ALGORITHM

Introduction
The Karatsuba algorithm is a fast multiplication algorithm. It was
discovered by Anatoly Karatsuba in 1960 and published in 1962.

A fast multiplication algorithm uses a divide and conquer approach to


multiply two numbers.

The naive algorithm for multiplying 2 numbers has a running time of


O(n2), where karatsuba algorithm has a runtime of
Being able to multiply numbers quickly is very important.
Computer scientists often consider multiplication to be a constant
time ~ O(1) operation which is reasonable for small numbers.

Whereas for larger numbers, the actual running times need to be


factored in, which is O(n2)
Algorithm
1. Compute starting set (a*c)
2. Compute set after starting set may it be ending set (b*d)
3. Compute starting set with ending sets (a+b)(c+d)
4. Subtract values of Step 3 from Step 2 from Step 1
5. Add all the values with the following modifications:-
1. Pad up 10n to the number obtained from Step 1
2. Step 2 value unchanged
3. Pad up 10n/2 to the value obtained from Step 4.
Karatsuba Algorithm

Let’s consider two 4-digit numbers x and y where x=1234 and y=5678.
First of all, we should divide the n-digit numbers into n/2-digit numbers as
shown below.

a and c represent the first n/2 digits of x and y.


Similarly, b and d represent the last n/2 digits of x and y
The Karatsuba algorithm involves 4 main steps

Step 1: Compute a.c = 12 x 56 = 672


Step 2: Compute b.d = 34 x 78 = 2652
Step 3: Compute (a+b)(c+d) = 46 x 134 = 6164
Step 4: Compute (3)-(2)-(1)=6164-2652-672 = 2840

Finally, multiply the output of step 1 by 10 n, the output of step 4 by


10n/2, and add them both with the output of step 2.

6720000 + 284000 + 2652 = 7006652


To multiply X = 1234 and Y = 2345 using the Karatsuba algorithm
12 34 23 45

Step 1: Compute a.c = 12 x 23 = 276


Step 2: Compute b.d = 34 x 45 = 1530
Step 3: Compute (a+b)(c+d) = 46 x 68 = 3128
Step 4: Compute (3) - (2) - (1) = 3128 - 1530 - 276 = 1322
Finally, multiply the output of step 1 by 10000 (104), the output of step 4 by 100 (102),
and add them both with the output of step 2.
Result: 2760000 + 132200 + 1530 = 2893730
The product of 1234 and 2345 using the Karatsuba algorithm is 2897730.
Compute the product of 23 and 67 using the Karatsuba algorithm.

2 3 6 7

Step 1: Compute a.c = 2 x 6 = 12


Step 2: Compute b.d = 3 x 7 = 21
Step 3: Compute (a+b)(c+d) = (2+3)(6+7) = 5 x 13 = 65
Step 4: Compute (3) - (2) - (1) = 65 - 21 - 12 = 32
Finally, multiply the output of Step 1 by 10^n (where n is the number of digits), the
output of Step 4 by 10^n/2, and add them both with the output of Step 2.
1200 + 320 + 21 = 1541
product of 23 and 67 using the Karatsuba algorithm is 1541.
Time Complexity
Assuming that we replace two of the multiplications with only one makes the program
faster.

Karatsuba improves the multiplication process by replacing the initial complexity


from quadratic to

Where n is the number of digits of the numbers multiplying.

The Time Complexity of the algorithm can be represented as follows


import java.math.BigInteger;
import java.util.Random;
class Karatsuba {
private final static BigInteger ZERO = new BigInteger("0");
public static BigInteger karatsuba(BigInteger x, BigInteger y)
{
// cutoff to brute force
int N = Math.max(x.bitLength(), y.bitLength());
if (N <= 2000) return x.multiply(y); // optimize this parameter
// number of bits divided by 2, rounded up
N = (N / 2) + (N % 2);
// x = a + 2^N b, y = c + 2^N d
BigInteger b = x.shiftRight(N);
BigInteger a = x.subtract(b.shiftLeft(N));
BigInteger d = y.shiftRight(N);
BigInteger c = y.subtract(d.shiftLeft(N));
// compute sub-expressions
BigInteger ac = karatsuba(a, c);
BigInteger bd = karatsuba(b, d);
BigInteger abcd = karatsuba(a.add(b), c.add(d));
return ac.add(abcd.subtract(ac).subtract(bd).shiftLeft(N)).add(bd.shiftLeft(2*N));
}
public static void main(String[] args)
{
long start, stop, elapsed;
Random random = new Random();

int N = Integer.parseInt(args[0]);
BigInteger a = new BigInteger(N, random);
BigInteger b = new BigInteger(N, random);

start = System.currentTimeMillis();
BigInteger c = karatsuba(a, b);
stop = System.currentTimeMillis();
StdOut.println(stop - start);

start = System.currentTimeMillis();
BigInteger d = a.multiply(b);
stop = System.currentTimeMillis();
StdOut.println(stop - start);

StdOut.println((c.equals(d)));
}
}
Interview questions
What is the Karatsuba algorithm?

The Karatsuba algorithm is a fast multiplication algorithm that allows


multiplying large integers in a more efficient manner than the traditional
multiplication algorithm. It reduces the number of recursive multiplications
required by breaking down the numbers into smaller parts.
Interview questions

How does the Karatsuba algorithm work?

The Karatsuba algorithm works by splitting the input numbers into two
halves and recursively calculating three products. These three products are
combined using some arithmetic operations to obtain the final product.
Interview questions

What are the advantages of the Karatsuba algorithm over


traditional multiplication?

The Karatsuba algorithm has a lower time complexity than traditional


multiplication algorithms for large numbers. It reduces the number of
multiplications required and, therefore, improves the overall efficiency of
the multiplication operation.
Interview questions
Can you explain the recursive steps involved in the
Karatsuba algorithm?

In the Karatsuba algorithm, the input numbers are split into two halves.
Three recursive multiplications are performed: one for the lower halves, one
for the sum of the halves, and one for the upper halves. These products are
combined using arithmetic operations to obtain the final result.
THANK YOU

You might also like