Given two positive integer n, k. Consider an undirected complete connected graph of n nodes in a complete connected graph. The task is to calculate the number of ways in which one can start from any node and return to it by visiting K nodes.
Input : n = 3, k = 3 Output : 2 Input : n = 4, k = 2 Output : 3
Lets f(n, k) be a function which return number of ways in which one can start from any node and return to it by visiting K nodes.
If we start and end from one node, then we have K – 1 choices to make for the intermediate nodes since we have already chosen one node in the beginning. For each intermediate choice, you have n – 1 options. So, this will yield (n – 1)k – 1 but then we have to remove all the choices cause smaller loops, so we subtract f(n, k – 1).
So, recurrence relation becomes,
f(n, k) = (n – 1)k – 1 – f(n, k – 1) with base case f(n, 2) = n – 1.
f(n, k) = (n – 1)k – 1 – (n – 1)k – 2 + (n – 1)k – 3 ….. (n – 1) (say eqn 1)
Dividing f(n, k) by (n – 1),
f(n, k)/(n – 1) = (n – 1)k – 2 – (n – 1)k – 3 + (n – 1)k – 4 ….. 1 (say eqn 2)
On adding eqn 1 and eqn 2,
f(n, k) + f(n, k)/(n – 1) = (n – 1)k – 1 + (-1)k
f(n, k) * ( (n -1) + 1 )/(n – 1) = (n – 1)k – 1 + (-1)k
Below is the implementation of this approach:
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.
- Number of unique permutations starting with 1 of a Binary String
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Minimum number of reversals to reach node 0 from every other node
- Minimum cost path from source node to destination node via an intermediate node
- Total number of Subsets of size at most K
- Number of arrays of size N whose elements are positive integers and sum is K
- Find number of ways to form sets from N distinct things with no set of size A or B
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Maximum number of nodes which can be reached from each node in a graph.
- Number of ways to select a node from each connected component
- Print the lexicographically smallest DFS of the graph starting from 1
- Print the lexicographically smallest BFS of the graph starting from 1
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Check whether the cost of going from any node to any other node via all possible paths is same
- Level of Each node in a Tree from source node (using BFS)
- Print all subsets of given size of a set
- Make all combinations of size k
- Python program to get all subsets of given size of a set
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.