Problem 1. Exercise 4.4: Search a Matrix. Given a matrix (2D array) with n integers, search for a target value x efficiently, i.e., return whether x exists in the matrix (true/false). In what follows, we will call a matrix any matrix that satisfies the conditions shown in the image above. We additionally assume that the matrix may have missing numerical elements. In this case, the cell will be filled with the special value INF, which is larger than any number (like infinity in mathematics). If a row contains an INF value, this must be the last value, and if a column contains an INF value, this must be the last value as well. For example, if a matrix contains a single INF value (and the rest is filled with numbers), the INF value must be located in the bottom row of the right-most column. If a matrix contains no numbers, it will be filled only with INF values. Read the picture for the rest of the question.
Problem 1. (30 MARKS, 10 MARKS EACH PART) Recall exercise 4.4 from Week 4 lecture notes:
Exercise 4.4: Search a matrix
Given a matrix (2D array) with n integers, search for a target value x efficiently, i.e., return whether x exists in the matrix (true/false).
Assumptions:
- Each row is sorted in ascending order
- Each column is sorted in ascending order
In what follows, we will call a matrix any matrix that satisfies the conditions shown in the image above. We will additionally assume that the matrix may have missing numerical elements. In this case, the cell will be filled with the special value INF, which is larger than any number (like infinity in mathematics). If a row contains an INF value, this must be the last value, and if a column contains an INF value, this must be the last value as well. For example, if a matrix contains a single INF value (and the rest is filled with numbers), the INF value must be located in the bottom row of the right-most column. If a matrix contains no numbers (a.k.a empty matrix), it will be filled only with INF values. In the form of a warm-up exercise, draw a 4x4 matrix containing elements {9, 16, 3, 2, 4, 8, 5, 14, 12}.
1. Develop an efficient algorithm called popmin(n) where A is a nonempty n x n matrix which returns the smallest element of A (which by definition is the element located in row 1, column 1) preserving the properties of the matrix as described above. Since we are removing a number from the matrix, the matrix after the operation will contain one more INF value. HINT: Think of BuildMaxHeap and don't forget to use recursion! The required operation must occur in place, that is no additional matrix (or additional array of any kind) can be used.
2. Develop an efficient algorithm insert(A, value) where A is a non-full matrix (i.e. a matrix satisfying the above conditions, containing at least one INF value), and value a number to be inserted. The insertion must occur in place, that is no additional matrix (or additional array of any kind) can be used.
3. Using no other sorting method, but the algorithms developed in previous parts of this exercise (if needed), develop an algorithm that sorts 2 numbers in O(n^3) time. You must use an n x n matrix as described above.