Number of Hamiltonian cycle - GeeksforGeeks

16 Apr, 2021
Given an undirected complete graph of N vertices where N > 2. The task is to find the number of different Hamiltonian cycle of the graph.Complete Graph: A graph is said to be complete if each possible vertices is connected through an Edge.Hamiltonian Cycle: It is a closed walk such that each vertex is visited at most once except the initial vertex. and it is not necessary to visit all the edges.Formula:  
Examples:  
Input : N = 6
Output : Hamiltonian cycles = 60

Input : N = 4
Output : Hamiltonian cycles = 3
 
Explanation: Let us take the example of N = 4 complete undirected graph, The 3 different hamiltonian cycle is as shown below: 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program for implementation of the// above program #include <bits/stdc++.h>using namespace std; // Function that calculates// number of Hamiltonian cycleint Cycles(int N){     int fact = 1, result = 0;     result = N - 1;     // Calculating factorial    int i = result;    while (i > 0) {        fact = fact * i;        i--;    }     return fact / 2;} // Driver codeint main(){    int N = 5;     int Number = Cycles(N);     cout << "Hamiltonian cycles = " << Number;    return 0;}
// Java program for implementation// of the above programclass GFG{     // Function that calculates// number of Hamiltonian cyclestatic int Cycles(int N){    int fact = 1, result = 0;     result = N - 1;     // Calculating factorial    int i = result;    while (i > 0)    {        fact = fact * i;        i--;    }     return fact / 2;} // Driver codepublic static void main(String[] args){    int N = 5;     int Number = Cycles(N);     System.out.println("Hamiltonian cycles = " +                                        Number);}} // This code is contributed// by Code_Mech.
# Python3 program for implementation# of the above programimport math as mt # Function that calculates# number of Hamiltonian cycledef Cycles(N):     fact = 1     result = N - 1     # Calculating factorial    i = result    while (i > 0):        fact = fact * i        i -= 1         return fact // 2 # Driver codeN = 5 Number = Cycles(N) print("Hamiltonian cycles = ",                       Number) # This code is contributed# by Mohit Kumar
// C# program for implementation of// the above programusing System; class GFG{ // Function that calculates// number of Hamiltonian cyclestatic int Cycles(int N){    int fact = 1, result = 0;     result = N - 1;     // Calculating factorial    int i = result;    while (i > 0)    {        fact = fact * i;        i--;    }     return fact / 2;} // Driver codepublic static void Main(){    int N = 5;     int Number = Cycles(N);     Console.Write("Hamiltonian cycles = " +                                   Number);}} // This code is contributed// by Akanksha Rai
<?php// PHP program for implementation// of the above program // Function that calculates// number of Hamiltonian cyclefunction Cycles($N){    $fact = 1 ;    $result = 0;     $result = $N - 1;     // Calculating factorial    $i = $result;    while ($i > 0)    {        $fact = $fact * $i;        $i--;    }     return floor($fact / 2);} // Driver code$N = 5; $Number = Cycles($N); echo "Hamiltonian cycles = ",                     $Number; // This code is contributed by Ryuga?>
<script> // Javascript program for implementation of the// above program // Function that calculates// number of Hamiltonian cyclefunction Cycles(N){     var fact = 1, result = 0;     result = N - 1;     // Calculating factorial    var i = result;    while (i > 0) {        fact = fact * i;        i--;    }     return fact / 2;} // Driver codevar N = 5;var Number = Cycles(N);document.write( "Hamiltonian cycles = " + Number); // This code is contributed by rutvik_56.</script>
Hamiltonian cycles = 12
 
mohit kumar 29
ankthon
Akanksha_Rai
Code_Mech
rutvik_56
school-programming
C++ Programs
Graph
Mathematical
Mathematical
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
Passing a function as a parameter in C++
cin in C++
Generics in C++
CSV file management using C++
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Graph and its representations