Javatpoint Logo
Javatpoint Logo

Count Paths in Given Matrix in Java

A typical problem in computer science is counting pathways in a given matrix that can be solved in a number of ways. In this section, we will discuss three different ways to count path in the given matrix in Java.

Problem Statement

We have given a 2D matrix that resembles a grid. The grid is made up of non-negative integers, and each cell in the grid indicates the cost to go through it. Counting the number of possible routes that only move in the direction from the top-left corner to the bottom-right corner of the matrix is your task.

Input:

  1. A 2D matrix matrix of size m x n (1 <= m, n <= 100).
  2. Each cell matrix[i][j] represents the cost of that cell. You can assume all values are non-negative.

Output:

  1. An integer representing the total number of unique paths.

Method 1: Recursion

  1. Define a method countPathsRecursive() that takes the matrix and current position (row, col) as parameters.
  2. Check if (row, col) is the destination. If true, return 1.
  3. Check if (row, col) is outside the matrix boundaries. If true, return 0.
  4. Call countPathsRecursive() for moving right (row, col + 1) and down (row + 1, col).
  5. Sum up the results obtained from the recursive calls.
  6. In the main method, initialize the matrix and call countPathsRecursive() with the starting position (0, 0).

MatrixPathCount.java

Output:

Number of paths: 6

Method 2: Dynamic Programming (Memoization)

  1. Create method countPathsMemoization() that takes the matrix, current position, and a memoization table (memo) as parameters.
  2. Initialize a 2D memoization table (memo) with dimensions matching the matrix.
  3. Check memo[row][col] before recursive calls.
  4. If memo[row][col] is not zero, return its value.
  5. Memoize the results of recursive calls.
  6. Call countPathsMemoization() for moving right and down.
  7. Sum up the results obtained from the recursive calls.
  8. In the main method, initialize the matrix and memoization table, and call countPathsMemoization() with the starting position (0, 0).

MatrixPathCount.java

Output:

Number of paths: 6

Method 3: Dynamic Programming (Tabulation)

  1. Create a method countPathsTabulation() that takes the matrix as a parameter.
  2. Create a 2D dynamic programming table (dp) with dimensions matching the matrix.
  3. Initialize the rightmost column and bottom row of the DP table based on the fact that there is only one way to reach each cell in those rows/columns.
  4. Iterate through the matrix bottom-up, starting from the second-to-last row and column.
  5. Fill in the DP table based on the recursive formula: dp[i][j] = dp[i + 1][j] + dp[i][j + 1].
  6. The result is stored in dp[0][0].
  7. In the main() method, initialize the matrix and call countPathsTabulation().

MatrixPathCount.java

Output:

Number of paths: 6

Conclusion

In conclusion, there are several ways to solve the path counting problem in a given matrix, each involving a unique set of trade-offs. Though conceptually straightforward, the recursive method becomes ineffective for larger matrices since it involves duplicate computations. Whether by tabulation or memoization, dynamic programming overcomes this inefficiency by streamlining the computing process.

By using a memoization table to store and retrieve previously computed results, the memoization technique greatly reduces duplicate calculations and boosts productivity. When there are overlapping subproblems and recursion makes sense for addressing the problem, this approach is especially helpful.

On the other hand, tabulation approach uses a bottom-up dynamic programming table to iteratively fill in values, avoiding the overhead of recursion altogether. While this method is efficient, it requires additional space for the dynamic table.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA