Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Row with Minimum element in Matrix

Python | Row with Minimum element in Matrix

We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come in web development. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using reduce() + lambda The above two function can help us achieving this particular task. The lambda function does the task of logic and iteration and reduce function does the task of returning the required result. Works in python 2 only. 

Python3




# Python code to demonstrate
# Row with Minimum element in Matrix
# using reduce() + lambda
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
# using reduce() + lambda
# Row with Minimum element in Matrix
res = reduce(lambda i, j: i if min(i) < min(j) else j, test_matrix)
 
# printing result
print("Minimum element sublist is : " + str(res))


Output : 

The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]

Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.

Auxiliary Space: O(m)

Method #2: Using min() + key The min function can get the minimum of all the list and key is used to specify on what the min condition has to be applied that is on rows in this case. 

Python3




# Python3 code to demonstrate
# Row with Minimum element in Matrix
# using min() + key
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
# using min() + key
# Row with Minimum element in Matrix
res = min(test_matrix, key=min)
 
# printing result
print("Minimum element sublist is : " + str(res))


Output : 

The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using min() and extend() methods

Python3




# Python code to demonstrate
# Row with Minimum element in Matrix
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
 
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
 
# Row with Minimum element in Matrix
a=[]
for i in test_matrix:
    a.extend(i)
mi=min(a)
for i in test_matrix:
    if mi in i:
        res=i
        break
 
# printing result
print ("Minimum element sublist is : " + str(res))


Output

The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]

Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(k), k is the length of the list 

Method #4: Using numpy

Step-by-step algorithm:

  1. Import the numpy library.
  2. Initialize the matrix.
  3. Find the index of the row with the minimum element using the argmin() function of the numpy library.
  4. Get the corresponding row from the matrix.
  5. Print the result.

Python3




# importing the numpy library
import numpy as np
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
 
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
 
# using the argmin() function of the numpy library to find the index of the row with the minimum element
min_index = np.argmin(test_matrix)
 
# getting the corresponding row from the matrix
min_row = test_matrix[min_index]
 
# printing the result
print ("Minimum element sublist is : " + str(min_row))
#This code is contributed by Vinay Pinjala.


Output

The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]

Time complexity: The argmin() function takes O(N) time to find the minimum element and the corresponding row is obtained in O(1) time. Therefore, the overall time complexity is O(N).

Auxiliary Space: The space complexity is O(1) since no extra space is used to store the matrix.

METHOD 5:Using nested loop 

APPROACH:

The given Python code finds the sublist with the minimum element from a given matrix.

ALGORITHM:

1. Assign the first sublist of the matrix to min_sublist.
2. Iterate over each sublist in the matrix using a for loop.
3. For each sublist, iterate over each element using another for loop.
4. If the current element is smaller than the minimum element of min_sublist, update min_sublist to the current sublist.
5. After iterating over all sublists and elements, min_sublist will contain the sublist with the minimum element.
6. Print the minimum element sublist.

Python3




matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
 
min_sublist = matrix[0]
 
for sublist in matrix:
    for element in sublist:
        if element < min(min_sublist):
            min_sublist = sublist
 
print("Minimum element sublist is :", min_sublist)


Output

Minimum element sublist is : [1, 3, 1]

Time Complexity: O(n^2), where n is the number of sublists in the matrix. This is because the code iterates over each element in each sublist using nested for loops.
Space Complexity: O(n), where n is the number of elements in the smallest sublist. This is because min_sublist is assigned the first sublist of the matrix, and then updated to the sublist with the minimum element. Therefore, the maximum size of min_sublist is the size of the smallest sublist.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments