Personal understanding of divide-and-conquer algorithm

Source: Internet
Author: User

Personal understanding of the divide-and-conquer algorithm:
Divide a complex problem into two or more identical or similar sub-problems, then divide the problem into smaller sub-problems ... Until the last sub-problem can be solved simply, the solution of the original problem is the merger of the solution of the sub-problem.
Take the quick sort as an example, taking a parameter first. Put the value greater than this parameter on the right, and the value less than the parameter on the left, so that the parameter is in the most correct position.
The basic idea of this case is that the array sorting problem is divided into the values in the array and the value of the selected parameter comparison size. A simple one-to-one comparison problem.
The comparison problem is independent, has the best substructure property, and can be combined.

1. Quick Sort

public class test{

public static void Main (String []args) {

int arr[] = {40,30,15,57,86,68,98};

Quick (arr, 0, arr.length-1);

for (int i:arr) {
System.out.print (i+ "");
}

}

public static void Quick (int[] arr, int min, int max) {

if (min < max) {

int n = sort (arr, Min, max);

Quick (arr, Min, n-1);
Quick (arr, n+1, Max);

}

}

public static int sort (int[] arr, int min, int max) {

int sum = Arr[min];

while (min < max) {

while (min < max && Arr[max] >= sum) {

max--;

}

Arr[min] = Arr[max];

while (min < max && Arr[min] <= sum) {

min++;

}

Arr[max] = Arr[min];

}

Arr[min] = sum;

return min;

}

}

Two-point search:
public class test{

public static void Main (String []args) {

int []arr = {0,1,3,5,7,8,9};

System.out.println (select (arr,0,arr.length-1,3));
}

public static int Select (int[] arr, int min, int max, int i) {

while (min <= max) {

int middle = (min + max)/2;

if (i = = Arr[middle]) {
return middle;
}else if (I<arr[middle]) {
Return select (Arr,min,middle-1,i);
}else{
Return Select (Arr,middle + 1,max,i);
}
}
return-1;
}

}

Hanoi

public class Test {

public static void Main (String []args) {

Move (3, "A", "B", "C");
}

public static void Move (I,string A, Sting B, String C) {

if (i==1) {
System.out.println ("+i+" is moved from "+a+" to "+c");
}else{

Move (I-1,A,C,B);
System.out.println ("+i+" is moved from "+a+" to "+c");
Move (I-1,B,A,C);
}
}
}


Matrix addition:

public class test{

public static void Main (String []args) {

Int[][] A = {{1,2},{3,4}};
Int[][] B = {{5,6},{7,8}};

Int[][] C = new Int[a.length][b.length];

if (a.length! = b.length) {
System.out.println ();
}else{
for (int i=0;i<a.length;i++) {
for (int j=0;i<b.length;j++) {
C[I][J] = A[i][j] + b[i][j];
}

}
}
}
}

Matrix Subtraction:

public class test{

public static void Main (String []args) {

Int[][] a= {{1,2},{3,4}};
Int[][] b= {{5,6},{7,8}};

Int[][] C = new Int[a.length][b.length];

if (a.length! = b.length) {
System.out.println ();
}else{
for (int i=0;i<a.length;i++) {
for (int j=0;i<b.length;j++) {
C[I][J] = a[i][j]-b[i][j];
}

}
}
}
}

Matrix multiplication (Simple direct):

public class test{

public static void Main (String []args) {
Int[][] a= {{1,2},{3,4}};
Int[][] b= {{5,6},{7,8}};

Int[][] C = new Int[a.length][b.length];

if (a.length! = b.length) {
System.out.println ("The matrix you entered is not correct!!! ");
}else{
for (int i=0;i<a.length;i++) {
for (int j=0;i<b.length;j++) {
C[I][J] = 0;
for (int k=0;k<c.length;k++) {
C[I][J] + = A[i][k]*b[k][j]
}
}

}
}
}
}

Matrix multiplication (using divide-and-conquer thinking):


public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub
int [][]a = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int [][]b = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};

int [][]C = flag (a,b,a.length);

for (int i = 0; i < c.length; i++) {
for (int j = 0; J < C.length; J + +) {
System.out.print (C[i][j] + "\ t");
}
System.out.println ();
}
}

private static int[][] Flag (int[][] A, int[][] b, int n) {

Int[][] result = new Int[n][n];

if (n = = 1) {
Result[0][0] = a[0][0]*b[0][0];
return result;
}

int [][]A11 = new INT[N/2][N/2];
int [][]A12 = new INT[N/2][N/2];
int [][]A21 = new INT[N/2][N/2];
int [][]a22 = new INT[N/2][N/2];
int [][]B11 = new INT[N/2][N/2];
int [][]B12 = new INT[N/2][N/2];
int [][]b21 = new INT[N/2][N/2];
int [][]b22 = new INT[N/2][N/2];

To block a matrix of a
for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
A11[I][J] = A[i][j];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
A12[I][J] = A[I][J+N/2];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
A21[I][J] = A[i+n/2][j];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
A22[I][J] = A[I+N/2][J+N/2];
}
}

To block a matrix of a
for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
B11[I][J] = B[i][j];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
B12[I][J] = B[I][J+N/2];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
B21[I][J] = B[i+n/2][j];
}
}

for (int i = 0; i < N/2; i++) {
for (int j = 0; J < N/2; J + +) {
B22[I][J] = B[I+N/2][J+N/2];
}
}

Find out the sum and subtract values of each block
int[][] S1 = del (B12,B22,N/2);
int[][] S2 = Add (A11,A12,N/2);
Int[][] S3 = Add (A21,A22,N/2);
Int[][] S4 = del (B21,B11,N/2);
Int[][] S5 = Add (A11,A22,N/2);
Int[][] S6 = Add (B11,B22,N/2);
int[][] s7 = del (A12,A22,N/2);
int[][] S8 = Add (B21,B22,N/2);
int[][] S9 = del (A11,A21,N/2);
Int[][] S10 = Add (B11,B12,N/2);

Find out 7 multiplication in this operation

int[][] M1 = Flag (A11,S1,N/2);
int[][] M2 = Flag (S2,B22,N/2);
int[][] M3 = Flag (S3,B11,N/2);
int[][] M4 = Flag (A22,S4,N/2);
int[][] M5 = Flag (S5,S6,N/2);
int[][] M6 = Flag (S7,S8,N/2);
int[][] M7 = Flag (S9,S10,N/2);


Make a final addition and subtraction
Int[][] C11 = Add (Del (Add (M5,M4,N/2), M2,N/2), M6,N/2);
Int[][] C12 = Add (M1,M2,N/2);
Int[][] C21 = Add (M3,M4,N/2);
int[][] C22 = del (Del (Add (M5,M1,N/2), M3,N/2), M7,N/2);

for (int i = 0; i < n; i++)
for (int j = 0; J < N; j + +) {
if (I < N/2) {
if (J < N/2) {
RESULT[I][J] = C11[i][j];
}else{
RESULT[I][J] = C12[I][J-N/2];
}
}else {
if (J < N/2) {
RESULT[I][J] = C21[i-n/2][j];
}else{
RESULT[I][J] = C22[I-N/2][J-N/2];
}
}
}

return result;
}

private static int[][] Add (int[][] A, int[][] b, int n) {
Int[][] result = new Int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
RESULT[I][J] = A[i][j] + b[i][j];
}
}
return result;
}

private static int[][] del (int[][] A, int[][] b, int n) {
Int[][] result = new Int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
RESULT[I][J] = a[i][j]-b[i][j];
}
}
return result;
}

}

Personal understanding of 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.