作为一项关于休息的家庭作业,我们的教授要求我们找到斯特拉森的算法效率低于蛮力的地步(具体来说,在我蛮力之前,我应该在这台机器上进行多少次递归? )
我是Java,并且真的很难让它恢复到暴力。我有一个跑步课,一个蛮力课,一个叫Strassen的课,还有一个Strassen本身的课。到目前为止,我只会把斯特拉森的课程搞定。我问用户他们想要做多少次递归,然后我把它作为参数传递给Strassen's。这个想法是,当深度'深度'它变得大于指定的递归数,它调用强力方法来完成。我感到完全迷失了,可能没有正确接近,但我们没有得到非常明确的指示,而且我一直在尝试不同的方法,但无济于事。
public class strassens
{
private int totalCalls;
private int runtime;
public strassens(int recursions, int runtime)
{
totalCalls = recursions;
this.runtime = runtime;
}
public int[][] strassen(int[][] A, int[][] B, int depth)
{
int n = A.length;
if(depth > totalCalls)
{
int[][] complete = new int[A.length][B.length];
complete = bruteForce.brute(n, A, B, runtime);
return complete;
}
int [][] result = new int[n][n];
if((n%2 != 0 ) && (n != 1))
{
int[][] a1, b1, c1;
int n1 = n+1;
a1 = new int[n1][n1];
b1 = new int[n1][n1];
c1 = new int[n1][n1];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
a1[i][j] = A[i][j];
b1[i][j] = B[i][j];
}
c1 = strassen(a1, b1, depth++);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
result[i][j] = c1[i][j];
return result;
}
if(n == 1)
{
result[0][0] = A[0][0] * B[0][0];
}
else
{
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];
divideArray(A, A11, 0 , 0);
divideArray(A, A12, 0 , n/2);
divideArray(A, A21, n/2, 0);
divideArray(A, A22, n/2, n/2);
divideArray(B, B11, 0 , 0);
divideArray(B, B12, 0 , n/2);
divideArray(B, B21, n/2, 0);
divideArray(B, B22, n/2, n/2);
int [][] P1 = strassen(addMatrices(A11, A22), addMatrices(B11, B22), depth);
int [][] P2 = strassen(addMatrices(A21, A22), B11, depth);
int [][] P3 = strassen(A11, subtractMatrices(B12, B22), depth);
int [][] P4 = strassen(A22, subtractMatrices(B21, B11), depth);
int [][] P5 = strassen(addMatrices(A11, A12), B22, depth);
int [][] P6 = strassen(subtractMatrices(A21, A11), addMatrices(B11, B12), depth);
int [][] P7 = strassen(subtractMatrices(A12, A22), addMatrices(B21, B22), depth);
int [][] C11 = addMatrices(subtractMatrices(addMatrices(P1, P4), P5), P7);
int [][] C12 = addMatrices(P3, P5);
int [][] C21 = addMatrices(P2, P4);
int [][] C22 = addMatrices(subtractMatrices(addMatrices(P1, P3), P2), P6);
copySubArray(C11, result, 0 , 0);
copySubArray(C12, result, 0 , n/2);
copySubArray(C21, result, n/2, 0);
copySubArray(C22, result, n/2, n/2);
}
return result;
}
public int [][] addMatrices(int [][] A, int [][] B)
{
int n = A.length;
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;
}
public int [][] subtractMatrices(int [][] A, int [][] B)
{
int n = A.length;
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;
}
public void divideArray(int[][] parent, int[][] child, int iB, int jB)
{
for(int i1 = 0, i2=iB; i1<child.length; i1++, i2++)
for(int j1 = 0, j2=jB; j1<child.length; j1++, j2++)
{
child[i1][j1] = parent[i2][j2];
}
}
public void copySubArray(int[][] child, int[][] parent, int iB, int jB)
{
for(int i1 = 0, i2=iB; i1<child.length; i1++, i2++)
for(int j1 = 0, j2=jB; j1<child.length; j1++, j2++)
{
parent[i2][j2] = child[i1][j1];
}
}
public static void printArray(int [][] array)
{
int n = array.length;
System.out.println("Strassens: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}