A M x N Young tableau is an M x 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 infinity, which indicates an empty entry. Thus, a Young tableau can be used to hold n <= N2 finite numbers.



For example, following matrix is a Young tableau:



In this post, we will see how to

1. Insert operation in Young tableau

To insert an element into non-full Young tableau, the idea is to place the element at bottom-right corner of the tableau and then move it upwards and leftwards to its correct position within the tableau. This is demonstrated below in C++, Java and Python:

C++ #include <iostream> #include <climits> #include <vector> using namespace std; // M x N Young tableau #define M 4 #define N 4 // Recursive function to insert a new element into a non-full MxN Young tableau // The new element is initially placed at bottom right corner of the tableau. // The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] // and recur for the smaller value. void insert(int tableau[][N], int i, int j) { // base case if (i == 0 && j == 0) { return; } // handle separately for the first row if (i == 0) { if (tableau[i][j] < tableau[i][j-1]) { swap(tableau[i][j], tableau[i][j-1]); insert(tableau, i, j - 1); } return; } // handle separately for the first column if (j == 0) { if (tableau[i][j] < tableau[i-1][j]) { swap(tableau[i][j], tableau[i-1][j]); insert(tableau, i - 1, j); } return; } if (tableau[i][j] < tableau[i-1][j]) // go up { swap(tableau[i][j], tableau[i-1][j]); insert(tableau, i - 1, j); } if (tableau[i][j] < tableau[i][j-1]) // go left { swap(tableau[i][j], tableau[i][j-1]); insert(tableau, i, j - 1); } } // Utility function to print a Young tableau void printTableau(int tableau[][N]) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << tableau[i][j] << ' '; } cout << endl; } } // Recursive function to insert a new element into a non-full MxN Young tableau void insert(int tableau[][N], vector<int> &keys) { // initialize the Young tableau by infinity std::fill(*tableau, *tableau + M*N, INT_MAX); // do for each key for (int key: keys) { // check for overflow if (tableau[M-1][N-1] != INT_MAX) { cout << "Young tableau is full. Skipping key " << key << endl; } else { // place key at the bottom right corner of the tableau tableau[M-1][N-1] = key; // move the key to its correct position in the tableau insert(tableau, M-1, N-1); } } } int main() { // construct a M x N Young tableau int tableau[M][N]; // construct a Young tableau from below keys vector<int> keys = { 12, 10, 20, 22, 25, 30, 34, 11, 44, 27, 16, 40, 35, 15, 18, 45 }; insert(tableau, keys); printTableau(tableau); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #include <iostream> #include <climits> #include <vector> using namespace std ; // M x N Young tableau #define M 4 #define N 4 // Recursive function to insert a new element into a non-full MxN Young tableau // The new element is initially placed at bottom right corner of the tableau. // The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] // and recur for the smaller value. void insert ( int tableau [ ] [ N ] , int i , int j ) { // base case if ( i == 0 && j == 0 ) { return ; } // handle separately for the first row if ( i == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j - 1 ] ) ; insert ( tableau , i , j - 1 ) ; } return ; } // handle separately for the first column if ( j == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) { swap ( tableau [ i ] [ j ] , tableau [ i - 1 ] [ j ] ) ; insert ( tableau , i - 1 , j ) ; } return ; } if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) // go up { swap ( tableau [ i ] [ j ] , tableau [ i - 1 ] [ j ] ) ; insert ( tableau , i - 1 , j ) ; } if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) // go left { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j - 1 ] ) ; insert ( tableau , i , j - 1 ) ; } } // Utility function to print a Young tableau void printTableau ( int tableau [ ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { cout << tableau [ i ] [ j ] << ' ' ; } cout << endl ; } } // Recursive function to insert a new element into a non-full MxN Young tableau void insert ( int tableau [ ] [ N ] , vector < int > &keys ) { // initialize the Young tableau by infinity std :: fill ( * tableau , * tableau + M * N , INT_MAX ) ; // do for each key for ( int key : keys ) { // check for overflow if ( tableau [ M - 1 ] [ N - 1 ] != INT_MAX ) { cout << "Young tableau is full. Skipping key " << key << endl ; } else { // place key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key ; // move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) ; } } } int main ( ) { // construct a M x N Young tableau int tableau [ M ] [ N ] ; // construct a Young tableau from below keys vector < int > keys = { 12 , 10 , 20 , 22 , 25 , 30 , 34 , 11 , 44 , 27 , 16 , 40 , 35 , 15 , 18 , 45 } ; insert ( tableau , keys ) ; printTableau ( tableau ) ; return 0 ; } Download Run Code Output:



10 11 12 15

16 18 20 22

25 27 30 34

35 40 44 45

