Skip to content

Commit 3017c91

Browse files
committed
feat(matrix): add MatrixRowPermutation and corresponding tests for row permutations
1 parent f9a9ccb commit 3017c91

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@
496496
- 📄 [InverseOfMatrix](src/main/java/com/thealgorithms/matrix/InverseOfMatrix.java)
497497
- 📄 [MatrixMultiplication](src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java)
498498
- 📄 [MatrixRank](src/main/java/com/thealgorithms/matrix/MatrixRank.java)
499+
- 📄 [MatrixRowPermutation](src/main/java/com/thealgorithms/matrix/MatrixRowPermutation.java)
499500
- 📄 [MatrixTranspose](src/main/java/com/thealgorithms/matrix/MatrixTranspose.java)
500501
- 📄 [MedianOfMatrix](src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java)
501502
- 📄 [MirrorOfMatrix](src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java)
@@ -1191,6 +1192,7 @@
11911192
- 📄 [InverseOfMatrixTest](src/test/java/com/thealgorithms/matrix/InverseOfMatrixTest.java)
11921193
- 📄 [MatrixMultiplicationTest](src/test/java/com/thealgorithms/matrix/MatrixMultiplicationTest.java)
11931194
- 📄 [MatrixRankTest](src/test/java/com/thealgorithms/matrix/MatrixRankTest.java)
1195+
- 📄 [MatrixRowPermutation](src/main/java/com/thealgorithms/matrix/MatrixRowPermutationTest.java)
11941196
- 📄 [MatrixTransposeTest](src/test/java/com/thealgorithms/matrix/MatrixTransposeTest.java)
11951197
- 📄 [MatrixUtilTest](src/test/java/com/thealgorithms/matrix/MatrixUtilTest.java)
11961198
- 📄 [MedianOfMatrixTest](src/test/java/com/thealgorithms/matrix/MedianOfMatrixTest.java)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static com.thealgorithms.matrix.utils.MatrixUtil.validateInputMatrix;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
*
10+
* <h1>Find All Row Permutations of a Matrix!</h1>
11+
*
12+
* This program generates and returns all possible permutations of the rows of a
13+
* given matrix.
14+
* It is useful for exploring different row arrangements while keeping column
15+
* structure intact.
16+
*
17+
* <p>
18+
* <b>Note:</b> Giving proper comments in your program makes it more user
19+
* friendly and it is assumed as a high quality code.
20+
*
21+
* @author Copilot
22+
* @version 11.0.9
23+
* @since 2025-10-01
24+
*/
25+
public final class MatrixRowPermutation {
26+
27+
private MatrixRowPermutation() {
28+
}
29+
30+
/**
31+
* Generate all permutations of the rows of a matrix.
32+
*
33+
* @param matrix The input matrix whose row permutations are to be generated
34+
* @throws IllegalArgumentException if the matrix is empty
35+
* @throws NullPointerException if the matrix is null
36+
* @return A list of matrices, each representing a unique row permutation
37+
*/
38+
public static List<int[][]> permuteRows(int[][] matrix) {
39+
if (matrix == null) {
40+
throw new NullPointerException("Matrix is null");
41+
}
42+
if (matrix.length == 0) {
43+
throw new IllegalArgumentException("Matrix is empty");
44+
}
45+
46+
List<int[][]> result = new ArrayList<>();
47+
permute(matrix, 0, result);
48+
return result;
49+
}
50+
51+
/**
52+
* Helper method to recursively generate permutations.
53+
*
54+
* @param matrix The matrix being permuted
55+
* @param start The current index to fix
56+
* @param result The list to store all permutations
57+
*/
58+
private static void permute(int[][] matrix, int start, List<int[][]> result) {
59+
if (start == matrix.length) {
60+
int[][] copy = new int[matrix.length][];
61+
for (int i = 0; i < matrix.length; i++) {
62+
copy[i] = matrix[i].clone();
63+
}
64+
result.add(copy);
65+
return;
66+
}
67+
68+
for (int i = start; i < matrix.length; i++) {
69+
swap(matrix, start, i);
70+
permute(matrix, start + 1, result);
71+
swap(matrix, start, i); // backtrack
72+
}
73+
}
74+
75+
/**
76+
* Swap two rows in the matrix.
77+
*
78+
* @param matrix The matrix in which rows are to be swapped
79+
* @param i The first row index
80+
* @param j The second row index
81+
*/
82+
private static void swap(int[][] matrix, int i, int j) {
83+
int[] temp = matrix[i];
84+
matrix[i] = matrix[j];
85+
matrix[j] = temp;
86+
}
87+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.List;
8+
import java.util.Arrays;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class MatrixRowPermutationTest {
13+
14+
@Test
15+
void testPermute2x2Matrix() {
16+
int[][] matrix = {
17+
{1, 2},
18+
{3, 4}
19+
};
20+
21+
List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
22+
assertEquals(2, permutations.size(), "Expected 2 permutations for 2x2 matrix");
23+
24+
// Check that both permutations are present
25+
boolean foundOriginal = false;
26+
boolean foundSwapped = false;
27+
28+
for (int[][] perm : permutations) {
29+
if (Arrays.deepEquals(perm, matrix)) {
30+
foundOriginal = true;
31+
} else if (Arrays.deepEquals(perm, new int[][]{{3, 4}, {1, 2}})) {
32+
foundSwapped = true;
33+
}
34+
}
35+
36+
assertTrue(foundOriginal, "Original matrix permutation missing");
37+
assertTrue(foundSwapped, "Swapped matrix permutation missing");
38+
}
39+
40+
@Test
41+
void testPermute3x1Matrix() {
42+
int[][] matrix = {
43+
{1},
44+
{2},
45+
{3}
46+
};
47+
48+
List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
49+
assertEquals(6, permutations.size(), "Expected 6 permutations for 3x1 matrix");
50+
}
51+
52+
@Test
53+
void testEmptyMatrixThrowsException() {
54+
int[][] empty = new int[0][0];
55+
assertThrows(IllegalArgumentException.class, () -> MatrixRowPermutation.permuteRows(empty));
56+
}
57+
58+
@Test
59+
void testNullMatrixThrowsException() {
60+
assertThrows(NullPointerException.class, () -> MatrixRowPermutation.permuteRows(null));
61+
}
62+
63+
@Test
64+
void testSingleRowMatrix() {
65+
int[][] matrix = {
66+
{42, 99}
67+
};
68+
69+
List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
70+
assertEquals(1, permutations.size(), "Expected 1 permutation for single-row matrix");
71+
assertTrue(Arrays.deepEquals(permutations.get(0), matrix), "Single-row matrix should remain unchanged");
72+
}
73+
}

0 commit comments

Comments
 (0)