![]() |
VOOZH | about |
Given two binary strings that represent value of two integers, find the product of two strings. For example, if the first bit string is "1100" and second bit string is "1010", output should be 120.
For simplicity, let the length of two strings be same and be n.
A Naive Approach is to follow the process we study in school. One by one take all bits of second number and multiply it with all bits of first number. Finally add all multiplications. This algorithm takes O(n^2) time.
Using Divide and Conquer, we can multiply two integers in less time complexity. We divide the given numbers in two halves. Let the given numbers be X and Y.
For simplicity let us assume that n is even
X = Xl*2n/2 + Xr [Xl and Xr contain leftmost and rightmost n/2 bits of X]
Y = Yl*2n/2 + Yr [Yl and Yr contain leftmost and rightmost n/2 bits of Y]
The product XY can be written as follows.
XY = (Xl*2n/2 + Xr)(Yl*2n/2 + Yr)
= 2n XlYl + 2n/2(XlYr + XrYl) + XrYr
If we take a look at the above formula, there are four multiplications of size n/2, so we basically divided the problem of size n into four sub-problems of size n/2. But that doesn't help because the solution of recurrence T(n) = 4T(n/2) + O(n) is O(n^2). The tricky part of this algorithm is to change the middle two terms to some other form so that only one extra multiplication would be sufficient. The following is tricky expression for middle two terms.
XlYr + XrYl = (Xl + Xr)(Yl + Yr) - XlYl- XrYrSo the final value of XY becomes
XY = 2n XlYl + 2n/2 * [(Xl + Xr)(Yl + Yr) - XlYl - XrYr] + XrYr
With above trick, the recurrence becomes T(n) = 3T(n/2) + O(n) and solution of this recurrence is O(n1.59).
What if the lengths of input strings are different and are not even? To handle the different length case, we append 0's in the beginning. To handle odd length, we put floor(n/2) bits in left half and ceil(n/2) bits in right half. So the expression for XY changes to following.
XY = 22ceil(n/2) XlYl + 2ceil(n/2) * [(Xl + Xr)(Yl + Yr) - XlYl - XrYr] + XrYr
The above algorithm is called Karatsuba algorithm and it can be used for any base.
Following is C++ implementation of above algorithm.
120 60 30 10 0 49 9
Time Complexity: Time complexity of the above solution is O(nlog23) = O(n1.59).
Time complexity of multiplication can be further improved using another Divide and Conquer algorithm, fast Fourier transform. We will soon be discussing fast Fourier transform as a separate post.
Auxiliary Space: O(n)
Exercise:
The above program returns a long int value and will not work for big strings. Extend the above program to return a string instead of a long int value.
Solution:
Multiplication process for large numbers is an important problem in Computer Science. Given approach uses Divide and Conquer methodology.
Run the code to see the time complexity comparison for normal Binary Multiplication and Karatsuba Algorithm.
You can see the full code in this repository
Examples:
First Binary Input : 101001010101010010101001010100101010010101010010101
Second Binary Input : 101001010101010010101001010100101010010101010010101
Decimal Output : Not Representable
Output : 2.1148846e+30
First Binary Input : 1011
Second Binary Input : 1000
Decimal Output : 88
Output : 5e-05
Time Complexity:
The time complexity of both Classical and Karatsuba methods of binary string multiplication is O(n^2).
In the classical method, the time complexity is O(n^2) because the loop is iterated n times. The time complexity of the addBinary() method is constant because the loop runs with a maximum of two iterations.
In the Karatsuba method, the time complexity is O(n^2) because the 'multiply' method of the Karatsuba class is called recursively for each of the three products. The time complexity of the addStrings() method is constant because the loop runs with a maximum of two iterations.
Auxiliary Space :
The Auxiliary Space of both Classical and Karatsuba methods of binary string multiplication is O(n).
In the classical method, the Auxiliary Space is O(n) because the loop is iterated n times and a single string is used to store the result. The space complexity of the addBinary() method is constant because the loop runs with a maximum of two iterations.
In the Karatsuba method, the auxiliary Space is O(n) because the 'multiply' method of the Karatsuba class is called recursively for each of the three products.
Related Article :
Multiply Large Numbers Represented as Strings