Java import java.util.Arrays; class Main { private static final int M = 4; private static final int N = 4; // Recursive function to insert a new element into a non-full MxN Young tableau. // The new element is initially placed at the bottom right corner of the tableau. // The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] // and recur for the smaller value. public static void insert(int[][] tableau, int i, int j) { // base case if (i == 0 && j == 0) { return; } // handle separately for the first row if (i == 0) { if (tableau[i][j] < tableau[i][j-1]) { // swap tableau[i][j] and tableau[i][j-1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j-1]; tableau[i][j-1] = temp; insert(tableau, i, j - 1); } return; } // handle separately for the first column if (j == 0) { if (tableau[i][j] < tableau[i-1][j]) { // swap tableau[i][j] and tableau[i-1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i-1][j]; tableau[i-1][j] = temp; insert(tableau, i - 1, j); } return; } if (tableau[i][j] < tableau[i-1][j]) // go up { // swap tableau[i][j] and tableau[i-1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i-1][j]; tableau[i-1][j] = temp; insert(tableau, i - 1, j); } if (tableau[i][j] < tableau[i][j-1]) // go left { // swap tableau[i][j] and tableau[i][j-1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j-1]; tableau[i][j-1] = temp; insert(tableau, i, j - 1); } } // Utility function to print a Young tableau public static void printTableau(int tableau[][]) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { System.out.print(tableau[i][j] + " "); } System.out.println(); } } // Recursive function to insert a new element into a non-full MxN Young tableau. public static void insert(int[][] tableau, int[] keys) { // initialize the Young tableau by infinity for (int i = 0; i < M; i++) { Arrays.fill(tableau[i], Integer.MAX_VALUE); } // do for each key for (int key: keys) { // check for overflow if (tableau[M-1][N-1] != Integer.MAX_VALUE) { System.out.print("Young tableau is full. Skipping key " + key); } else { // place key at the bottom right corner of the tableau tableau[M-1][N-1] = key; // move the key to its correct position in the tableau insert(tableau, M-1, N-1); } } } public static void main(String[] args) { // construct a M x N Young tableau int[][] tableau = new int[M][N]; // construct a Young tableau from below keys int[] keys = { 12, 10, 20, 22, 25, 30, 34, 11, 44, 27, 16, 40, 35, 15, 18, 45 }; insert(tableau, keys); printTableau(tableau); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 import java . util . Arrays ; class Main { private static final int M = 4 ; private static final int N = 4 ; // Recursive function to insert a new element into a non-full MxN Young tableau. // The new element is initially placed at the bottom right corner of the tableau. // The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] // and recur for the smaller value. public static void insert ( int [ ] [ ] tableau , int i , int j ) { // base case if ( i == 0 && j == 0 ) { return ; } // handle separately for the first row if ( i == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) { // swap tableau[i][j] and tableau[i][j-1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] ; tableau [ i ] [ j - 1 ] = temp ; insert ( tableau , i , j - 1 ) ; } return ; } // handle separately for the first column if ( j == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) { // swap tableau[i][j] and tableau[i-1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] ; tableau [ i - 1 ] [ j ] = temp ; insert ( tableau , i - 1 , j ) ; } return ; } if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) // go up { // swap tableau[i][j] and tableau[i-1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] ; tableau [ i - 1 ] [ j ] = temp ; insert ( tableau , i - 1 , j ) ; } if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) // go left { // swap tableau[i][j] and tableau[i][j-1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] ; tableau [ i ] [ j - 1 ] = temp ; insert ( tableau , i , j - 1 ) ; } } // Utility function to print a Young tableau public static void printTableau ( int tableau [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { System . out . print ( tableau [ i ] [ j ] + " " ) ; } System . out . println ( ) ; } } // Recursive function to insert a new element into a non-full MxN Young tableau. public static void insert ( int [ ] [ ] tableau , int [ ] keys ) { // initialize the Young tableau by infinity for ( int i = 0 ; i < M ; i ++ ) { Arrays . fill ( tableau [ i ] , Integer . MAX_VALUE ) ; } // do for each key for ( int key : keys ) { // check for overflow if ( tableau [ M - 1 ] [ N - 1 ] != Integer . MAX_VALUE ) { System . out . print ( "Young tableau is full. Skipping key " + key ) ; } else { // place key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key ; // move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) ; } } } public static void main ( String [ ] args ) { // construct a M x N Young tableau int [ ] [ ] tableau = new int [ M ] [ N ] ; // construct a Young tableau from below keys int [ ] keys = { 12 , 10 , 20 , 22 , 25 , 30 , 34 , 11 , 44 , 27 , 16 , 40 , 35 , 15 , 18 , 45 } ; insert ( tableau , keys ) ; printTableau ( tableau ) ; } } Download Run Code Output:



10 11 12 15

16 18 20 22

25 27 30 34

35 40 44 45

Python # Recursive function to insert an element into a non-full MxN Young tableau. # The element is initially placed at the bottom right corner of the tableau. # The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] # and recur for the smaller value. def insert(tableau, i, j): # base case if i == 0 and j == 0: return # handle separately for the first row if i == 0: if tableau[i][j] < tableau[i][j - 1]: # swap tableau[i][j] and tableau[i][j-1] temp = tableau[i][j] tableau[i][j] = tableau[i][j - 1] tableau[i][j - 1] = temp insert(tableau, i, j - 1) return # handle separately for the first column if j == 0: if tableau[i][j] < tableau[i - 1][j]: # swap tableau[i][j] and tableau[i-1][j] temp = tableau[i][j] tableau[i][j] = tableau[i - 1][j] tableau[i - 1][j] = temp insert(tableau, i - 1, j) return if tableau[i][j] < tableau[i - 1][j]: # go up # swap tableau[i][j] and tableau[i-1][j] temp = tableau[i][j] tableau[i][j] = tableau[i - 1][j] tableau[i - 1][j] = temp insert(tableau, i - 1, j) if tableau[i][j] < tableau[i][j - 1]: # go left # swap tableau[i][j] and tableau[i][j-1] temp = tableau[i][j] tableau[i][j] = tableau[i][j - 1] tableau[i][j - 1] = temp insert(tableau, i, j - 1) # Utility function to print a Young tableau def printTableau(tableau): for i in range(M): for j in range(N): print(tableau[i][j], end=' ') print() # Recursive function to insert an element into a non-full MxN Young tableau. def insertKeys(tableau, keys): # do for each key for key in keys: # check for overflow if tableau[M - 1][N - 1] != float('inf'): print("Young tableau is full. Skipping key", key) else: # place key at the bottom right corner of the tableau tableau[M - 1][N - 1] = key # move the key to its correct position in the tableau insert(tableau, M - 1, N - 1) if __name__ == '__main__': # construct a M x N Young tableau M = N = 4 # initialize the Young tableau by infinity tableau = [[float('inf') for x in range(N)] for y in range(M)] # construct a Young tableau from below keys keys = [12, 10, 20, 22, 25, 30, 34, 11, 44, 27, 16, 40, 35, 15, 18, 45] insertKeys(tableau, keys) printTableau(tableau) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 # Recursive function to insert an element into a non-full MxN Young tableau. # The element is initially placed at the bottom right corner of the tableau. # The function works by swapping the smaller of [i-1,j] and [i,j-1] with [i,j] # and recur for the smaller value. def insert ( tableau , i , j ) : # base case if i == 0 and j == 0 : return # handle separately for the first row if i == 0 : if tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] : # swap tableau[i][j] and tableau[i][j-1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] tableau [ i ] [ j - 1 ] = temp insert ( tableau , i , j - 1 ) return # handle separately for the first column if j == 0 : if tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] : # swap tableau[i][j] and tableau[i-1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] tableau [ i - 1 ] [ j ] = temp insert ( tableau , i - 1 , j ) return if tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] : # go up # swap tableau[i][j] and tableau[i-1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] tableau [ i - 1 ] [ j ] = temp insert ( tableau , i - 1 , j ) if tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] : # go left # swap tableau[i][j] and tableau[i][j-1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] tableau [ i ] [ j - 1 ] = temp insert ( tableau , i , j - 1 ) # Utility function to print a Young tableau def printTableau ( tableau ) : for i in range ( M ) : for j in range ( N ) : print ( tableau [ i ] [ j ] , end = ' ' ) print ( ) # Recursive function to insert an element into a non-full MxN Young tableau. def insertKeys ( tableau , keys ) : # do for each key for key in keys : # check for overflow if tableau [ M - 1 ] [ N - 1 ] != float ( 'inf' ) : print ( "Young tableau is full. Skipping key" , key ) else : # place key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key # move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) if __name__ == '__main__' : # construct a M x N Young tableau M = N = 4 # initialize the Young tableau by infinity tableau = [ [ float ( 'inf' ) for x in range ( N ) ] for y in range ( M ) ] # construct a Young tableau from below keys keys = [ 12 , 10 , 20 , 22 , 25 , 30 , 34 , 11 , 44 , 27 , 16 , 40 , 35 , 15 , 18 , 45 ] insertKeys ( tableau , keys ) printTableau ( tableau ) Download Run Code Output:



10 11 12 15

16 18 20 22

25 27 30 34

35 40 44 45







The time complexity for the insert operation is O(M + N) while the overall time complexity to construct a M x N Young tableau from given list of elements is O(M*N*(M + N)). The auxiliary space used by the program is O(M + N) for call stack.

2. Search operation in Young tableau

Naive solution is to traverse the complete tableau to search for the given value. The problem with this approach is that it would require O(M * N) time for searching in a M x N Young tableau.

Can we do better?

The idea is to take advantage of the fact that a Young tableau is row-wise and column-wise sorted matrix. We start from the top-rightmost corner of the tableau and compare the current element with the one we’re looking for and accordingly move left or down:

If current element is less than the key, we increment row index (move to the next row)

If current element is more than the key, we decrement column index (move to the previous column)

If current element is equal to the key, we return true as we have found the element. If we end up in a row or column beyond the tableau boundary, we terminate the search and return false.



The algorithm can be implemented as follows in C++, Java and Python:

C++ #include <iostream> #include <vector> #include <climits> using namespace std; // M x N Young tableau #define M 4 #define N 4 // A utility function to search an element in a Young tableau. bool search(int tableau[][N], int key) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0, j = N - 1; // run till tableau boundary is reached while (i < M && j >= 0) { // if current element is less than the key, increment the row index if (tableau[i][j] < key) i++; // if current element is more than the key, decrement the column index else if (tableau[i][j] > key) j--; // current element is equal to the key else return true; } return false; } int main() { // M x N Young tableau int tableau[M][N] = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; vector<int> keys = { 20, 23, 25, 29 }; // do for each key for (int key: keys) { if (search(tableau, key)) cout << "Key " << key << " found in tableau" << endl; } return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <iostream> #include <vector> #include <climits> using namespace std ; // M x N Young tableau #define M 4 #define N 4 // A utility function to search an element in a Young tableau. bool search ( int tableau [ ] [ N ] , int key ) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0 , j = N - 1 ; // run till tableau boundary is reached while ( i < M && j >= 0 ) { // if current element is less than the key, increment the row index if ( tableau [ i ] [ j ] < key ) i ++ ; // if current element is more than the key, decrement the column index else if ( tableau [ i ] [ j ] > key ) j -- ; // current element is equal to the key else return true ; } return false ; } int main ( ) { // M x N Young tableau int tableau [ M ] [ N ] = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; vector < int > keys = { 20 , 23 , 25 , 29 } ; // do for each key for ( int key : keys ) { if ( search ( tableau , key ) ) cout << "Key " << key << " found in tableau" << endl ; } return 0 ; } Download Run Code Output:



Key 20 found in tableau

Key 25 found in tableau

Java class Main { // A utility function to search an element in a Young Tableau. public static boolean search(int[][] tableau, int key) { // start from the top-rightmost cell of matrix i.e. (0, N-1) cell int i = 0, j = tableau[0].length - 1; // run till matrix boundary is reached while (i < tableau.length && j >= 0) { // if current element is less than the key, increment the row index if (tableau[i][j] < key) { i++; } // if current element is more than the key, decrement the column index else if (tableau[i][j] > key) { j--; } // current element is equal to the key else { return true; } } return false; } public static void main(String[] args) { // M x N Young tableau int[][] tableau = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; int[] keys = { 20, 23, 25, 29 }; // do for each key for (int key: keys) { if (search(tableau, key)) { System.out.println("Key " + key + " found in tableau"); } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 class Main { // A utility function to search an element in a Young Tableau. public static boolean search ( int [ ] [ ] tableau , int key ) { // start from the top-rightmost cell of matrix i.e. (0, N-1) cell int i = 0 , j = tableau [ 0 ] . length - 1 ; // run till matrix boundary is reached while ( i < tableau . length && j >= 0 ) { // if current element is less than the key, increment the row index if ( tableau [ i ] [ j ] < key ) { i ++ ; } // if current element is more than the key, decrement the column index else if ( tableau [ i ] [ j ] > key ) { j -- ; } // current element is equal to the key else { return true ; } } return false ; } public static void main ( String [ ] args ) { // M x N Young tableau int [ ] [ ] tableau = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; int [ ] keys = { 20 , 23 , 25 , 29 } ; // do for each key for ( int key : keys ) { if ( search ( tableau , key ) ) { System . out . println ( "Key " + key + " found in tableau" ) ; } } } } Download Run Code Output:



Key 20 found in tableau

Key 25 found in tableau

Python # A utility function to search an element in a Young Tableau. def search(tableau, key): # start from the top-rightmost cell of matrix i.e. (0, N-1) cell i = 0 j = len(tableau[0]) - 1 # run till matrix boundary is reached while i < len(tableau) and j >= 0: # if current element is less than the key, increment the row index if tableau[i][j] < key: i = i + 1 # if current element is more than the key, decrement the column index elif tableau[i][j] > key: j = j - 1 # current element is equal to the key else: return True return False if __name__ == '__main__': # M x N Young tableau tableau = [ [10, 12, 15, 16], [11, 18, 20, 25], [22, 27, 30, 35], [34, 40, 44, 45] ] keys = [20, 23, 25, 29] # do for each key for key in keys: if search(tableau, key): print("Key", key, "found in tableau") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # A utility function to search an element in a Young Tableau. def search ( tableau , key ) : # start from the top-rightmost cell of matrix i.e. (0, N-1) cell i = 0 j = len ( tableau [ 0 ] ) - 1 # run till matrix boundary is reached while i < len ( tableau ) and j >= 0 : # if current element is less than the key, increment the row index if tableau [ i ] [ j ] < key : i = i + 1 # if current element is more than the key, decrement the column index elif tableau [ i ] [ j ] > key : j = j - 1 # current element is equal to the key else : return True return False if __name__ == '__main__' : # M x N Young tableau tableau = [ [ 10 , 12 , 15 , 16 ] , [ 11 , 18 , 20 , 25 ] , [ 22 , 27 , 30 , 35 ] , [ 34 , 40 , 44 , 45 ] ] keys = [ 20 , 23 , 25 , 29 ] # do for each key for key in keys : if search ( tableau , key ) : print ( "Key" , key , "found in tableau" ) Download Run Code Output:



Key 20 found in tableau

Key 25 found in tableau







Time complexity of search operation is O(M + N) and auxiliary space used by the program is O(1).

3. Extract-Min operation in Young tableau

The minimum element is present at the position (0,0) in tableau. To remove an element (i,j) from a tableau, we simply replace the cell value by infinity and return the element. This causes the Young tableau to become unstable and the sub-matrix formed by (i, j), (i, N-1), (M-1, j), and (M-1, N-1) violates the Young tableau property (the sub-matrix is not row-wise sorted and column-wise sorted anymore).

Young tableau is similar to a heap where each cell (i,j) can be considered as the root node. The bottom cell (i+1,j) and right cell (i,j+1) of a cell (i,j) can be considered as the left and right child of the root node.

The idea is to perform a procedure similar to HEAPIFY-DOWN to restore the Young tableau property. If (i,j) is larger than its neighbors, we swap the smaller of (i+1,j) and (i,j+1) with (i,j). This restores the tableau property for cell (i,j) but reduces the problem for cell (i+1,j) or cell (i,j+1).

The algorithm can be implemented recursively as follows in C++, Java and Python –

C++ #include <iostream> #include <climits> using namespace std; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau. // An infinite value is initially placed at the first cell (0,0) of the tableau. // The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] // and recur for the smaller value. void fixTableau(int tableau[][N], int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : INT_MAX; int right = (j + 1 < N) ? tableau[i][j + 1] : INT_MAX; if (bottom < right) // go down { swap(tableau[i][j], tableau[i + 1][j]); fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { swap(tableau[i][j], tableau[i][j + 1]); fixTableau(tableau, i, j + 1); } } // Function to extract next minimum element from the Young tableau int extractMin(int tableau[][N]) { // the first cell of tableau stores the minimum element int min = tableau[0][0]; // make the first element as infinity tableau[0][0] = INT_MAX; // fix the young tableau property fixTableau(tableau, 0, 0); return min; } int main() { // M x N Young tableau int tableau[M][N] = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; // repeat M*N times for (int i = 0; i < M*N; i++) { cout << extractMin(tableau) << ' '; } return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <iostream> #include <climits> using namespace std ; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau. // An infinite value is initially placed at the first cell (0,0) of the tableau. // The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] // and recur for the smaller value. void fixTableau ( int tableau [ ] [ N ] , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : INT_MAX ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : INT_MAX ; if ( bottom < right ) // go down { swap ( tableau [ i ] [ j ] , tableau [ i + 1 ] [ j ] ) ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j + 1 ] ) ; fixTableau ( tableau , i , j + 1 ) ; } } // Function to extract next minimum element from the Young tableau int extractMin ( int tableau [ ] [ N ] ) { // the first cell of tableau stores the minimum element int min = tableau [ 0 ] [ 0 ] ; // make the first element as infinity tableau [ 0 ] [ 0 ] = INT_MAX ; // fix the young tableau property fixTableau ( tableau , 0 , 0 ) ; return min ; } int main ( ) { // M x N Young tableau int tableau [ M ] [ N ] = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; // repeat M*N times for ( int i = 0 ; i < M * N ; i ++ ) { cout << extractMin ( tableau ) << ' ' ; } return 0 ; } Download Run Code Output:



10 11 12 15 16 18 20 22 25 27 30 34 35 40 44 45

Java class Main { // M x N matrix private static int M, N; // Recursive function to fix the tableau property in a MxN Young tableau. // An infinite value is initially placed at the first cell (0,0) of the tableau. // The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] // and recur for the smaller value. public static void fixTableau(int[][] tableau, int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : Integer.MAX_VALUE; int right = (j + 1 < N) ? tableau[i][j + 1] : Integer.MAX_VALUE; if (bottom < right) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i + 1][j]; tableau[i + 1][j] = temp; fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j + 1]; tableau[i][j + 1] = temp; fixTableau(tableau, i, j + 1); } } // Function to extract next minimum element from the Young tableau public static int extractMin(int[][] tableau) { // the first cell of tableau stores the minimum element int min = tableau[0][0]; // make the first element as infinity tableau[0][0] = Integer.MAX_VALUE; // fix the young tableau property fixTableau(tableau, 0, 0); return min; } public static void main(String[] args) { // M x N Young tableau int[][] tableau = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; M = tableau.length; N = tableau[0].length; // repeat M*N times for (int i = 0; i < M*N; i++) { System.out.print(extractMin(tableau) + " "); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 class Main { // M x N matrix private static int M , N ; // Recursive function to fix the tableau property in a MxN Young tableau. // An infinite value is initially placed at the first cell (0,0) of the tableau. // The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] // and recur for the smaller value. public static void fixTableau ( int [ ] [ ] tableau , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : Integer . MAX_VALUE ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : Integer . MAX_VALUE ; if ( bottom < right ) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] ; tableau [ i + 1 ] [ j ] = temp ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] ; tableau [ i ] [ j + 1 ] = temp ; fixTableau ( tableau , i , j + 1 ) ; } } // Function to extract next minimum element from the Young tableau public static int extractMin ( int [ ] [ ] tableau ) { // the first cell of tableau stores the minimum element int min = tableau [ 0 ] [ 0 ] ; // make the first element as infinity tableau [ 0 ] [ 0 ] = Integer . MAX_VALUE ; // fix the young tableau property fixTableau ( tableau , 0 , 0 ) ; return min ; } public static void main ( String [ ] args ) { // M x N Young tableau int [ ] [ ] tableau = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; M = tableau . length ; N = tableau [ 0 ] . length ; // repeat M*N times for ( int i = 0 ; i < M * N ; i ++ ) { System . out . print ( extractMin ( tableau ) + " " ) ; } } } Download Run Code Output:



10 11 12 15 16 18 20 22 25 27 30 34 35 40 44 45

Python # Recursive function to fix the tableau property in a MxN Young tableau. # An infinite value is initially placed at the first cell (0,0) of the tableau. # The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] # and recur for the smaller value. def fixTableau(tableau, i=0, j=0): # get the values present at the bottom and right cell of the current cell bottom = tableau[i + 1][j] if (i + 1 < M) else float('inf') right = tableau[i][j + 1] if (j + 1 < N) else float('inf') if bottom < right: # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau[i][j] tableau[i][j] = tableau[i + 1][j] tableau[i + 1][j] = temp fixTableau(tableau, i + 1, j) if bottom > right: # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau[i][j] tableau[i][j] = tableau[i][j + 1] tableau[i][j + 1] = temp fixTableau(tableau, i, j + 1) # Function to extract next minimum element from the Young tableau def extractMin(tableau): # the first cell of tableau stores the minimum element min = tableau[0][0] # make the first element as infinity tableau[0][0] = float('inf') # fix the young tableau property fixTableau(tableau) return min if __name__ == '__main__': # M x N Young tableau tableau = [ [10, 12, 15, 16], [11, 18, 20, 25], [22, 27, 30, 35], [34, 40, 44, 45] ] (M, N) = (len(tableau), len(tableau[0])) # repeat M*N times for i in range(M * N): print(extractMin(tableau), end=' ') 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # Recursive function to fix the tableau property in a MxN Young tableau. # An infinite value is initially placed at the first cell (0,0) of the tableau. # The function works by swapping the smaller of [i+1,j] and [i,j+1] with [i,j] # and recur for the smaller value. def fixTableau ( tableau , i = 0 , j = 0 ) : # get the values present at the bottom and right cell of the current cell bottom = tableau [ i + 1 ] [ j ] if ( i + 1 < M ) else float ( 'inf' ) right = tableau [ i ] [ j + 1 ] if ( j + 1 < N ) else float ( 'inf' ) if bottom < right : # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] tableau [ i + 1 ] [ j ] = temp fixTableau ( tableau , i + 1 , j ) if bottom > right : # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] tableau [ i ] [ j + 1 ] = temp fixTableau ( tableau , i , j + 1 ) # Function to extract next minimum element from the Young tableau def extractMin ( tableau ) : # the first cell of tableau stores the minimum element min = tableau [ 0 ] [ 0 ] # make the first element as infinity tableau [ 0 ] [ 0 ] = float ( 'inf' ) # fix the young tableau property fixTableau ( tableau ) return min if __name__ == '__main__' : # M x N Young tableau tableau = [ [ 10 , 12 , 15 , 16 ] , [ 11 , 18 , 20 , 25 ] , [ 22 , 27 , 30 , 35 ] , [ 34 , 40 , 44 , 45 ] ] ( M , N ) = ( len ( tableau ) , len ( tableau [ 0 ] ) ) # repeat M*N times for i in range ( M * N ) : print ( extractMin ( tableau ) , end = ' ' ) Download Run Code Output:



10 11 12 15 16 18 20 22 25 27 30 34 35 40 44 45







The fixTableau() routine takes a M x N matrix and breaks it into either a (M-1) x N or a M x (N-1) matrix. So, the recurrence relation is:

T(M, N) = T(M-1, N) + T(M, N-1)

So the overall time complexity of Extract-Min operation on a Young tableau is O(M + N).

4. Delete operation in Young tableau

The idea is very simple and inspired from Extract-Min operation on the Young tableau. To delete an element present at cell (i, j), make it as infinity and then fix the young tableau property of sub-matrix formed by (i, j), (i, N-1), (M-1, j), and (M-1, N-1).



The algorithm can be implemented as follows in C++, Java and Python:

C++ #include <iostream> #include <algorithm> #include <climits> using namespace std; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau void fixTableau(int tableau[][N], int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : INT_MAX; int right = (j + 1 < N) ? tableau[i][j + 1] : INT_MAX; if (bottom < right) // go down { swap(tableau[i][j], tableau[i + 1][j]); fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { swap(tableau[i][j], tableau[i][j + 1]); fixTableau(tableau, i, j + 1); } } // Function to delete given element from the Young tableau void deletion(int tableau[][N], int i, int j) { // to delete the element at cell (i, j), make it as infinity tableau[i][j] = INT_MAX; // fix the young tableau property fixTableau(tableau, i, j); } // Utility function to print a Young tableau void printTableau(int tableau[][N]) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << tableau[i][j] << ' '; } cout << endl; } } int main() { // M x N Young tableau int tableau[M][N] = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; deletion(tableau, 1, 2); // delete 20 printTableau(tableau); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <iostream> #include <algorithm> #include <climits> using namespace std ; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau void fixTableau ( int tableau [ ] [ N ] , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : INT_MAX ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : INT_MAX ; if ( bottom < right ) // go down { swap ( tableau [ i ] [ j ] , tableau [ i + 1 ] [ j ] ) ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j + 1 ] ) ; fixTableau ( tableau , i , j + 1 ) ; } } // Function to delete given element from the Young tableau void deletion ( int tableau [ ] [ N ] , int i , int j ) { // to delete the element at cell (i, j), make it as infinity tableau [ i ] [ j ] = INT_MAX ; // fix the young tableau property fixTableau ( tableau , i , j ) ; } // Utility function to print a Young tableau void printTableau ( int tableau [ ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { cout << tableau [ i ] [ j ] << ' ' ; } cout << endl ; } } int main ( ) { // M x N Young tableau int tableau [ M ] [ N ] = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; deletion ( tableau , 1 , 2 ) ; // delete 20 printTableau ( tableau ) ; return 0 ; } Download Run Code Output:



10 12 15 16

11 18 25 35

22 27 30 45

34 40 44 2147483647

Java import java.util.Arrays; class Main { // M x N matrix private static int M, N; // Recursive function to fix the tableau property in a MxN Young tableau public static void fixTableau(int[][] tableau, int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : Integer.MAX_VALUE; int right = (j + 1 < N) ? tableau[i][j + 1] : Integer.MAX_VALUE; if (bottom < right) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i + 1][j]; tableau[i + 1][j] = temp; fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j + 1]; tableau[i][j + 1] = temp; fixTableau(tableau, i, j + 1); } } // Function to delete given element from the Young tableau public static void deletion(int[][] tableau, int i, int j) { // to delete the element at cell (i, j), make it as infinity tableau[i][j] = Integer.MAX_VALUE; // fix the young tableau property fixTableau(tableau, i, j); } public static void main(String[] args) { // M x N Young tableau int[][] tableau = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; M = tableau.length; N = tableau[0].length; deletion(tableau, 1, 2); // delete 20 // print Young tableau for (var r: tableau) { System.out.println(Arrays.toString(r)); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 import java . util . Arrays ; class Main { // M x N matrix private static int M , N ; // Recursive function to fix the tableau property in a MxN Young tableau public static void fixTableau ( int [ ] [ ] tableau , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : Integer . MAX_VALUE ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : Integer . MAX_VALUE ; if ( bottom < right ) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] ; tableau [ i + 1 ] [ j ] = temp ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] ; tableau [ i ] [ j + 1 ] = temp ; fixTableau ( tableau , i , j + 1 ) ; } } // Function to delete given element from the Young tableau public static void deletion ( int [ ] [ ] tableau , int i , int j ) { // to delete the element at cell (i, j), make it as infinity tableau [ i ] [ j ] = Integer . MAX_VALUE ; // fix the young tableau property fixTableau ( tableau , i , j ) ; } public static void main ( String [ ] args ) { // M x N Young tableau int [ ] [ ] tableau = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; M = tableau . length ; N = tableau [ 0 ] . length ; deletion ( tableau , 1 , 2 ) ; // delete 20 // print Young tableau for ( var r : tableau ) { System . out . println ( Arrays . toString ( r ) ) ; } } } Download Run Code Output:



[10, 12, 15, 16]

[11, 18, 25, 35]

[22, 27, 30, 45]

[34, 40, 44, 2147483647]

Python # Recursive function to fix the tableau property in a MxN Young tableau def fixTableau(tableau, i, j): # get the values present at the bottom and right cell of the current cell bottom = tableau[i + 1][j] if (i + 1 < M) else float('inf') right = tableau[i][j + 1] if (j + 1 < N) else float('inf') if bottom < right: # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau[i][j] tableau[i][j] = tableau[i + 1][j] tableau[i + 1][j] = temp fixTableau(tableau, i + 1, j) if bottom > right: # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau[i][j] tableau[i][j] = tableau[i][j + 1] tableau[i][j + 1] = temp fixTableau(tableau, i, j + 1) # Function to delete given element from the Young tableau def deletion(tableau, i, j): # to delete the element at cell (i, j), make it as infinity tableau[i][j] = float('inf') # fix the young tableau property fixTableau(tableau, i, j) # Utility function to print a Young tableau def printTableau(tableau): for i in range(M): print(tableau[i]) if __name__ == '__main__': # M x N Young tableau tableau = [ [10, 12, 15, 16], [11, 18, 20, 25], [22, 27, 30, 35], [34, 40, 44, 45] ] (M, N) = (len(tableau), len(tableau[0])) deletion(tableau, 1, 2) # delete 20 printTableau(tableau) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # Recursive function to fix the tableau property in a MxN Young tableau def fixTableau ( tableau , i , j ) : # get the values present at the bottom and right cell of the current cell bottom = tableau [ i + 1 ] [ j ] if ( i + 1 < M ) else float ( 'inf' ) right = tableau [ i ] [ j + 1 ] if ( j + 1 < N ) else float ( 'inf' ) if bottom < right : # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] tableau [ i + 1 ] [ j ] = temp fixTableau ( tableau , i + 1 , j ) if bottom > right : # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] tableau [ i ] [ j + 1 ] = temp fixTableau ( tableau , i , j + 1 ) # Function to delete given element from the Young tableau def deletion ( tableau , i , j ) : # to delete the element at cell (i, j), make it as infinity tableau [ i ] [ j ] = float ( 'inf' ) # fix the young tableau property fixTableau ( tableau , i , j ) # Utility function to print a Young tableau def printTableau ( tableau ) : for i in range ( M ) : print ( tableau [ i ] ) if __name__ == '__main__' : # M x N Young tableau tableau = [ [ 10 , 12 , 15 , 16 ] , [ 11 , 18 , 20 , 25 ] , [ 22 , 27 , 30 , 35 ] , [ 34 , 40 , 44 , 45 ] ] ( M , N ) = ( len ( tableau ) , len ( tableau [ 0 ] ) ) deletion ( tableau , 1 , 2 ) # delete 20 printTableau ( tableau ) Download Run Code Output:



[10, 12, 15, 16]

[11, 18, 25, 35]

[22, 27, 30, 45]

[34, 40, 44, inf]







The time complexity of delete operation is O(M + N) and auxiliary space used by the program is O(M + N) for call stack.

5. Replace operation in Young tableau

To replace an element x with the element y in a Young tableau, we start by searching x‘s position (i, j) in the tableau. Then we replace the element at cell (i, j) by infinity, and fix the young tableau property. Finally, we place the element y at the bottom right corner of the tableau and move the key to its correct position in the tableau.



The algorithm can be implemented as follows in C++, Java and Python:

C++ #include <iostream> #include <algorithm> #include <climits> using namespace std; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau void fixTableau(int tableau[][N], int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : INT_MAX; int right = (j + 1 < N) ? tableau[i][j + 1] : INT_MAX; if (bottom < right) // go down { swap(tableau[i][j], tableau[i + 1][j]); fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { swap(tableau[i][j], tableau[i][j + 1]); fixTableau(tableau, i, j + 1); } } // Recursive function to insert a new element into a non-full MxN Young tableau void insert(int tableau[][N], int i, int j) { // base case if (i == 0 && j == 0) { return; } // handle separately for the first row if (i == 0) { if (tableau[i][j] < tableau[i][j-1]) { swap(tableau[i][j], tableau[i][j-1]); insert(tableau, i, j - 1); } return; } // handle separately for the first column if (j == 0) { if (tableau[i][j] < tableau[i-1][j]) { swap(tableau[i][j], tableau[i-1][j]); insert(tableau, i - 1, j); } return; } if (tableau[i][j] < tableau[i-1][j]) // go up { swap(tableau[i][j], tableau[i-1][j]); insert(tableau, i - 1, j); } if (tableau[i][j] < tableau[i][j-1]) // go left { swap(tableau[i][j], tableau[i][j-1]); insert(tableau, i, j - 1); } } // Function to replace a given element in the Young tableau void replace(int tableau[][N], int i, int j, int key) { // delete element at cell (i, j) tableau[i][j] = INT_MAX; // fix the young tableau property fixTableau(tableau, i, j); // place the given key at the bottom right corner of the tableau tableau[M-1][N-1] = key; // move the key to its correct position in the tableau insert(tableau, M-1, N-1); } // A utility function to search an element in a Young tableau. bool search(int tableau[][N], int key, int value) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0, j = N - 1; // run till tableau boundary is reached while (i < M && j >= 0) { // if current element is less than the key, increment the row index if (tableau[i][j] < key) i++; // if current element is more than the key, decrement the column index else if (tableau[i][j] > key) j--; // current element is equal to the key else { replace(tableau, i, j, value); return true; } } return false; } // Utility function to print a Young tableau void printTableau(int tableau[][N]) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << tableau[i][j] << ' '; } cout << endl; } } int main() { // M x N Young tableau int tableau[M][N] = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; search(tableau, 20, 14); // replace 20 by 14 printTableau(tableau); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 #include <iostream> #include <algorithm> #include <climits> using namespace std ; // M x N Young tableau #define M 4 #define N 4 // Recursive function to fix the tableau property in a MxN Young tableau void fixTableau ( int tableau [ ] [ N ] , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : INT_MAX ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : INT_MAX ; if ( bottom < right ) // go down { swap ( tableau [ i ] [ j ] , tableau [ i + 1 ] [ j ] ) ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j + 1 ] ) ; fixTableau ( tableau , i , j + 1 ) ; } } // Recursive function to insert a new element into a non-full MxN Young tableau void insert ( int tableau [ ] [ N ] , int i , int j ) { // base case if ( i == 0 && j == 0 ) { return ; } // handle separately for the first row if ( i == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j - 1 ] ) ; insert ( tableau , i , j - 1 ) ; } return ; } // handle separately for the first column if ( j == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) { swap ( tableau [ i ] [ j ] , tableau [ i - 1 ] [ j ] ) ; insert ( tableau , i - 1 , j ) ; } return ; } if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) // go up { swap ( tableau [ i ] [ j ] , tableau [ i - 1 ] [ j ] ) ; insert ( tableau , i - 1 , j ) ; } if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) // go left { swap ( tableau [ i ] [ j ] , tableau [ i ] [ j - 1 ] ) ; insert ( tableau , i , j - 1 ) ; } } // Function to replace a given element in the Young tableau void replace ( int tableau [ ] [ N ] , int i , int j , int key ) { // delete element at cell (i, j) tableau [ i ] [ j ] = INT_MAX ; // fix the young tableau property fixTableau ( tableau , i , j ) ; // place the given key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key ; // move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) ; } // A utility function to search an element in a Young tableau. bool search ( int tableau [ ] [ N ] , int key , int value ) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0 , j = N - 1 ; // run till tableau boundary is reached while ( i < M && j >= 0 ) { // if current element is less than the key, increment the row index if ( tableau [ i ] [ j ] < key ) i ++ ; // if current element is more than the key, decrement the column index else if ( tableau [ i ] [ j ] > key ) j -- ; // current element is equal to the key else { replace ( tableau , i , j , value ) ; return true ; } } return false ; } // Utility function to print a Young tableau void printTableau ( int tableau [ ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { cout << tableau [ i ] [ j ] << ' ' ; } cout << endl ; } } int main ( ) { // M x N Young tableau int tableau [ M ] [ N ] = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; search ( tableau , 20 , 14 ) ; // replace 20 by 14 printTableau ( tableau ) ; return 0 ; } Download Run Code Output:



10 12 14 15

11 16 18 25

22 27 30 35

34 40 44 45

Java import java.util.Arrays; class Main { private static int M, N; // Recursive function to fix the tableau property in a MxN Young tableau public static void fixTableau(int[][] tableau, int i, int j) { // get the values present at the bottom and right cell of the current cell int bottom = (i + 1 < M) ? tableau[i + 1][j] : Integer.MAX_VALUE; int right = (j + 1 < N) ? tableau[i][j + 1] : Integer.MAX_VALUE; if (bottom < right) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i + 1][j]; tableau[i + 1][j] = temp; fixTableau(tableau, i + 1, j); } if (bottom > right) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j + 1]; tableau[i][j + 1] = temp; fixTableau(tableau, i, j + 1); } } // Recursive function to insert a new element into a non-full MxN Young tableau. public static void insert(int[][] tableau, int i, int j) { // base case if (i == 0 && j == 0) { return; } // handle separately for the first row if (i == 0) { if (tableau[i][j] < tableau[i][j-1]) { // swap tableau[i][j] with tableau[i][j - 1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j - 1]; tableau[i][j - 1] = temp; insert(tableau, i, j - 1); } return; } // handle separately for the first column if (j == 0) { if (tableau[i][j] < tableau[i-1][j]) { // swap tableau[i][j] with tableau[i-1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i-1][j]; tableau[i-1][j] = temp; insert(tableau, i - 1, j); } return; } if (tableau[i][j] < tableau[i-1][j]) // go up { // swap tableau[i][j] with tableau[i-1][j] int temp = tableau[i][j]; tableau[i][j] = tableau[i-1][j]; tableau[i-1][j] = temp; insert(tableau, i - 1, j); } if (tableau[i][j] < tableau[i][j-1]) // go left { // swap tableau[i][j] with tableau[i][j - 1] int temp = tableau[i][j]; tableau[i][j] = tableau[i][j - 1]; tableau[i][j - 1] = temp; insert(tableau, i, j - 1); } } // Function to replace a given element in the Young tableau public static void replace(int[][] tableau, int i, int j, int key) { // delete element at cell (i, j) tableau[i][j] = Integer.MAX_VALUE; // fix the young tableau property fixTableau(tableau, i, j); // place the given key at the bottom right corner of the tableau tableau[M-1][N-1] = key; // move the key to its correct position in the tableau insert(tableau, M-1, N-1); } // A utility function to search an element in a Young tableau. public static boolean search(int[][] tableau, int key, int value) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0, j = N - 1; // run till tableau boundary is reached while (i < M && j >= 0) { // if current element is less than the key, increment the row index if (tableau[i][j] < key) { i++; } // if current element is more than the key, decrement the column index else if (tableau[i][j] > key) { j--; } // current element is equal to the key else { replace(tableau, i, j, value); return true; } } return false; } public static void main(String[] args) { // M x N Young tableau int[][] tableau = { { 10, 12, 15, 16 }, { 11, 18, 20, 25 }, { 22, 27, 30, 35 }, { 34, 40, 44, 45 } }; M = tableau.length; N = tableau[0].length; search(tableau, 20, 14); // replace 20 by 14 // print Young tableau for (var r: tableau) { System.out.println(Arrays.toString(r)); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 import java . util . Arrays ; class Main { private static int M , N ; // Recursive function to fix the tableau property in a MxN Young tableau public static void fixTableau ( int [ ] [ ] tableau , int i , int j ) { // get the values present at the bottom and right cell of the current cell int bottom = ( i + 1 < M ) ? tableau [ i + 1 ] [ j ] : Integer . MAX_VALUE ; int right = ( j + 1 < N ) ? tableau [ i ] [ j + 1 ] : Integer . MAX_VALUE ; if ( bottom < right ) // go down { // swap tableau[i][j] with tableau[i + 1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] ; tableau [ i + 1 ] [ j ] = temp ; fixTableau ( tableau , i + 1 , j ) ; } if ( bottom > right ) // go right { // swap tableau[i][j] with tableau[i][j + 1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] ; tableau [ i ] [ j + 1 ] = temp ; fixTableau ( tableau , i , j + 1 ) ; } } // Recursive function to insert a new element into a non-full MxN Young tableau. public static void insert ( int [ ] [ ] tableau , int i , int j ) { // base case if ( i == 0 && j == 0 ) { return ; } // handle separately for the first row if ( i == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) { // swap tableau[i][j] with tableau[i][j - 1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] ; tableau [ i ] [ j - 1 ] = temp ; insert ( tableau , i , j - 1 ) ; } return ; } // handle separately for the first column if ( j == 0 ) { if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) { // swap tableau[i][j] with tableau[i-1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] ; tableau [ i - 1 ] [ j ] = temp ; insert ( tableau , i - 1 , j ) ; } return ; } if ( tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] ) // go up { // swap tableau[i][j] with tableau[i-1][j] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] ; tableau [ i - 1 ] [ j ] = temp ; insert ( tableau , i - 1 , j ) ; } if ( tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] ) // go left { // swap tableau[i][j] with tableau[i][j - 1] int temp = tableau [ i ] [ j ] ; tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] ; tableau [ i ] [ j - 1 ] = temp ; insert ( tableau , i , j - 1 ) ; } } // Function to replace a given element in the Young tableau public static void replace ( int [ ] [ ] tableau , int i , int j , int key ) { // delete element at cell (i, j) tableau [ i ] [ j ] = Integer . MAX_VALUE ; // fix the young tableau property fixTableau ( tableau , i , j ) ; // place the given key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key ; // move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) ; } // A utility function to search an element in a Young tableau. public static boolean search ( int [ ] [ ] tableau , int key , int value ) { // start from the top-rightmost cell of tableau i.e. (0, N-1) cell int i = 0 , j = N - 1 ; // run till tableau boundary is reached while ( i < M && j >= 0 ) { // if current element is less than the key, increment the row index if ( tableau [ i ] [ j ] < key ) { i ++ ; } // if current element is more than the key, decrement the column index else if ( tableau [ i ] [ j ] > key ) { j -- ; } // current element is equal to the key else { replace ( tableau , i , j , value ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { // M x N Young tableau int [ ] [ ] tableau = { { 10 , 12 , 15 , 16 } , { 11 , 18 , 20 , 25 } , { 22 , 27 , 30 , 35 } , { 34 , 40 , 44 , 45 } } ; M = tableau . length ; N = tableau [ 0 ] . length ; search ( tableau , 20 , 14 ) ; // replace 20 by 14 // print Young tableau for ( var r : tableau ) { System . out . println ( Arrays . toString ( r ) ) ; } } } Download Run Code Output:



[10, 12, 14, 15]

[11, 16, 18, 25]

[22, 27, 30, 35]

[34, 40, 44, 45]

Python # Recursive function to fix the tableau property in a MxN Young tableau def fixTableau(tableau, i, j): # get the values present at the bottom and right cell of the current cell bottom = tableau[i + 1][j] if (i + 1 < M) else float('inf') right = tableau[i][j + 1] if (j + 1 < N) else float('inf') if bottom < right: # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau[i][j] tableau[i][j] = tableau[i + 1][j] tableau[i + 1][j] = temp fixTableau(tableau, i + 1, j) if bottom > right: # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau[i][j] tableau[i][j] = tableau[i][j + 1] tableau[i][j + 1] = temp fixTableau(tableau, i, j + 1) # Recursive function to insert an element into a non-full MxN Young tableau. def insert(tableau, i, j): # base case if i == 0 and j == 0: return # handle separately for the first row if i == 0: if tableau[i][j] < tableau[i][j - 1]: # swap tableau[i][j] with tableau[i][j - 1] temp = tableau[i][j] tableau[i][j] = tableau[i][j - 1] tableau[i][j - 1] = temp insert(tableau, i, j - 1) return # handle separately for the first column if j == 0: if tableau[i][j] < tableau[i - 1][j]: # swap tableau[i][j] with tableau[i-1][j] temp = tableau[i][j] tableau[i][j] = tableau[i - 1][j] tableau[i - 1][j] = temp insert(tableau, i - 1, j) return if tableau[i][j] < tableau[i - 1][j]: # go up # swap tableau[i][j] with tableau[i-1][j] temp = tableau[i][j] tableau[i][j] = tableau[i - 1][j] tableau[i - 1][j] = temp insert(tableau, i - 1, j) if tableau[i][j] < tableau[i][j - 1]: # go left # swap tableau[i][j] with tableau[i][j - 1] temp = tableau[i][j] tableau[i][j] = tableau[i][j - 1] tableau[i][j - 1] = temp insert(tableau, i, j - 1) # Function to replace a given element in the Young tableau def replace(tableau, i, j, key): # delete element at cell (i, j) tableau[i][j] = float('inf') # fix the young tableau property fixTableau(tableau, i, j) # place the given key at the bottom right corner of the tableau tableau[M - 1][N - 1] = key # move the key to its correct position in the tableau insert(tableau, M - 1, N - 1) # A utility function to search an element in a Young tableau. def search(tableau, key, value): # start from the top-rightmost cell of tableau i.e. (0, N-1) cell i = 0 j = N - 1 # run till tableau boundary is reached while i < M and j >= 0: # if current element is less than the key, increment the row index if tableau[i][j] < key: i = i + 1 # if current element is more than the key, decrement the column index elif tableau[i][j] > key: j = j - 1 # current element is equal to the key else: replace(tableau, i, j, value) return True return False # Utility function to print a Young tableau def printTableau(tableau): for i in range(M): print(tableau[i]) if __name__ == '__main__': # M x N Young tableau tableau = [ [10, 12, 15, 16], [11, 18, 20, 25], [22, 27, 30, 35], [34, 40, 44, 45] ] (M, N) = (len(tableau), len(tableau[0])) search(tableau, 20, 14) # replace 20 by 14 printTableau(tableau) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 # Recursive function to fix the tableau property in a MxN Young tableau def fixTableau ( tableau , i , j ) : # get the values present at the bottom and right cell of the current cell bottom = tableau [ i + 1 ] [ j ] if ( i + 1 < M ) else float ( 'inf' ) right = tableau [ i ] [ j + 1 ] if ( j + 1 < N ) else float ( 'inf' ) if bottom < right : # go down # swap tableau[i][j] with tableau[i + 1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i + 1 ] [ j ] tableau [ i + 1 ] [ j ] = temp fixTableau ( tableau , i + 1 , j ) if bottom > right : # go right # swap tableau[i][j] with tableau[i][j + 1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j + 1 ] tableau [ i ] [ j + 1 ] = temp fixTableau ( tableau , i , j + 1 ) # Recursive function to insert an element into a non-full MxN Young tableau. def insert ( tableau , i , j ) : # base case if i == 0 and j == 0 : return # handle separately for the first row if i == 0 : if tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] : # swap tableau[i][j] with tableau[i][j - 1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] tableau [ i ] [ j - 1 ] = temp insert ( tableau , i , j - 1 ) return # handle separately for the first column if j == 0 : if tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] : # swap tableau[i][j] with tableau[i-1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] tableau [ i - 1 ] [ j ] = temp insert ( tableau , i - 1 , j ) return if tableau [ i ] [ j ] < tableau [ i - 1 ] [ j ] : # go up # swap tableau[i][j] with tableau[i-1][j] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i - 1 ] [ j ] tableau [ i - 1 ] [ j ] = temp insert ( tableau , i - 1 , j ) if tableau [ i ] [ j ] < tableau [ i ] [ j - 1 ] : # go left # swap tableau[i][j] with tableau[i][j - 1] temp = tableau [ i ] [ j ] tableau [ i ] [ j ] = tableau [ i ] [ j - 1 ] tableau [ i ] [ j - 1 ] = temp insert ( tableau , i , j - 1 ) # Function to replace a given element in the Young tableau def replace ( tableau , i , j , key ) : # delete element at cell (i, j) tableau [ i ] [ j ] = float ( 'inf' ) # fix the young tableau property fixTableau ( tableau , i , j ) # place the given key at the bottom right corner of the tableau tableau [ M - 1 ] [ N - 1 ] = key # move the key to its correct position in the tableau insert ( tableau , M - 1 , N - 1 ) # A utility function to search an element in a Young tableau. def search ( tableau , key , value ) : # start from the top-rightmost cell of tableau i.e. (0, N-1) cell i = 0 j = N - 1 # run till tableau boundary is reached while i < M and j >= 0 : # if current element is less than the key, increment the row index if tableau [ i ] [ j ] < key : i = i + 1 # if current element is more than the key, decrement the column index elif tableau [ i ] [ j ] > key : j = j - 1 # current element is equal to the key else : replace ( tableau , i , j , value ) return True return False # Utility function to print a Young tableau def printTableau ( tableau ) : for i in range ( M ) : print ( tableau [ i ] ) if __name__ == '__main__' : # M x N Young tableau tableau = [ [ 10 , 12 , 15 , 16 ] , [ 11 , 18 , 20 , 25 ] , [ 22 , 27 , 30 , 35 ] , [ 34 , 40 , 44 , 45 ] ] ( M , N ) = ( len ( tableau ) , len ( tableau [ 0 ] ) ) search ( tableau , 20 , 14 ) # replace 20 by 14 printTableau ( tableau ) Download Run Code Output:



[10, 12, 14, 15]

[11, 16, 18, 25]

[22, 27, 30, 35]

[34, 40, 44, 45]







The time complexity of replace operation is O(M + N) and the auxiliary space used by the program is O(M + N) for call stack.



Exercise: Determine if a given matrix is Young Tableau or not i.e. check if the matrix is row wise and column wise sorted.









(8 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

