{"id":8138,"date":"2018-06-27T17:04:15","date_gmt":"2018-06-27T17:04:15","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=8138"},"modified":"2023-01-06T11:36:28","modified_gmt":"2023-01-06T11:36:28","slug":"how-to-find-the-submatrix-sum","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/how-to-find-the-submatrix-sum\/","title":{"rendered":"How to Find the Submatrix Sum"},"content":{"rendered":"<blockquote><p><i><span style=\"font-weight: 400;\">The essence of the problem that this test had was to find the <\/span><\/i><i>submatrix sum<\/i><i><span style=\"font-weight: 400;\"> of the elements. After a short period of time, our expert solved this problem. The challenge was easily accepted by our expert and now you can see the excellent result of it. Despite the fact that our sample can help students with their homework, we can offer an even better solution. AssignmentShark.com can find solutions to any of your homework problems. The very idea of our service is to provide <a href=\"https:\/\/assignmentshark.com\/math-assignment-help.html\" target=\"_blank\" rel=\"noopener\">math assignment help<\/a> students with technical tasks in the shortest period of time.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">First, you need to place your order on our site mentioning your requirements and setting the deadline. Obviously, we try to make the order system simple so you will not be confused. If you receive a completed order from us and don&#8217;t like something in it, you can request free revisions. We work 24\/7 to meet &#8220;<a href=\"https:\/\/assignmentshark.com\/do-my-homework.html\" target=\"_blank\" rel=\"noopener\">do my homework for me free<\/a>&#8221; requests. Check out our sample and find some helpful ideas to deal with your own homework.<\/span><\/i><\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">Submatrix With the Largest Sum of Elements <\/span><\/h2>\n<p><span style=\"font-weight: 400;\">We have the matrix N*N containing positive and negative numbers. We need to find the submatrix with the largest sum of elements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are many solutions to this problem. We will start with the most obvious method, then we will optimize it.<\/span><\/p>\n<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">The most obvious method: O(N<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">)<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Like the other tasks associated with the search of the maximum, this problem has a simple solution. It is enough to check all submatrices, calculate the sum of each, and to find the largest one.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To check out all the submatrices and avoid duplication we will have to go through all the ordered pairs of rows and then all ordered pairs of columns.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This solution will require O(N<\/span><span style=\"font-weight: 400;\">\u2076<\/span><span style=\"font-weight: 400;\">) time, since it is necessary to check the O(N<\/span><span style=\"font-weight: 400;\">\u2074<\/span><span style=\"font-weight: 400;\">) matrices, and the check of one matrix takes O(N\u00b2) time.<\/span><\/p>\n<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">Dynamic programming: O(N<\/span><span style=\"font-weight: 400;\">\u2074<\/span><span style=\"font-weight: 400;\">)<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Note that the previous solution is slow due to the calculation of the sum of matrix elements &#8211; O(N\u00b2) is a very slow operation. We can reduce the time of calcSum to O(1).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Take a look at the following rectangle:<\/span><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8142\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix1-300x105.jpg\" alt=\"\" width=\"300\" height=\"105\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix1-300x105.jpg 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix1-768x269.jpg 768w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix1.jpg 770w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Assume that we know the following values:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ValA = area(point(xl, yl))<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ValB = area(point(xl, y2))<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ValC = area(point(x2, yl))<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ValD = area(point(x2, y2))<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Each of these values begins at the starting point and ends at the bottom right corner of the subrectangle. We know the following about these values:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">area(D) = D &#8211; area(A and \u0421) &#8211; area(A and B) + area(A).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">or<\/span><\/p>\n<p><span style=\"font-weight: 400;\">area(D) = D &#8211; B &#8211; C + A<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This information will let us effectively calculate these values for all points of the matrix:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0423\u04301(\u0445, \u0443) = \u0423\u04301(\u0445-1, \u0443) + \u0423\u04301(\u0443-1, \u0445) &#8211; \u0423\u04301(\u0445-1, \u0443-1)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can calculate these values and then find the maximal submatrix. The following code implements this algorithm:<\/span><\/p>\n<p>[code]<br \/>\nint maxMatrix(int[][] inputMatrix) {<br \/>\n  int maximalSum = Integer.MIN_VALUE; \/\/ Maximal sum can be negative<br \/>\n  int rows = inputMatrix.length;<br \/>\n  int cols = inputMatrix[0].length;<br \/>\n  int[][] matrix = precalcMatrix(inputMatrix);<br \/>\n  for (int rowl = 0; rowl &lt; rows; rowl++) {<br \/>\n    for (int row2 = rowl; row2 &lt; rows; row2++) {<br \/>\n      for (int coli = 0; coli &lt; cols; coll++) {<br \/>\n        for (int col2 = coli; col2 &lt; cols; col2++) {<br \/>\n          maximalSum = Math.max(maximalSum, calcSum(matrix, rowl, row2, coli, col2));<br \/>\n        }<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n  return maximalSum<br \/>\n}<\/p>\n<p>int[][] precalcMatrix(int[][] matrix) {<br \/>\n  int[][] sumMatrix = new int[matrix.length][matrix[0].length];<br \/>\n  for (int i = 0; i &lt; matrix.length; i++) {<br \/>\n    for (int j = 0; j &lt; matrix.length; j++) {<br \/>\n      if (i == 0 &amp;&amp; j == 0) { \/\/ first cell<br \/>\n        sumMatrix[i][j] = matrix[i][j];<br \/>\n      } else if (j == 0) { \/\/ cell in the first column<br \/>\n        sumMatrix[i][j] = sumMatrix[i &#8211; l][j] + matrix[i][j];<br \/>\n      } else if (i == 0) { \/\/ cell in the first row<br \/>\n        sumMatrix[i][j] = sumMatrix[i][j-1] + matrix[i][j];<br \/>\n      } else {<br \/>\n        sumMatrix[i][j] = sumMatrix[i-l][j] +<br \/>\n        sumMatrix[i][j-1] &#8211; sumMatrix[i-l][j-1] +<br \/>\n        matrix[i][j];<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n  return sumMatrix;<br \/>\n}<\/p>\n<p>int calcSum(int[][] sumMatrix, int il, int i2, int jl, int j2) {<br \/>\n  if (il == 0 &amp;&amp; jl == 0) { \/\/ row 0, column 0<br \/>\n    return sumMatrix[i2][j2];<br \/>\n  } else if (il == 0) { \/\/ row 0<br \/>\n    return sumMatrix[i2][j2] &#8211; sumMatrix[i2][jl &#8211; 1];<br \/>\n  } else if (jl == 0) { \/\/ column 0<br \/>\n    return sumMatrix[i2][j2] &#8211; sumMatrix[il-l][j2];<br \/>\n  } else {<br \/>\n    return sumMatrix[i2][j2] &#8211; sumMatrix[i2][jl-1] &#8211; sumMatrix[il-l][j2] + sumMatrix[il-1][jl-1];<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">Optimized solution: O(N\u00b3)<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">There is a better solution. If we have R rows and C columns, the problem can be solved in O (R\u00b2C) time.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Each submatrix can be represented as a series of rows and columns. You can walk along the rows and columns to find what is giving the maximum amount.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The code looks like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = 0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach startRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach endRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = max(currentMax, max)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">return max<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now we have a question: how to find the \u201cbest\u201d startCol and endCol?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s take a look at the submatrix:<\/span><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8140\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix2-300x115.png\" alt=\"\" width=\"300\" height=\"115\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix2-300x115.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix2-604x231.png 604w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/submatrix2.png 605w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">We need to find startCol and endCol, which will give us the biggest possible sum of all submatrices startRow at the top and endRow at the bottom. We can calculate the sum of each column and use the maxSubArr function.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now we can write the following pseudocode:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = 0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach startRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach endRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach col in columns<\/span><\/p>\n<p><span style=\"font-weight: 400;\">fractionalSum[col] = sum of matrix[startRow, col] through<\/span><\/p>\n<p><span style=\"font-weight: 400;\">matrix[endRow, col]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">currentMax = maxSubArray(fractionalSum)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = max(currentMax, max)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">return max<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Calculation of the sum in rows 5 and 6 takes R*C time which gives the total time O(R\u00b3C).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = 0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach startRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">clear array fractionalSum<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach endRow in rows<\/span><\/p>\n<p><span style=\"font-weight: 400;\">foreach col in columns<\/span><\/p>\n<p><span style=\"font-weight: 400;\">fractionalSum[col] += matrix[endRow, col]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">currentMax = maxSubArray(fractionalSum)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">max = max(currentMax, max)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">return max<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The full version of the code looks like this:<\/span><\/p>\n<p>[code]<br \/>\npublic void clear(int[] arr) {<br \/>\n  for (int i = 0; i &lt; arr.length; i++) {<br \/>\n    arr[i] = 0;<br \/>\n  }<br \/>\n}<\/p>\n<p>public static int maxSubMatrix(int[][] originalMatrix) {<br \/>\n  int rows = originalMatrix.length;<br \/>\n  int cols = originalMatrix[0].length;<\/p>\n<p>  int[] fractionalSum = new int[cols];<br \/>\n  int max = 0;<\/p>\n<p>  for (int startRow = 0; startRow &lt; rows; startRow++) {<br \/>\n    clear(fractionalSum);<\/p>\n<p>    for (int endRow = startRow; endRow &lt; rows; endRow++) {<br \/>\n      for (int i = 0; i &lt; cols; i++) {<br \/>\n        fractionalSum[i] += originalMatrix[endRow][i]j<br \/>\n      }<\/p>\n<p>      int tempMaxSum = maxSubArray(fractionalSum, cols);<\/p>\n<p>      max = Math.max(max, tempMaxSum);<br \/>\n    }<br \/>\n  }<br \/>\n  return max;<br \/>\n}<\/p>\n<p>public static int maxSubArray(int arr[], int N) {<br \/>\n  int max = 0;<br \/>\n  int currentSum = 0;<\/p>\n<p>  for (int i = 0; i &lt; N; i++) {<br \/>\n    currentSum += arr[i];<br \/>\n    max = Math.max(max, currentSum);<\/p>\n<p>    if  (currentSum &lt; 0) {<br \/>\n      currentSum  = 0;<br \/>\n    }<br \/>\n  }<br \/>\n  return  max;<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p><span style=\"font-weight: 400;\">This is an extremely difficult task. But as we can see, nothing is impossible!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The essence of the problem that this test had was to find the submatrix sum of the elements. After a short period of time, our expert solved this problem. The challenge was easily accepted by our expert and now you can see the excellent result of it. Despite the fact that our sample can help [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-8138","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=8138"}],"version-history":[{"count":6,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8138\/revisions"}],"predecessor-version":[{"id":13391,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8138\/revisions\/13391"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=8138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=8138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=8138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}