We need to count all paths from the top left to the bottom right of the MxN matrix. You can either move down or right.
Recursion:
public int countMatrixPathsRec(int [][] matrix, int row, int col){ //base case // If you reach at last row or column then you have only one path to go if(row==matrix.length-1 || col==matrix[0].length-1){ return 1; } return countMatrixPathsRec(matrix, row+1, col) + countMatrixPathsRec(matrix, row, col+1); }
Recursion will work fine but time complexity of this solution will be exponential as there are lots of overlapping subproblems here.
We can use dynamic programming to solve the problem. We won’t recompute any subproblem more than once.
Dynamic programming
Here is a simple algorithm for a dynamic programming solution.
- Initialize an array dp with matrix’s dimensions. This array will give us the final count, once we reach to bottom right.
- Fill the first row with 1, as you can reach here from one direction(by going right)
- Fill the first column with 1 as well, as you can reach here from one direction(by going down).
- Iterate over all other rows and columns and use the formula dp=dp[i-1][j] + dp[i][j-1], as you can reach here from two directions (By going right or By going down)
- return dp[matrix.length-1][matrix[0].length-1]
Here is a diagramtic illustration of the algorithm.
public class CountMatrixPaths { public static void main(String[] args) { CountMatrixPaths cmp=new CountMatrixPaths(); int[][] matrix= { {1,2,3}, {4,5,6}, {7,8,9} }; int totalPathsRec = cmp.countMatrixPathsRec(matrix,0,0); System.out.println("Total paths to reach top left to bottom right using recursion: "+totalPathsRec); int totalPaths = cmp.countMatrixPathsDynamicProgramming(matrix); System.out.println("Total paths to reach top left to bottom right using DP: "+totalPaths); } public int countMatrixPathsRec(int [][] matrix, int row, int col){ //base case // If you reach at last row or column then you have only one path to go if(row==matrix.length-1 || col==matrix[0].length-1){ return 1; } return countMatrixPathsRec(matrix, row+1, col) + countMatrixPathsRec(matrix, row, col+1); } public int countMatrixPathsDynamicProgramming(int [][] matrix){ int dp [][] = new int[matrix.length][matrix[0].length]; // no of path for first row will be 1 as you can move only in one direction for (int i = 0; i <dp.length ; i++) { dp[0][i] = 1; } // no of path for first column will be 1 as you can move only in one direction for (int i = 0; i <dp.length ; i++) { dp[i][0] = 1; } for (int i = 1; i <dp.length ; i++) { for (int j = 1; j <dp.length ; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[matrix.length-1][matrix[0].length-1]; } }
public int countMatrixPathsDynamicProgramming(int [][] matrix){ int dp [][] = new int[matrix.length][matrix[0].length]; // no of path for first row will be 1 as you can move only in one direction for (int i = 0; i <dp.length ; i++) { dp[0][i] = 1; } // no of path for first column will be 1 as you can move only in one direction for (int i = 0; i <dp.length ; i++) { dp[i][0] = 1; } for (int i = 1; i <dp.length ; i++) { for (int j = 1; j <dp.length ; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[matrix.length-1][matrix[0].length-1]; }
Here is the output of the above program.:
Total paths to reach top left to bottom right using recursion: 6 Total paths to reach top left to bottom right using DP: 6
That’s all about counting all paths from top left to the bottom right of MxN matrix.