Skip to content
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@
- 📄 [InverseOfMatrix](src/main/java/com/thealgorithms/matrix/InverseOfMatrix.java)
- 📄 [MatrixMultiplication](src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java)
- 📄 [MatrixRank](src/main/java/com/thealgorithms/matrix/MatrixRank.java)
- 📄 [MatrixRowPermutation](src/main/java/com/thealgorithms/matrix/MatrixRowPermutation.java)
- 📄 [MatrixTranspose](src/main/java/com/thealgorithms/matrix/MatrixTranspose.java)
- 📄 [MedianOfMatrix](src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java)
- 📄 [MirrorOfMatrix](src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java)
Expand Down Expand Up @@ -1191,6 +1192,7 @@
- 📄 [InverseOfMatrixTest](src/test/java/com/thealgorithms/matrix/InverseOfMatrixTest.java)
- 📄 [MatrixMultiplicationTest](src/test/java/com/thealgorithms/matrix/MatrixMultiplicationTest.java)
- 📄 [MatrixRankTest](src/test/java/com/thealgorithms/matrix/MatrixRankTest.java)
- 📄 [MatrixRowPermutation](src/main/java/com/thealgorithms/matrix/MatrixRowPermutationTest.java)
- 📄 [MatrixTransposeTest](src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java)
- 📄 [MatrixUtilTest](src/test/java/com/thealgorithms/matrix/MatrixUtilTest.java)
- 📄 [MedianOfMatrixTest](src/test/java/com/thealgorithms/matrix/MedianOfMatrixTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.thealgorithms.matrix;

import java.util.ArrayList;
import java.util.List;

/**
* This class provides a method to generate all possible permutations of the rows of a matrix.
* Row permutations are useful for exploring different arrangements while keeping the column structure intact.
*
* For example, consider the following 2x2 matrix:
* 1 2
* 3 4
* The row permutations are:
* 1 2 3 4
* 3 4 1 2
*
* @author Suraj Singh Chauhan
*/
public final class MatrixRowPermutation {

private MatrixRowPermutation() {
}

/**
* @brief Generates all permutations of the rows of a matrix.
*
* @param matrix The input matrix; must be non-null and non-empty
* @return A list of matrices, each representing a unique row permutation
* @throws IllegalArgumentException if the matrix is empty
* @throws NullPointerException if the matrix is null
*/
public static List<int[][]> permuteRows(int[][] matrix) {
if (matrix == null) {
throw new NullPointerException("Matrix is null");
}
if (matrix.length == 0) {
throw new IllegalArgumentException("Matrix is empty");
}

List<int[][]> result = new ArrayList<>();
permute(matrix, 0, result);
return result;
}

/**
* @brief Helper method to recursively generate permutations of matrix rows.
*
* @param matrix The matrix being permuted
* @param start The current index to fix
* @param result The list to store all permutations
*/
private static void permute(int[][] matrix, int start, List<int[][]> result) {
if (start == matrix.length) {
int[][] copy = new int[matrix.length][];
for (int i = 0; i < matrix.length; i++) {
copy[i] = matrix[i].clone();
}
result.add(copy);
return;
}
for (int i = start; i < matrix.length; i++) {
swap(matrix, start, i);
permute(matrix, start + 1, result);
swap(matrix, start, i); // backtrack
}
}

/**
* @brief Swaps two rows in the matrix.
*
* @param matrix The matrix
* @param i The index of the first row
* @param j The index of the second row
*/
private static void swap(int[][] matrix, int i, int j) {
int[] temp = matrix[i];
matrix[i] = matrix[j];
matrix[j] = temp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.thealgorithms.matrix;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

public class MatrixRowPermutationTest {

@Test
void testPermute2x2Matrix() {
int[][] matrix = {{1, 2}, {3, 4}};

List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
assertEquals(2, permutations.size(), "Expected 2 permutations for 2x2 matrix");

// Check that both permutations are present
boolean foundOriginal = false;
boolean foundSwapped = false;

for (int[][] perm : permutations) {
if (Arrays.deepEquals(perm, matrix)) {
foundOriginal = true;
} else if (Arrays.deepEquals(perm, new int[][] {{3, 4}, {1, 2}})) {
foundSwapped = true;
}
}

assertTrue(foundOriginal, "Original matrix permutation missing");
assertTrue(foundSwapped, "Swapped matrix permutation missing");
}

@Test
void testPermute3x1Matrix() {
int[][] matrix = {{1}, {2}, {3}};

List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
assertEquals(6, permutations.size(), "Expected 6 permutations for 3x1 matrix");
}

@Test
void testEmptyMatrixThrowsException() {
int[][] empty = new int[0][0];
assertThrows(IllegalArgumentException.class, () -> MatrixRowPermutation.permuteRows(empty));
}

@Test
void testNullMatrixThrowsException() {
assertThrows(NullPointerException.class, () -> MatrixRowPermutation.permuteRows(null));
}

@Test
void testSingleRowMatrix() {
int[][] matrix = {{42, 99}};

List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
assertEquals(1, permutations.size(), "Expected 1 permutation for single-row matrix");
assertTrue(Arrays.deepEquals(permutations.get(0), matrix), "Single-row matrix should remain unchanged");
}
}
Loading