Young's Matrix

Source: Internet
Author: User

If each row of a matrix is strictly monotonically incremented, we call the matrix the young Tableau. For the young matrix (a[m][N]), it usually involves two questions: (1) How to find an element x in the young matrix. (2) How to find the number of K large in the young matrix.

2. The solution

The young matrix is a very ingenious data structure, which can be used as a heap and as a balance tree.

(1) Problem 1 solution

"Programme One"

< two-point search >

For the young matrix, the binary lookup can be used in the matrix because each row is ordered. The specific methods are:

For the current sub-matrix A[i][j]~a[s][t], the intermediate element is a[(i+s)/2][(j+t)/2) and if a[(i+s)/2][(j+t) is/2]==x, the element is found; if a[(I+s)/2][(j+t)/2] > x, is found in the Matrix a[i][j]~a[(i+s)/2][(j+t)/2], if a[(i+s)/2][(j+t)/2] < X, then in three sub-matrices: a[i][(j+t)/2]~ a[(i+s)/2][t],a[(i+s)/2][ (j+t)/2]~ a[s][t] and a[(i+s)/2][j]~ a[s][(j+t)/2]. The recursive type of the algorithm is F (MN) =3f (MN/4) +o (1), which, according to the principal theorem, has a time complexity of: O ((MN) ^ (LOG4 (3)).

"Programme II"

< class heap Lookup method >

The young matrix has obvious heap characteristics: starting from the upper-right corner of the matrix (similar to the idea of starting from the lower left corner), for element A[i][j], if a[i][j]==x, find element x, return directly, if A[I][J] > x, then Move down, that is, continue comparing a[i+1][j] with X ; if a[i][j]<x, move left, that is, continue comparing a[i][j-1] with X. The time complexity of the algorithm is O (m+n), the code is as follows: [cpp]  view plain Copy Bool find_in_youngtableau (int** a, int m,  int n, int x)  {     assert (a != null &&  m > 0 && n > 0);     int row,  col;     row = 0;     col = n-1;      while (row <= m-1 && col >= 0)  {        if (a[row][col] == x)          return true;        else if (a[row][col] > x)           col--;       else         row++;     }     return false;   }   

(2) Problem 2 solution

"Programme One"

< small top Pile method >

First you add a[0][0] to the small top heap, and then each time you take the smallest element out of the heap and add the two elements (for a[0][0) that are larger than the element and are adjacent to it (to the a[0][1] and a[1][0]) until you remove the k element, you need to be aware that Additional space is required to record whether an element has been added to the small top heap to prevent duplicate joins.

"Programme II"

< Binary enumeration + class Heap Lookup >

First, the binary enumeration finds a number x, which is larger than the K number in the young matrix, and then uses the class heap lookup method to find elements that are just less than X. The time complexity of the algorithm is O ((m+n) LG (MN)), but no additional storage space is required. The code is as follows:


[cpp]  View Plain Copy Int find_kth_in_youngtableau (Int** a, int m, int n,  int k)  {     int low = a[0][0];     int  high = a[m-1][n-1];     int order = 0;      int mid = 0;     do {       mid  =  (Low + high)  >> 1;       order =  get_order (A, m, n, mid);       if (order == k)          break;       else if (order  > k)          high = mid - 1;        else         low = mid +  1;   &nbsP; } while (1);  //Binary enumeration integer to find an integer  mid          int that is just greater than k  row = 0;     int col = n - 1;      int ret = mid;     while (row <= m-1 &&  col >= 0)  { //Find the maximum number of smaller than mid        if (a[row][col] <  mid)  {         ret =  (a[row][col] >  RET)  ? a[row][col] : ret;         row++;        } else {         col--;        }     }     return ret;  }       //integer k ranking in matrices    Int get_order (int** a, int m,  int  n, int k)  {     int row, col, order;      row = 0;     col = n-1;     order = 0;      while (row <= m-1 && col >= 0)  {        if (a[row][col] < k)  {          order += col + 1;         row++;        } else {         col--;        }     }     return order;   }   

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.