# Number of loops of size k starting from a specific node

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.

Examples:

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.

On expanding,

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:

## C++

`// C++ Program to find number of cycles of length ` `// k in a graph with n nodes. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the Number of ways from a ` `// node to make a loop of size K in undirected ` `// complete connected graph of N nodes ` `int` `numOfways(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `p = 1; ` ` ` ` ` `if` `(k % 2) ` ` ` `p = -1; ` ` ` ` ` `return` `(` `pow` `(n - 1, k) + p * (n - 1)) / n; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 2; ` ` ` `cout << numOfways(n, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find number of ` `// cycles of length k in a graph ` `// with n nodes. ` `public` `class` `GFG { ` ` ` ` ` `// Return the Number of ways ` ` ` `// from a node to make a loop ` ` ` `// of size K in undirected ` ` ` `// complete connected graph of ` ` ` `// N nodes ` ` ` `static` `int` `numOfways(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `p = ` `1` `; ` ` ` ` ` `if` `(k % ` `2` `!= ` `0` `) ` ` ` `p = -` `1` `; ` ` ` ` ` `return` `(` `int` `)(Math.pow(n - ` `1` `, k) ` ` ` `+ p * (n - ` `1` `)) / n; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `, k = ` `2` `; ` ` ` ` ` `System.out.println(numOfways(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## Python3

`# python Program to find number of ` `# cycles of length k in a graph ` `# with n nodes. ` ` ` `# Return the Number of ways from a ` `# node to make a loop of size K in ` `# undirected complete connected ` `# graph of N nodes ` `def` `numOfways(n,k): ` ` ` ` ` `p ` `=` `1` ` ` ` ` `if` `(k ` `%` `2` `): ` ` ` `p ` `=` `-` `1` ` ` ` ` `return` `(` `pow` `(n ` `-` `1` `, k) ` `+` ` ` `p ` `*` `(n ` `-` `1` `)) ` `/` `n ` ` ` `# Driver code ` `n ` `=` `4` `k ` `=` `2` `print` `(numOfways(n, k)) ` ` ` `# This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find number of cycles ` `// of length k in a graph with n nodes. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the Number of ways from ` ` ` `// a node to make a loop of size ` ` ` `// K in undirected complete ` ` ` `// connected graph of N nodes ` ` ` `static` `int` `numOfways(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `p = 1; ` ` ` ` ` `if` `(k % 2 != 0) ` ` ` `p = -1; ` ` ` ` ` `return` `(` `int` `)(Math.Pow(n - 1, k) ` ` ` `+ p * (n - 1)) / n; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4, k = 2; ` ` ` ` ` `Console.Write( numOfways(n, k) ); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find number ` `// of cycles of length ` `// k in a graph with n nodes. ` ` ` `// Return the Number of ways from a ` `// node to make a loop of size K ` `// in undirected complete connected ` `// graph of N nodes ` `function` `numOfways( ` `$n` `, ` `$k` `) ` `{ ` `$p` `= 1; ` ` ` `if` `(` `$k` `% 2) ` ` ` `$p` `= -1; ` ` ` `return` `(pow(` `$n` `- 1, ` `$k` `) + ` ` ` `$p` `* (` `$n` `- 1)) / ` `$n` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 4; ` ` ` `$k` `= 2; ` ` ` `echo` `numOfways(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Number of unique permutations starting with 1 of a Binary String
- Minimum cost path from source node to destination node via an intermediate node
- Number of arrays of size N whose elements are positive integers and sum is K
- Number of ways to select a node from each connected component
- Maximum number of nodes which can be reached from each node in a graph.
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Level of Each node in a Tree from source node (using BFS)
- Print the lexicographically smallest DFS of the graph starting from 1
- Print the lexicographically smallest BFS of the graph starting from 1
- Make all combinations of size k
- Print all subsets of given size of a set
- XOR of all the nodes in the sub-tree of the given node
- Python program to get all subsets of given size of a set
- Find the node whose sum with X has maximum set bits

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.