Rotate a given Matrix in Java in Java

Rotating a matrix is a common problem in computer science, particularly in the fields of graphics and image processing. There are different ways to rotate a matrix, with varying time and space complexities. Here, we will discuss how to rotate a matrix 90 degrees clockwise using three different approaches:

  1. Using Transpose and Reverse
  2. Using a Layer-by-Layer Approach

Using Transpose and Reverse Approach

This method involves two main steps. First, transpose the matrix, which means converting all the rows of a matrix into columns and vice-versa. Then, reverse each row of the transposed matrix to achieve a 90-degree clockwise rotation.

  • Transpose: Swap the element at (i, j) with the element at (j, i) for all elements where i < j.
  • Reverse Rows: For each row, reverse the elements from the start to the end of the row.

File Name: RotateMatrixTransposeReverse.java

Output:

 
Rotated Matrix:
7 4 1 
8 5 2 
9 6 3   

Time Complexity: O(n2) due to traversing the matrix for both transpose and reverse operations.

Space Complexity: O(1) if done in-place.

Using a Layer-by-Layer Approach

This approach rotates the matrix in layers, starting from the outermost layer and moving towards the innermost layer. Each layer consists of four sides of the matrix, and each element is moved to its corresponding position in a 90-degree rotated fashion.

  • Layer-by-Layer: For each layer, swap the elements in groups of four. Move the elements from the top to the right, right to bottom, bottom to left, and left to top.
  • Iterate through Layers: Continue this process for each layer, moving inwards from the outermost layer to the innermost layer.

File Name: RotateMatrixLayerByLayer.java

Output:

 
Rotated Matrix:
7 4 1 
8 5 2 
9 6 3   

Time Complexity: O(n2) since each element is moved once.

Space Complexity: O(1) as the rotation is done in-place.

Conclusion

Both the Transpose and Reverse method and the Layer-by-Layer approach offer efficient ways to rotate a matrix 90 degrees clockwise, each with its own strengths:

  • Transpose and Reverse: This method involves first transposing the matrix and then reversing each row. It has a time complexity of O(n2) due to traversing the matrix for both transpose and reverse operations.
    It is also space-efficient with an O(1) space complexity since the operations can be performed in-place. This method is simple to implement and understand, making it a good choice for many applications.
  • Layer-by-Layer: This approach rotates the matrix by processing it layer by layer, starting from the outermost layer and moving inward. It also has a time complexity of O(n2) since each element is moved once.
    The space complexity is O(1) as it modifies the matrix in-place without requiring additional storage. This method is effective for scenarios where in-place rotation is critical and provides a clear, systematic way of handling matrix rotation.

Both methods are suitable for rotating matrices in place with efficient use of time and space, allowing for flexibility depending on the specific requirements of the problem at hand.