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 tetrahedron ` `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 ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << countPaths(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program count total ` `// number of paths to reach ` `// B from B ` `import` `java.io.*; ` ` ` `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 ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(countPaths(n)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Number of paths with exactly k coins
- Count number of paths with at-most k turns
- Number of palindromic paths in a matrix
- Number of Paths of Weight W in a K-ary tree
- Total number of decreasing paths in a matrix
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of paths from source to destination in a directed acyclic graph
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Maximize the number of segments of length p, q and r
- Number of palindromic subsequences of length k where k <= 3
- Number of decimal numbers of length k, that are strict monotone
- Number of Binary Strings of length N with K adjacent Set Bits
- Unique paths in a Grid with Obstacles
- Count all possible paths from top left to bottom right of a mXn matrix
- Counts paths from a point to reach Origin

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.