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.
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Number of paths with exactly k coins
- Count number of paths with at-most k turns
- Number of palindromic paths in a matrix
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Total number of decreasing paths in a matrix
- Number of paths from source to destination in a directed acyclic graph
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of Paths of Weight W in a K-ary tree
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Paths requiring minimum number of jumps to reach end of array
- Count unique paths is a matrix whose product of elements contains odd number of divisors
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Count all possible paths from top left to bottom right of a mXn matrix
- Paths from entry to exit in matrix and maximum path sum
- Sum of lengths of all paths possible in a given tree
- Counts paths from a point to reach Origin : Memory Optimized
- Count of Fibonacci paths in a Binary tree
- Sum of cost of all paths to reach a given cell in a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.