Count of ways to travel a cyclic path in N steps in a Triangular Pyramid

Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices.

Triangular Pyramid

Examples:

Input: N = 1
Output: 0
Explanation:
In 1 step, it is impossible to be again at position O.

Input: N = 2
Output: 3
Explanation:
The three ways to reach back to O in two steps are:
O->A->O
O->B->O
O->C->O



Input: N = 3
Output: 6
Explanation:
The 6 ways to reach back to O in three steps are:
O->A->B->O
O->A->C->O
O->B->A->O
O->B->C->O
O->C->A->O
O->C->B->O

Approach: The idea is to use the concept of Dynamic programming.

  1. A table T[][] is created where the row represents the number of ways and the column represents the position.
  2. In order to fill the table, one observation needs to be made. That is, we can go back to the position O if we are not at O in the previous step.
  3. Therefore, the number of ways to reach the origin O in the current step is equal to the sum of the number of ways the person is not at the origin O in the previous steps.
  4. Lets understand how the table is filled for N = 3:
        0   1    2   3
    O   1   0    3   6
    A   0   1    2   7
    B   0   1    2   7
    C   0   1    2   7
    
  5. The base case for this table is when N = 1. We can reach the origin in 1 step from all the positions except O.

Below is the implementation of the above approach:

Using Tabulation Approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for Dynamic
// Programming implementation of
// Number of Path in a Triangular
// pyramid
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number of
// ways we can reach back to the
// initial position O
int count(int n)
{
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
  
    // If n is equal to 1
    // then we can't reach at position O
    if (n == 1)
        return 0;
  
    int dp[4][n + 1];
  
    // Initial Conditions
  
    // Represents position O
    dp[0][0] = 1;
  
    // Represents position A
    dp[1][0] = 0;
  
    // Represents position B
    dp[2][0] = 0;
  
    // Represents position C
    dp[3][0] = 0;
  
    // Filling the table
    for (int i = 1; i <= n; i++) {
  
        // The number of ways to reach
        // a particular position (say X)
        // at the i'th step is equivalent
        // to the sum of the number
        // of ways the person is not at
        // position X in the last step.
  
        int countPositionO
            = dp[1][i - 1] + dp[2][i - 1]
              + dp[3][i - 1];
  
        int countPositionA
            = dp[0][i - 1] + dp[2][i - 1]
              + dp[3][i - 1];
  
        int countPositionB
            = dp[0][i - 1] + dp[1][i - 1]
              + dp[3][i - 1];
  
        int countPositionC
            = dp[0][i - 1] + dp[1][i - 1]
              + dp[2][i - 1];
  
        dp[0][i] = countPositionO;
        dp[1][i] = countPositionA;
        dp[2][i] = countPositionB;
        dp[3][i] = countPositionC;
    }
  
    return dp[0][n];
}
  
