--karastsuba algorithm for divide-and-conquer algorithm

Source: Internet
Author: User

Divide and Conquer (Divide and Conquer) algorithm: The problem can be decomposed into sub-problems, each problem can be solved independently, from the solution of sub-problem can build the original problem.

Divide: Intermediate points, random points, odd even points, and so on, the problem is decomposed into independent sub-problems

Conquer: The solution of sub-problem can be solved by itself, and the final solution of the original problem is constructed from the solution of sub-problem

Combine: Each step merges the solution produced by the sub-problem to get the final solution, and the complexity of the merging affects the final algorithm time complexity.

The Karatsuba algorithm is promoted on the basis of the common multiplication algorithm, which makes the final complexity from O (n^2) to O (n^1.585), the basic idea is to reduce the size of the original problem each time, and solve three sub-problems at a time:

X =  xl*2n/2 + XR    [Xl left N/2 number  Xr right n/2 digits]y =  YL*2N/2 + Yr    [Yl left N/2 number of digits  
XY = (XL*2N/2 + Xr) (YL*2N/2 + Yr)   = 2n xlyl + 2N/2 (Xlyr + xryl) + Xryr
XY = 2n xlyl + 2N/2 * [(Xl + Xr) (Yl + Yr)-Xlyl-xryr] + Xryr
XY = 22ceil (N/2) xlyl + 2ceil (N/2) * [(Xl + Xr) (Yl + Yr)-Xlyl-xryr] + Xryr
From

In order to get the final algorithm time complexity of t (n) = 3T (N/2) + O (n), get t (n) = O (n^1.585). The pseudo code of the algorithm is as follows:

Karatsuba (NUM1, num2)  if (Num1 <) or (Num2 < ten)   return num1*num2/  * Calculates the size of the Numbe RS */  m = max (Size_base10 (NUM1), Size_base10 (num2))  m2 = M/2/* Split the digit sequences about the  middle */< C6/>HIGH1, Low1 = Split_at (Num1, m2)  high2, Low2 = Split_at (num2, m2)/  * 3 calls made to numbers approximately HAL f the size */  z0 = Karatsuba (low1,low2)  z1 = Karatsuba ((LOW1+HIGH1), (LOW2+HIGH2))  z2 = Karatsuba (HIGH1, HIGH2)  return (z2*10^ (2*m2)) + ((z1-z2-z0) *10^ (m2)) + (Z0)
The following is a concrete implementation of the C + + process, if the direct use of integer type implementation, overflow may occur, so use the input string representation, the actual operation of the process of converting strings into arrays for addition, subtraction, multiplication operation. Let's look at the final algorithm implementation:

String Multiplicate (string x, string y) {int len = getsamesize (x, y), if (len = = 0) return 0;if (len = = 1) return Multiplyst Ring (x, y); int p = len% 2 = = 0? LEN/2: LEN/2 + 1;string Xh = x.substr (0, LEN/2); string Yh = Y.substr (0, LEN/2); string Xl = X.substr (LEN/2); stri ng Yl = Y.substr (LEN/2); string P1 = Multiplicate (Xh, Yh); string P2 = Multiplicate (Xl, Yl); string P3 = Multiplicate (addst Ring (Xh, Xl), AddString (Yh, Yl)), Return addstring (AddString (Multiplypower, 2 * p), P1 (Multiplypower ( Minusstring (P3, P1), P2), p)), P2);}
The above is implemented according to pseudo-code, but using a string of numeric operations, including string and array conversion, array addition, subtraction, multiplication, the implementation of the following:

void StringToArray (String a, int *arr) {int n = a.size (); for (int i = 0; i < n; i++) arr[n-i-1] = a.at (i)-' 0 ';} void arraytostring (int *arr, int len, string & A) {for (int i = 0; i < len; i++) A + = ' 0 ' + arr[len-i-1];} String Delprezero (String a) {int zeros = 0;for (int i = 0; i < a.size (); i++) if (a.at (i) = = ' 0 ') Zeros++;else break;if ( Zeros = = A.size ()) return "0"; return a.substr (zeros);}  void Multiplyarray (int a[], int la, int b[], int lb, int *arr) {int i;for (i = 0; i < LA; i++) for (int j = 0; j < lb; J + +) Arr[i + j] + + a[i] * b[j];for (i = 0; I < La + lb-1; i++) {arr[i + 1] + = Arr[i]/10;arr[i] = arr[i]% 10;}} void AddArray (int a[], int la, int b[], int lb, int *arr) {int I;int len = la > lb? lb:la;for (i = 0; i < Len; i++  ) Arr[i] + = A[i] + b[i];if (La > lb) {for (i = lb; i < LA; i++) arr[i] = a[i];for (i = 0; i < LA; i++) {arr[i + 1] + = Arr[i]/10;arr[i] = arr[i]% 10;}} Else{for (i = la; i < lb; i++) arr[i] = b[i];for (i = 0; i < lb; i++){arr[i + 1] + = Arr[i]/10;arr[i] = arr[i]% 10;}} void Minusarray (int a[], int la, int b[], int lb, int *arr)//a must be bigger than b{int i;for (i = 0; i < lb; i++) arr [i] = a[i]-b[i];for (i = lb; i < LA; i++) arr[i] = a[i];for (i = 0; i < la-1; i++) {if (Arr[i] < 0) {arr[i + 1]- -;arr[i] = ten + Arr[i];}}} String Multiplystring (String A, string b) {int m = a.size (), n = b.size (); int *arra = new Int[m];int *ARRB = new Int[n]; StringToArray (A, ArrA); StringToArray (b, ARRB); int *ARRC = new Int[m + n];for (int i = 0; i < n + m; i++) arrc[i] = 0;string rst; Multiplyarray (ArrA, M, ARRB, N, ArrC); Arraytostring (ArrC, M + N, rst);d elete []arra;delete []arrb;delete []arrc;return Delprezero (RST);} String AddString (String A, string b) {int m = a.size (), n = b.size (); int *arra = new Int[m];int *ARRB = new Int[n]; StringToArray (A, ArrA); StringToArray (b, ARRB); int i, len = m > n? M:n;int *ARRC = new Int[len + 1];for (i = 0; i < len + 1; i++) arrc[i] = 0; AddArray (ArrA, M, ARRB, N, ArrC); string rst; Arraytostring (ArrC, Len + 1, rst);d elete []arra;delete []arrb;delete []arrc;return Delprezero (RST);} String Multiplypower (String a, int len) {for (int i = 0; i < len; i++) A + = ' 0 '; return Delprezero (a);} String Minusstring (String A, string b) {int m = a.size (), n = b.size (); int *arra = new Int[m];int *ARRB = new Int[n]; StringToArray (A, ArrA); StringToArray (b, ARRB); string Rst;int i, Len = m > n? M:n;int *ARRC = new Int[len];for (i = 0; i < len; i++) Arrc[i] = 0; Minusarray (ArrA, M, ARRB, N, ArrC); Arraytostring (ArrC, Len, rst);d elete []arra;delete []arrb;delete []arrc;return Delprezero (RST);}

Mainly involves the string and array conversion in the string is reversed in the number, the array operation is convenient, and for the subtraction between the array, only support a greater than the subtraction of B, if a is less than B can be minus a B and then take the inverse. There is also an array of dynamic space applications that need to be released in a timely manner.

Reference:

1.http://www.geeksforgeeks.org/divide-and-conquer-set-2-karatsuba-algorithm-for-fast-multiplication/

2.http://en.wikipedia.org/wiki/karatsuba_algorithm#pseudo_code_implementation


--karastsuba algorithm for divide-and-conquer algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.