![]() |
VOOZH | about |
Sometimes, while working with Python Matrix, one can have a problem in which, one needs to extract all the rows that are a possible subset of any row of other Matrix. This kind of problem can have applications in data domains as a matrix is a key data type in those domains. Let's discuss certain ways in which this problem can be solved.
Input : test_list = [[4, 5, 7], [2, 3, 4], [9, 8, 6]], check_matr = [[2, 3], [1, 2], [9, 0]] Output : [[2, 3]]
Input : test_list = [[4, 1, 2], [2, 3, 4], [9, 8, 0]], check_matr = [[2, 3], [1, 2], [9, 0]] Output : [[2, 3], [1, 2], [9, 0]]
The combination of the above functions offers a way in which this problem can be solved. In this, we check for the occurrence of all elements of row using all(), and any() is used to match any row of the Matrix. List comprehension is used to bind the logic together.
The original list is : [[4, 5, 7], [2, 3, 4], [9, 8, 0]] Matrix row subsets : [[2, 3], [9, 0]]
Time complexity: O(n^3) where n is the size of the input matrix. This is because the program uses nested loops to iterate over the elements of the input matrix and the check matrix, and the all() and any() functions also iterate over the lists. Therefore, the time complexity of the program can be expressed as O(n * m * k), where n is the number of rows in the check matrix, m is the number of rows in the input matrix, and k is the number of elements in each row.
Auxiliary space: O(n), where n is the size of the input matrix. This is because the program creates a new list res to store the matrix row subsets. The size of this list is at most equal to the number of rows in the input matrix. Therefore, the space complexity of the program can be expressed as O(m), where m is the number of rows in the input matrix.
The combination of the above functions can be used for this task. In this, we perform the task of nested loop using product() and set() conversion to check for subset of one container over another. List comprehension is used to bind all together.
The original list is : [[4, 5, 7], [2, 3, 4], [9, 8, 0]] Matrix row subsets : [[2, 3], [9, 0]]
Time complexity: O(n^2 * m^2), where n is the number of rows in check_matr and m is the number of rows in test_list.
Auxiliary space: O(m), where m is the number of elements in the longest row of test_list.
In this code loops over each row in check_matr and each list in test_list, then checks if the set of the current row is a subset of the set of the current list using set.issubset(). If it is, the row is added to the result list.
Matrix row subsets: [[2, 3], [9, 0]]
Time Complexity: O(n * m * k) where n, m, and k are the lengths of check_matr, test_list.
Auxiliary Space: O(1) since it uses a constant amount of extra space to store variables like res, row, and lst.
Matrix row subsets : [[2, 3], [9, 0]]
Time complexity: O(nmk) where n is the length of test_list, m is the length of check_matr, and k is the length of the sublists.
Auxiliary space: O(1) since we are not storing any additional data structures.
The original list is : [[4, 5, 7], [2, 3, 4], [9, 8, 0]] Matrix row subsets : [[2, 3], [9, 0]]
Time complexity: O(nmk) where n is the number of rows in check_matr, m is the number of rows in test_list, and k is the length of each row.
Auxiliary space: O(k) where k is the length of each row.
Matrix row subsets: [[2, 3], [9, 0]]
Time complexity: O(n^2), where n is the number of elements in the input matrices
Auxiliary space: O(1), because it does not use any additional data structures besides the input matrices and the output list.