// Driver code
int main()
{
  
    int n = 3;
    cout << count(n) << endl;
  
    n = 4;
    cout << count(n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for dynamic programming
// implementation of number of path in 
// a triangular pyramid
class GFG{
  
// Function to return the number of
// ways we can reach back to the
// initial position O
static int count(int n)
{
      
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
  
    // If n is equal to 1 then we
    // can't reach at position O
    if (n == 1)
        return 0;
  
    int [][]dp = new int[4][n + 1];
  
    // Initial Conditions
  
    // Represents position O
    dp[0][0] = 1;
  
    // Represents position A
    dp[1][0] = 0;
  
    // Represents position B
    dp[2][0] = 0;
  
    // Represents position C
    dp[3][0] = 0;
  
    // Filling the table
    for(int i = 1; i <= n; i++)
    {
         
       // The number of ways to reach
       // a particular position (say X)
       // at the i'th step is equivalent
       // to the sum of the number
       // of ways the person is not at
       // position X in the last step.
       int countPositionO = dp[1][i - 1] + 
                            dp[2][i - 1] + 
                            dp[3][i - 1];
       int countPositionA = dp[0][i - 1] + 
                            dp[2][i - 1] + 
                            dp[3][i - 1];
       int countPositionB = dp[0][i - 1] + 
                            dp[1][i - 1] + 
                            dp[3][i - 1];
       int countPositionC = dp[0][i - 1] + 
                            dp[1][i - 1] + 
                            dp[2][i - 1];
         
       dp[0][i] = countPositionO;
       dp[1][i] = countPositionA;
       dp[2][i] = countPositionB;
       dp[3][i] = countPositionC;
    }
    return dp[0][n];
}
  
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(count(n) + "\n");
  
    n = 4;
    System.out.print(count(n) + "\n");
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for Dynamic
# Programming implementation of
# Number of Path in a Triangular
# pyramid
  
# Function to return the number of
# ways we can reach back to the
# initial position O
def count(n):
  
    # If n is 0 then there is
    # 1 solution
    if (n == 0):
        return 1
  
    # If n is equal to 1
    # then we can't reach at position O
    if (n == 1):
        return 0
  
    dp = [[0 for i in range(n + 1)] 
             for j in range(4)]
  
    # Initial Conditions
  
    # Represents position O
    dp[0][0] = 1
  
    # Represents position A
    dp[1][0] = 0
  
    # Represents position B
    dp[2][0] = 0
  
    # Represents position C
    dp[3][0] = 0
  
    # Filling the table
    for i in range(1, n + 1):
  
        # The number of ways to reach
        # a particular position (say X)
        # at the i'th step is equivalent
        # to the sum of the number
        # of ways the person is not at
        # position X in the last step.
  
        countPositionO = (dp[1][i - 1] + 
                          dp[2][i - 1] + 
                          dp[3][i - 1])
  
        countPositionA = (dp[0][i - 1] + 
                          dp[2][i - 1] + 
                          dp[3][i - 1])
  
        countPositionB = (dp[0][i - 1] + 
                          dp[1][i - 1] + 
                          dp[3][i - 1])
  
        countPositionC = (dp[0][i - 1] + 
                          dp[1][i - 1] + 
                          dp[2][i - 1])
  
        dp[0][i] = countPositionO
        dp[1][i] = countPositionA
        dp[2][i] = countPositionB
        dp[3][i] = countPositionC
      
    return dp[0][n]
  
# Driver code
if __name__ == "__main__":
  
    n = 3
    print(count(n))
  
    n = 4
    print(count(n))
  
# This code is contributed by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for dynamic programming
// implementation of number of path in 
// a triangular pyramid
using System;
  
class GFG{
  
// Function to return the number 
// of ways we can reach back to 
// the initial position O
static int count(int n)
{
      
    // If n is 0 then there is
    // 1 solution
    if (n == 0)
        return 1;
  
    // If n is equal to 1 then we
    // can't reach at position O
    if (n == 1)
        return 0;
  
    int [,]dp = new int[4, n + 1];
  
    // Initial Conditions
  
    // Represents position O
    dp[0, 0] = 1;
  
    // Represents position A
    dp[1, 0] = 0;
  
    // Represents position B
    dp[2, 0] = 0;
  
    // Represents position C
    dp[3, 0] = 0;
  
    // Filling the table
    for(int i = 1; i <= n; i++)
    {
          
       // The number of ways to reach
       // a particular position (say X)
       // at the i'th step is equivalent
       // to the sum of the number
       // of ways the person is not at
       // position X in the last step.
       int countPositionO = dp[1, i - 1] + 
                            dp[2, i - 1] + 
                            dp[3, i - 1];
       int countPositionA = dp[0, i - 1] + 
                            dp[2, i - 1] + 
                            dp[3, i - 1];
       int countPositionB = dp[0, i - 1] +
                            dp[1, i - 1] + 
                            dp[3, i - 1];
       int countPositionC = dp[0, i - 1] + 
                            dp[1, i - 1] +
                            dp[2, i - 1];
         
       dp[0, i] = countPositionO;
       dp[1, i] = countPositionA;
       dp[2, i] = countPositionB;
       dp[3, i] = countPositionC;
    }
    return dp[0, n];
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    Console.Write(count(n) + "\n");
  
    n = 4;
    Console.Write(count(n) + "\n");
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

6
21

Time Complexity: O(N).
Auxiliary Space Complexity: O(4*N)

Note:

  • This program works more efficiently to find the number of ways in constant time after preprocessing for multiple queries if we fill the table for the largest number among the set of queries.
  • This program can also be made more space-efficient for a single query. The idea is that since we need just previous step values to compute the present step values, so by just taking 4 variables to store the previous step values, we can solve the given problem in constant space.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.