Number of different cyclic paths of length N in a tetrahedron

Given a tetrahedron(vertex are A, B, C, D), the task is to find the number of different cyclic paths with length n from a vertex.
Note: Considering only a single vertex B i.e. to find the number of different cyclic paths of length N from B to itself.
Examples:Â
Input: 2 Output: 3 The paths of length 2 which starts and ends at D are: B-A-B B-D-B B-C-B Input: 3 Output: 6
Approach: Dynamic Programming can be used to keep track of the number of paths for previous values of N. Check for the number of moves which are left and where are we when we are moving in a path. That is 4n states, each with 3 options. Observe that all the vertices A, B, C are equivalent. Let zB be 1 initially and as at 0 steps, we can reach B itself only. Let zACD be 1 as paths for reaching other vertexes A, C and D is 0. Hence the recurrence relation formed will be:Â Â
Paths for N steps to reach b is = zADC*3Â
At every step, zADC gets multiplied by 2 (2 states) and it is added by zB since zB is the number of paths at step n-1 which comprises of the remaining 2 states.Â
Below is the implementation of the above approach:Â Â
C++
// C++ program count total number of// paths to reach B from B#include <bits/stdc++.h>#include <math.h>using namespace std;Â
// Function to count the number of// steps in a tetrahedronint countPaths(int n){Â Â Â Â // initially coming to B is B->BÂ Â Â Â int zB = 1;Â
    // cannot reach A, D or C    int zADC = 0;Â
    // iterate for all steps    for (int i = 1; i <= n; i++) {Â
        // recurrence relation        int nzB = zADC * 3;Â
        int nzADC = (zADC * 2 + zB);Â
        // memoize previous values        zB = nzB;        zADC = nzADC;    }Â
    // returns steps    return zB;}Â
// Driver Codeint main(){Â Â Â Â int n = 3;Â Â Â Â cout << countPaths(n);Â
    return 0;} |
Java
// Java program count total // number of paths to reach// B from Bimport java.io.*;Â
class GFG {     // Function to count the// number of steps in a// tetrahedronstatic int countPaths(int n){    // initially coming     // to B is B->B    int zB = 1;Â
    // cannot reach A, D or C    int zADC = 0;Â
    // iterate for all steps    for (int i = 1; i <= n; i++)     {Â
        // recurrence relation        int nzB = zADC * 3;Â
        int nzADC = (zADC * 2 + zB);Â
        // memoize previous values        zB = nzB;        zADC = nzADC;    }Â
    // returns steps    return zB;}Â
// Driver Codepublic static void main (String[] args) {Â Â Â Â int n = 3;Â Â Â Â System.out.println(countPaths(n));}}Â
// This code is contributed by ajit |
Python3
# Python3 program count total number of# paths to reach B from BÂ
# Function to count the number of# steps in a tetrahedrondef countPaths(n):Â Â Â Â Â Â Â Â Â # initially coming to B is B->BÂ Â Â Â zB = 1Â
    # cannot reach A, D or C    zADC = 0Â
    # iterate for all steps    for i in range(1, n + 1): Â
        # recurrence relation        nzB = zADC * 3Â
        nzADC = (zADC * 2 + zB)Â
        # memoize previous values        zB = nzB        zADC = nzADC         # returns steps    return zBÂ
# Driver coden = 3print(countPaths(n))Â
# This code is contributed by ashutosh450 |
C#
// C# program count total // number of paths to reach // B from B using System;Â
class GFG{         // Function to count the // number of steps in a // tetrahedron static int countPaths(int n) {          // initially coming     // to B is B->B     int zB = 1; Â
    // cannot reach A, D or C     int zADC = 0; Â
    // iterate for all steps     for (int i = 1; i <= n; i++)     { Â
        // recurrence relation         int nzB = zADC * 3; Â
        int nzADC = (zADC * 2 + zB); Â
        // memoize previous values         zB = nzB;         zADC = nzADC;     } Â
    // returns steps     return zB; } Â
    // Driver Code     static public void Main ()    {        int n = 3;         Console.WriteLine(countPaths(n));     } } Â
// This code is contributed by Sach |
PHP
<?php// PHP program count total number // of paths to reach B from B Â
// Function to count the number // of steps in a tetrahedron function countPaths($n) {     // initially coming to B is B->B     $zB = 1; Â
    // cannot reach A, D or C     $zADC = 0; Â
    // iterate for all steps     for ($i = 1; $i <= $n; $i++)    { Â
        // recurrence relation         $nzB = $zADC * 3; Â
        $nzADC = ($zADC * 2 + $zB); Â
        // memoize previous values         $zB = $nzB;         $zADC = $nzADC;     } Â
    // returns steps     return $zB; } Â
// Driver Code $n = 3; echo countPaths($n); Â
Â
// This code is contributed // by Sachin?> |
Javascript
<script>Â
// Javascript program count total // number of paths to reach // B from B Â
// Function to count the // number of steps in a // tetrahedron function countPaths(n) {          // Initially coming     // to B is B->B     let zB = 1; Â
    // Cannot reach A, D or C     let zADC = 0; Â
    // Iterate for all steps     for(let i = 1; i <= n; i++)     {                  // recurrence relation         let nzB = zADC * 3; Â
        let nzADC = (zADC * 2 + zB); Â
        // Memoize previous values         zB = nzB;         zADC = nzADC;     } Â
    // Returns steps     return zB; }Â
// Driver codelet n = 3; document.write(countPaths(n)); Â
// This code is contributed by mukesh07Â Â Â Â Â Â Â
</script> |
6
Â
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


