An M × N Young tableau is an M × N matrix such that the entries of each row are sorted from left to right and the entries of each column are sorted from top to bottom. Some entries of a Young tableau may be infinity, which indicates an empty entry. Thus, a Young tableau can be used to hold n <= M*N finite numbers.

For example, the following matrix is a Young tableau:

Young tableau

 
In this post, we will see how to

1. Insert operation in Young tableau

Practice this problem

To insert an element into a non-full Young tableau, the idea is to place the element in the 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++


Download  Run Code

Output:

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

Java


Download  Run Code

Output:

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

Python


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 an M × N Young tableau from a given list of elements is O(M × N × (M + N)). The additional space used by the program is O(M + N) for the call stack.

A naive solution would be to traverse the complete tableau to search for a given value. The problem with this approach is that it would require O(M × N) time for searching in an M × N Young tableau.

Can we do better?

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

  1. If the current element is less than the key, increment the row index (move to the next row)
  2. If the current element is more than the key, decrement the column index (move to the previous column)
  3. If the current element is equal to the key, return true as we have found the element. If we end up in a row or column beyond the tableau boundary, terminate the search and return false.

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

C++


Download  Run Code

Output:

Key 20 found in tableau
Key 25 found in tableau

Java


Download  Run Code

Output:

Key 20 found in tableau
Key 25 found in tableau

Python


Download  Run Code

Output:

Key 20 found in tableau
Key 25 found in tableau

The time complexity of the search operation is O(M + N) for an M × N matrix and doesn’t require any extra space.

3. Extract-Min operation in Young tableau

Practice this problem

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

A Young tableau is similar to a heap where each cell (i, j) can be considered 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, swap the smallest 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 recursively implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

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

Java


Download  Run Code

Output:

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

Python


Download  Run Code

Output:

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

The fixTableau() routine takes an M × N matrix and breaks it into either an (M-1) × N or an M × (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 the Extract-Min operation on a Young tableau is O(M + N).

4. Delete operation in Young tableau

The idea is simple and inspired from the 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 submatrix 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++


Download  Run Code

Output:

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

Java


Download  Run Code

Output:

[10, 12, 15, 16]
[11, 18, 25, 35]
[22, 27, 30, 45]
[34, 40, 44, 2147483647]

Python


Download  Run Code

Output:

[10, 12, 15, 16]
[11, 18, 25, 35]
[22, 27, 30, 45]
[34, 40, 44, inf]

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

5. Replace operation in Young tableau

Practice this problem

To replace an element x with an element y in a Young tableau:

  1. Start by searching x's position (i, j) in the tableau.
  2. Replace the element at cell (i, j) by infinity and fix the Young tableau property.
  3. Place the element y in the tableau’s bottom-right corner and move the key to its correct position in the tableau.

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

C++


Download  Run Code

Output:

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

Java


Download  Run Code

Output:

[10, 12, 14, 15]
[11, 16, 18, 25]
[22, 27, 30, 35]
[34, 40, 44, 45]

Python


Download  Run Code

Output:

[10, 12, 14, 15]
[11, 16, 18, 25]
[22, 27, 30, 35]
[34, 40, 44, 45]

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

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