VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-all-possible-paths-from-source-to-destination-in-given-3d-array/

⇱ Count all possible paths from source to destination in given 3D array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count all possible paths from source to destination in given 3D array

Last Updated : 23 Jul, 2025

Given three integers p, q, and r, the task is to count all the possible paths from the cell (0, 0, 0) to cell (p-1, q-1, r-1) in a matrix of size (p, q, r).  Movement is allowed only in three directions, which are along the positive direction of the three axes i.e. from any cell (i, j, k) movement is allowed to cells (i+1, j, k), (i, j+1, k) and (i, j, k+1).

Examples:

Input: p = 1, q = 1, r = 1
Output: 1
Explanation: As the source and destination both are same, there is only one possible way to stay there.

Input: p = 2, q = 2, r = 2
Output: 6
Explanation: The possible paths are :

  • (0, 0, 0) -> (0, 1, 0) -> (0, 1, 1) -> (1, 1, 1)
  • (0, 0, 0) -> (0, 1, 0) -> (1, 1, 0) -> (1, 1, 1)
  • (0, 0, 0) -> (1, 0, 0) -> (1, 1, 0) -> (1, 1, 1)
  • (0, 0, 0) -> (1, 0, 0) -> (1, 0, 1) -> (1, 1, 1)
  • (0, 0, 0) -> (0, 0, 1) -> (0, 1, 1) -> (1, 1, 1)
  • (0, 0, 0) -> (0, 0, 1) -> (1, 0, 1) -> (1, 1, 1)

Using Recursion – O(2^(p+q+r)) Time and O(p+q+r) Space

The recursive approach can be described with three potential moves from a given cell (i, j, k), where we can move to the next cells in three directions: (i+1, j, k), (i, j+1, k), or (i, j, k+1). The recurrence relation can be mathematically defined as:

  • numberOfWays(i, j, k) = numberOfWays(i+1, j, k) + numberOfWays(i, j+1, k) + numberOfWays(i, j, k+1)

Base Case: If the indices reach the last cell, i.e., when i = p-1, j = q-1 and k = r-1, the number of ways is 1: numberOfWays(i, j, k) = 1


Output
6

Using Top-Down DP (Memoization) - O(p*q*r) Time and O(p*q*r) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:

1. Optimal Substructure:

The number of unique possible paths to reach the cell (i, j, k) depends on the optimal solutions of the smaller subproblems. Specifically, the number of ways to reach (i, j, k) is determined by the number of ways to reach the adjacent cells (i+1, j, k), (i, j+1, k), and (i, j, k+1). By combining these optimal substructures, we can efficiently calculate the total number of ways to reach the destination cell (p-1, q-1, r-1).

2. Overlapping Subproblems:

When applying a recursive approach to this problem, we observe that many subproblems are computed multiple times, leading to overlapping subproblems. For instance, when calculating the number of paths to a particular cell, say numberOfPaths(3, 3, 3), we may recursively calculate numberOfPaths(3, 2, 3) and numberOfPaths(2, 3, 3). Both of these calculations, in turn, would recursively compute numberOfPaths(2, 2, 3) multiple times—once from numberOfPaths(3, 2, 3) and again from numberOfPaths(2, 3, 3). This redundancy leads to overlapping subproblems.

  • The recursive solution for counting the number of paths involves changing three parameters: i, which represents the current row, j, which represents the current column, and k, which represents the current depth along the third dimension.
  • We need to track all three parameters, so we need to create a 3D array of size p x q x r, where p is the number of rows, q is the number of columns, and r is the number of depths in the matrix.
  • This allows us to store the result for every combination of (i, j, k) where i will range from 0 to p-1, j from 0 to q-1, and k from 0 to r-1.

Output
6

Using Bottom-Up DP (Tabulation) - O(p*q*r) Time and O(p*q*r) Space

The approach here is similar to the recursive solution but, instead of breaking down the problem recursively, we use an iterative approach to build up the solution from the base cases in a bottom-up manner.
To solve the 3D path problem iteratively, we maintain a dp[i][j][k] table where each entry dp[i][j][k] stores the count of all possible paths to reach the cell (i, j, k).

Base Case: For i == p-1 and j == q-1 and k == r-1 , dp[i][j][k] = 1

Recursive Case: dp[i][j][k] = dp[i-1][j][k] + dp[i][j-1][k] + dp[i][j][k-1];


Output
6

Using Space Optimized DP - O(p*q*r) Time and O(q*r) Space

In previous approach we can see that the dp[i][j][k] is depend upon dp[i - 1][j][k], dp[i][j - 1][k] and dp[i][j][k - 1] so we can convert 3d matrix into current and previous 2d matrix where i-1 refer to previous and i refer to current.

  • Create two 2d matrix refer to previous and current matrix.
  • Initialize both matrix starting point with 1.
  • Now the approach is same as the previous code but we have to only convert dp[i] to current and dp[i-1] to previous.

Output
6
Comment
Article Tags:
Article Tags: