private void button6_Click(object sender, EventArgs e) { //https://www.youtube.com/watch?v=SAiTh4F401k //construct a square //update 5/13/2015 watch the video before understanding the logic int[,] matrix = new int[, ] { { 2, -1, -4, -20 }, { -3, 4, 2, 1 }, { 8, 10, 1, 3 }, { -1, 1, 7, -6 } }; //test //int[,] matrix = new int[,] { {1,1,1,1}, // {1,1,1,1}, // {1,1,1,1}, // {1,1,1,1}}; SubSquareSum result = FindSubSquareWithMaxSum2(matrix); StringBuilder sb = new StringBuilder(); if (result != null) { sb.Append("Row vaue: ").Append(result.Row).Append(" , "); sb.Append("Col vaue: ").Append(result.Col).Append(" , "); sb.Append("Size: ").Append(result.Size).Append(" , "); sb.Append("Sum : ").Append(result.Sum); } else { sb.Append("No squares found"); } this.textBox6.Text = sb.ToString(); }
//This methods finds all the possible subsquares by size basis and compute the sum for each subsuare and return the max public SubSquareSum FindAllPossibleSubSquareBySize(int[,] matrix, int size) { SubSquareSum max = new SubSquareSum(-1, -1, -1, Int32.MinValue); //For a given square of size n, there are (n - sz + 1) squares of length sz //that is for n = 5 there will be only 1 square..for n = 4 there will be two on first row..two on second row.. //count is no of possible square for the given size on a particualr row or column..count is same for row and column bcoz square row and col length is same int count = matrix.GetLength(0) - size + 1; //size =5 count = 1 //size =4 count = 2 //loop through rows for (int i = 0; i < count; i++) { //loop through colums for (int j = 0; j < count; j++) { int sum = FindSumofCellBySize(matrix, i, j, size); if (sum > max.Sum) { max = new SubSquareSum(i, j, size, sum); } } } return((max.Sum == Int32.MinValue) ? null : max); }
private void button7_Click(object sender, EventArgs e) { //construct a square int[,] matrix = new int[, ] { { 2, -1, -4, -20 }, { -3, 4, 2, 1 }, { 8, 10, 1, 3 }, { -1, 1, 7, -6 } }; SubSquareSum result = FindSubSquareWithMaxSum(matrix); StringBuilder sb = new StringBuilder(); if (result != null) { sb.Append("Row vaue: ").Append(result.Row).Append(" , "); sb.Append("Col vaue: ").Append(result.Col).Append(" , "); sb.Append("Size: ").Append(result.Size).Append(" , "); sb.Append("Sum : ").Append(result.Sum); } else { sb.Append("No squares found"); } this.textBox6.Text = sb.ToString(); }
public SubSquareSum FindSubSquareWithMaxSum(int[,] matrix) { SubSquareSum max = new SubSquareSum(-1, -1, -1, Int32.MinValue); //To get all possible square go with the window approach n, n-1,,n-2 to 1 //n = 4 4,3,2,1 for (int i = matrix.GetLength(0); i >= 1; i--) { SubSquareSum resultbysize = FindAllPossibleSubSquareBySize(matrix, i); if (resultbysize.Sum > max.Sum) { max = resultbysize; } } return((max.Sum == Int32.MinValue) ? null : max); }
public SubSquareSum FindSubSquareMaxWithDynamicProgramming(int[,] original) { int max = Int32.MinValue; //SubSquareSum maxsubsquare = new SubSquareSum(-1, -1, -1, Int32.MinValue); SubSquareSum maxsubsquare = null; int rowcount = original.GetLength(0); int colcount = original.GetLength(1); int[,] summatrix = ProcessMatrixToHoldSumValue(original); //for (int row1 = 0; row1 < rowcount; row1++) //{ // for (int row2 = row1; row2 < rowcount; row2++) // { // for (int col1 = 0; col1 < colcount; col1++) // { // for (int col2 = col1; col2 < colcount; col2++) // { // int sum = ComputeSum(summatrix, row1, row2, col1, col2); // if(max < sum ) // { // maxsubsquare = new SubSquareSum(row1, col1, -1, sum); // } // //max = Math.Max(max, ComputeSum(summatrix, row1, row2, col1, col2)); // } // } // } //} int sum = ComputeSum(summatrix, 1, 3, 0, 2); if (max < sum) { //maxsubsquare = new SubSquareSum(row1, col1, -1, sum); } return(maxsubsquare); }
public SubSquareSum FindSubSquareWithMaxSum2(int[,] matrix) { //int[,] summatrix = ProcessMatrixToHoldSumValue(matrix); //Here the summatrix will be one size larger than matrix int[,] summatrix = ProcessMatrixToHoldSumValueMethod2(matrix); SubSquareSum max = new SubSquareSum(-1, -1, -1, Int32.MinValue); //To get all possible square go with the window approach n, n-1,,n-2 to 1 //n = 4 4,3,2,1 for (int i = matrix.GetLength(0); i >= 1; i--) { SubSquareSum resultbysize = FindAllPossibleSubSquareBySizeOptimized(matrix, i, summatrix); if (resultbysize.Sum > max.Sum) { max = resultbysize; } } return((max.Sum == Int32.MinValue) ? null : max); }
//This methods finds all the possible subsquares by size basis and compute the sum for each subsuare and return the max //use the computed matrix and get the sum public SubSquareSum FindAllPossibleSubSquareBySizeOptimized(int[,] matrix, int size, int[,] summatrix) { SubSquareSum max = new SubSquareSum(-1, -1, -1, Int32.MinValue); //For a given square of size n, there are (n - sz + 1) squares of length sz //that is for n = 5 there will be only 1 square..for n = 4 there will be two on first row..two on second row.. //count is no of possible square for the given size on a particualr row or column..count is same for row and column bcoz square row and col length is same int count = matrix.GetLength(0) - size + 1; //size =5 count = 1 //size =4 count = 2 //loop through rows for (int i = 0; i < count; i++) { //loop through colums for (int j = 0; j < count; j++) { //we have starting point i, j //get the end point from size int row1 = i; int col1 = j; int row2 = (i + size - 1); int col2 = (j + size - 1); //we have starting and ending point.find sum from the optimized processed matrix int sum = ComputeSumMethod2(summatrix, row1, row2, col1, col2); if (sum > max.Sum) { max = new SubSquareSum(row1, col1, size, sum); } } } return((max.Sum == Int32.MinValue) ? null : max); }