Submatrix Sum Queries - GeeksforGeeks



Submatrix Sum Queries - GeeksforGeeks

Given a matrix of size M x N, there are large number of queries to find submatrix sums. Inputs to queries are left top and right bottom indexes of submatrix whose sum is to find out.

How to preprocess the matrix so that submatrix sum queries can be performed in O(1) time.

Example:

tli :  Row number of top left of query submatrix  tlj :  Column number of top left of query submatrix  rbi :  Row number of bottom right of query submatrix  rbj :  Column number of bottom right of query submatrix    Input: mat[M][N] = {{1, 2, 3, 4, 6},                      {5, 3, 8, 1, 2},                      {4, 6, 7, 5, 5},                      {2, 4, 8, 9, 4} };  Query1: tli = 0, tlj = 0, rbi = 1, rbj = 1  Query2: tli = 2, tlj = 2, rbi = 3, rbj = 4  Query3: tli = 1, tlj = 2, rbi = 3, rbj = 3;    Output:  Query1: 11  // Sum between (0, 0) and (1, 1)  Query2: 38  // Sum between (2, 2) and (3, 4)  Query3: 38  // Sum between (1, 2) and (3, 3)  

We strongly recommend you to minimize your browser and try this yourself first.

The idea is to first create an auxiliary matrix aux[M][N] such that aux[i][j] stores sum of elements in submatrix from (0,0) to (i,j). Once aux[][] is constructed, we can compute sum of submatrix between (tli, tlj) and (rbi, rbj) in O(1) time. We need to consider aux[rbi][rbj] and subtract all unncessary elements. Below is complete expression to compute submatrix sum in O(1) time.

Sum between (tli, tlj) and (rbi, rbj) is,     aux[rbi][rbj] - aux[tli-1][rbj] -      aux[rbi][tlj-1] + aux[tli-1][tlj-1]    The submatrix aux[tli-1][tlj-1] is added because    elements of it are subtracted twice.  

Illustration:

mat[M][N] = {{1, 2, 3, 4, 6},               {5, 3, 8, 1, 2},               {4, 6, 7, 5, 5},               {2, 4, 8, 9, 4} };    We first preprocess the matrix and build  following aux[M][N]  aux[M][N] = {1,  3,   6,  10, 16}              {6,  11,  22, 27,  35},              {10, 21,  39, 49,  62},                {12, 27,  53, 72,  89} }    Query : tli = 2, tlj = 2, rbi = 3, rbj = 4            Sum between (2, 2) and (3, 4) = 89 - 35 - 27 + 11                                = 38             

How to build aux[M][N]?
1. Copy first row of mat[][] to aux[][]
2. Do column wise sum of the matrix and store it.
3. Do the row wise sum of updated matrix aux[][] in step 2.


Read full article from Submatrix Sum Queries - GeeksforGeeks


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts