diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java new file mode 100644 index 000000000..e0f373bf0 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -0,0 +1,100 @@ +package com.thealgorithms.datastructures.graphs; + +/** + * Java program for Hamiltonian Cycle (https://en.wikipedia.org/wiki/Hamiltonian_path) + * @author Akshay Dubey (https://github.com/itsAkshayDubey) + */ +public class HamiltonianCycle { + + private int V, pathCount; + private int[] cycle; + private int[][] graph; + + /** + * Find hamiltonian cycle for given graph G(V,E) + * @param graph Adjacency matrix of a graph G(V, E) + * for which hamiltonian path is to be found + * @return Array containing hamiltonian cycle + * else returns 1D array with value -1. + */ + public int[] findHamiltonianCycle(int[][] graph){ + this.V = graph.length; + this.cycle = new int[this.V+1]; + + //Initialize path array with -1 value + for(int i=0 ; i