## Count Paths in Given Matrix in JavaA 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 ## Problem StatementWe 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.
- A 2D matrix matrix of size m x n (1 <= m, n <= 100).
- Each cell matrix[i][j] represents the cost of that cell. You can assume all values are non-negative.
- An integer representing the total number of unique paths.
## Method 1: Recursion- Define a method countPathsRecursive() that takes the matrix and current position (row, col) as parameters.
- Check if (row, col) is the destination. If true, return 1.
- Check if (row, col) is outside the matrix boundaries. If true, return 0.
- Call countPathsRecursive() for moving right (row, col + 1) and down (row + 1, col).
- Sum up the results obtained from the recursive calls.
- In the main method, initialize the matrix and call countPathsRecursive() with the starting position (0, 0).
Number of paths: 6 ## Method 2: Dynamic Programming (Memoization)- Create method countPathsMemoization() that takes the matrix, current position, and a memoization table (memo) as parameters.
- Initialize a 2D memoization table (memo) with dimensions matching the matrix.
- Check memo[row][col] before recursive calls.
- If memo[row][col] is not zero, return its value.
- Memoize the results of recursive calls.
- Call countPathsMemoization() for moving right and down.
- Sum up the results obtained from the recursive calls.
- In the main method, initialize the matrix and memoization table, and call countPathsMemoization() with the starting position (0, 0).
Number of paths: 6 ## Method 3: Dynamic Programming (Tabulation)- Create a method countPathsTabulation() that takes the matrix as a parameter.
- Create a 2D dynamic programming table (dp) with dimensions matching the matrix.
- 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.
- Iterate through the matrix bottom-up, starting from the second-to-last row and column.
- Fill in the DP table based on the recursive formula: dp[i][j] = dp[i + 1][j] + dp[i][j + 1].
- The result is stored in dp[0][0].
- In the main() method, initialize the matrix and call countPathsTabulation().
Number of paths: 6 ## ConclusionIn 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. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]