算法导论 6-3 Young tableaus 杨氏矩阵

来源:互联网 发布:c语言三行表白代码 编辑:程序博客网 时间:2024/06/07 03:11
6-3 Young tableaus 杨氏矩阵
An m*n Young tableau is an m*n matrix such that the entries of each row are
in sorted order from left to right and the entries of each column are in sorted order
from top to bottom. 
Some of the entries of a Young tableau may be Max(Long.Max), which we treat as nonexistent elements. 

Thus, a Young tableau can be used to hold r <= m*n finite numbers.

a. Draw a 4*4 Young tableau containing the elements {9; 16; 3; 2; 4; 8; 5; 14; 12}.

[2,  3,  9,   12]
[4,  8,  14,  max]
[5, 16,  max, max]
[max,max,max, max]
b. Argue(prove) that an m*n Young tableau Y is empty if Y[1,1]= Max. 
Argue that Y is full (contains m*n elements) if Y[m,n]< Max.

public class YoungTableaus {  public static void main(String[] args) {    int[][] young = new int[4][4];    init(young);    youngExtractMin(young, 4, 4);    youngInsert(young, 17, 4, 4);    youngInsert(young, 23, 4, 4);    youngExtractMin(young, 4, 4);    youngInsert(young, 19, 4, 4);    youngInsert(young, 13, 4, 4);    youngSearch(young, 5, 4, 4);    youngSearch(young, 9, 4, 4);    youngSearch(young, 3, 4, 4);    youngSearch(young, 18, 4, 4);  }  /* c. Give an algorithm to implement EXTRACT-MIN on a nonempty m*n Young tableau   that runs in O(m+n) time.   */  public static int youngExtractMin(int[][] young, int m, int n) {    int min = young[0][0];    int i=0;    int j=0;    young[i][j] = Integer.MAX_VALUE;    youngAdjust(young, i, j, m, n);    System.out.println("Extracted min:" + min);    youngPrint(young, 4);    return min;  }  public static void youngAdjust(int[][] young, int i, int j, int m, int n) {//O(m+n)    int row =i;    int col =j;    int min = young[i][j];    if(i+1<m && min > young[i+1][j]) {//move down to next row      row = i +1;      col = j;      min = young[i+1][j];    }    if(j+1<n && min> young[i][j+1]) {//move right to next column      row = i;      col = j +1;      min = young[i][j+1];    }    if(row != i || col != j) {      int tmp = young[row][col];      young[row][col]=young[i][j];      young[i][j] = tmp;      youngAdjust(young, row, col, m, n);    }  }  /* d. Show how to insert a new element into a non full m*n Young tableau in O(m+n) time.   insert to Y[m-1][n-1] if table is not full, then adjust to left/up or up/left. */  public static void youngInsert(int[][] young, int key, int m, int n) {    if(young[m-1][n-1] < Integer.MAX_VALUE) {      throw new RuntimeException("Young table is full, can not insert any data!");    }    young[m-1][n-1] = key;    System.out.print("Insert "+key+" at ");    int i = m-1;    int j = n-1;    int row = i;    int col = j;    int max = key;    while(true) {      if(i > 0 && young[i-1][j] > max) {//check row-1 first, if [i][j-1]=[i-1[j], move up to row-1        row = i -1;        col = j;        max = young[i-1][j];      }      if(j > 0 && young[i][j-1] > max) {//check col-1 second, if [i][j-1]=[i-1[j], move up to row-1        row = i;        col = j-1;        max = young[i][j-1];      }      if(max != young[i][j]) {        int temp = young[row][col];        young[row][col] = young[i][j];        young[i][j] = temp;        i = row;        j = col;        max = young[i][j];      }      else {        System.out.println("["+i+","+j+"]");        break;      }    }    youngPrint(young, 4);  }/**f. Give an O(m+n)-time algorithm to determine whether a given number isstored in a given m*n Young tableau.From [m-1][0] or [0][n-1] start search, exclude one row/column after comparing */  public static boolean youngSearch(int[][] young, int key, int m, int n) {    int i = 0;    int j = n-1; //from Y[0][n-1] start search    while(i<m && j>=0) {      if(young[i][j] > key) {        j--;  // exclude j column since they are bigger      }      else if(young[i][j] < key) {        i++;  // exclude i row since they are smaller       }      else {        System.out.println(key+" was found at "+i+","+j);        return true;      }    }    System.out.println(key+" was not found even at "+i+","+j);    return false;  }  static void youngPrint(int[][] young, int m) {    System.out.println("Young Tableau:");    for(int i=0; i<m; i++) {      System.out.println(Arrays.toString(young[i]));    }  }  static void init(int[][] young) {    for(int i=0; i<4; i++) {      for(int j=0; j<4; j++) {        young[i][j] = Integer.MAX_VALUE;      }    }    young[0][0] = 2;    young[0][1] = 3;    young[0][2] = 9;    young[0][3] = 12;    young[1][0] = 4;    young[1][1] = 8;    young[1][2] = 14;    young[2][0] = 5;    young[2][1] = 16;    youngPrint(young, 4);  }/**e. Using no other sorting method as a subroutine, show how to use an n*n Young tableau to sort n^2 numbers in O(n^3) time. User extractMin to get first with O(n+n), then get n^2 sorted numbers with O(n^3)  */  public static void sortNumbers(int[][] young) {  }}

References:

http://blog.csdn.net/mishifangxiangdefeng/article/details/7976452

http://blog.csdn.net/v_JULY_v/article/details/7085669

http://blog.csdn.net/huangxy10/article/details/8017765


0 0
原创粉丝点击