Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Rotate Matrix in Python
Rotating a matrix in Python can be done using various approaches. The most common method is the Transpose and Reverse technique, which converts rows to columns and then reverses each row to achieve a 90-degree clockwise rotation.
Original Matrix
Let's consider a 3×3 matrix that we want to rotate 90 degrees clockwise ?
| 1 | 5 | 7 |
| 9 | 6 | 3 |
| 2 | 1 | 3 |
Using Transpose and Reverse
This method is efficient and involves two main steps ?
Transpose the matrix ? Convert rows to columns and columns to rows
Reverse each row ? Reverse the elements in each row to complete the rotation
def rotate_matrix(matrix):
n = len(matrix)
# Step 1: Transpose the matrix
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Step 2: Reverse each row
for i in range(n):
matrix[i].reverse()
return matrix
# Test with the example matrix
original_matrix = [[1, 5, 7], [9, 6, 3], [2, 1, 3]]
print("Original Matrix:")
for row in original_matrix:
print(row)
rotated_matrix = rotate_matrix(original_matrix)
print("\nRotated Matrix (90° clockwise):")
for row in rotated_matrix:
print(row)
Original Matrix: [1, 5, 7] [9, 6, 3] [2, 1, 3] Rotated Matrix (90° clockwise): [2, 9, 1] [1, 6, 5] [3, 3, 7]
Using Layer by Layer Rotation
This method rotates the matrix in-place without using extra space. It processes the matrix from the outermost layer to the inner layers ?
def rotate_layer_by_layer(matrix):
n = len(matrix)
for layer in range(n // 2):
for j in range(layer, n - layer - 1):
# Save the top element
temp = matrix[layer][j]
# Move left to top
matrix[layer][j] = matrix[n - j - 1][layer]
# Move bottom to left
matrix[n - j - 1][layer] = matrix[n - layer - 1][n - j - 1]
# Move right to bottom
matrix[n - layer - 1][n - j - 1] = matrix[j][n - layer - 1]
# Move top to right
matrix[j][n - layer - 1] = temp
return matrix
# Test with a fresh matrix
test_matrix = [[1, 5, 7], [9, 6, 3], [2, 1, 3]]
print("Original Matrix:")
for row in test_matrix:
print(row)
rotated_matrix = rotate_layer_by_layer(test_matrix)
print("\nRotated Matrix:")
for row in rotated_matrix:
print(row)
Original Matrix: [1, 5, 7] [9, 6, 3] [2, 1, 3] Rotated Matrix: [2, 9, 1] [1, 6, 5] [3, 3, 7]
Using Temporary Matrix
This approach creates a new matrix to store the rotated elements, then copies them back to the original matrix ?
def rotate_with_temp_matrix(matrix):
n = len(matrix)
temp_matrix = [[0] * n for _ in range(n)]
# Fill the temporary matrix with rotated values
for i in range(n):
for j in range(n):
temp_matrix[j][n - 1 - i] = matrix[i][j]
# Copy back to original matrix
for i in range(n):
for j in range(n):
matrix[i][j] = temp_matrix[i][j]
return matrix
# Test with another matrix
test_matrix = [[10, 15, 27], [11, 16, 8], [43, 17, 35]]
print("Original Matrix:")
for row in test_matrix:
print(row)
rotated_matrix = rotate_with_temp_matrix(test_matrix)
print("\nRotated Matrix:")
for row in rotated_matrix:
print(row)
Original Matrix: [10, 15, 27] [11, 16, 8] [43, 17, 35] Rotated Matrix: [43, 11, 10] [17, 16, 15] [35, 8, 27]
Comparison of Methods
| Method | Space Complexity | Time Complexity | Best For |
|---|---|---|---|
| Transpose & Reverse | O(1) | O(n²) | Most efficient |
| Layer by Layer | O(1) | O(n²) | In-place rotation |
| Temporary Matrix | O(n²) | O(n²) | Preserves original |
Conclusion
The transpose and reverse method is the most popular approach for rotating matrices due to its simplicity and efficiency. Use the layer-by-layer method when you need to understand the rotation mechanics, or the temporary matrix method when you need to preserve the original matrix.
---