|
| 1 | +package com.thealgorithms.datastructures.graphs; |
| 2 | + |
| 3 | +/** |
| 4 | + * Java program for Hamiltonian Cycle (https://en.wikipedia.org/wiki/Hamiltonian_path) |
| 5 | + * @author Akshay Dubey (https://github.com/itsAkshayDubey) |
| 6 | + */ |
| 7 | +public class HamiltonianCycle { |
| 8 | + |
| 9 | + private int V, pathCount; |
| 10 | + private int[] cycle; |
| 11 | + private int[][] graph; |
| 12 | + |
| 13 | + /** |
| 14 | + * Find hamiltonian cycle for given graph G(V,E) |
| 15 | + * @param graph Adjacency matrix of a graph G(V, E) |
| 16 | + * for which hamiltonian path is to be found |
| 17 | + * @return Array containing hamiltonian cycle |
| 18 | + * else returns 1D array with value -1. |
| 19 | + */ |
| 20 | + public int[] findHamiltonianCycle(int[][] graph){ |
| 21 | + this.V = graph.length; |
| 22 | + this.cycle = new int[this.V+1]; |
| 23 | + |
| 24 | + //Initialize path array with -1 value |
| 25 | + for(int i=0 ; i<this.cycle.length ; i++) { |
| 26 | + this.cycle[i] = -1; |
| 27 | + } |
| 28 | + |
| 29 | + this.graph = graph; |
| 30 | + |
| 31 | + this.cycle[0] = 0; |
| 32 | + this.pathCount = 1; |
| 33 | + if(!isPathFound(0)) { |
| 34 | + for(int i=0 ; i<this.cycle.length ; i++) { |
| 35 | + this.cycle[i] = -1; |
| 36 | + } |
| 37 | + } |
| 38 | + else { |
| 39 | + this.cycle[this.cycle.length-1] = this.cycle[0]; |
| 40 | + } |
| 41 | + |
| 42 | + return cycle; |
| 43 | + } |
| 44 | + |
| 45 | + /** function to find paths recursively |
| 46 | + * Find paths recursively from given vertex |
| 47 | + * @param vertex Vertex from which path is to be found |
| 48 | + * @returns true if path is found false otherwise |
| 49 | + */ |
| 50 | + public boolean isPathFound(int vertex) { |
| 51 | + if (this.graph[vertex][0] == 1 && this.pathCount == this.V) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + /** all vertices selected but last vertex not linked to 0 **/ |
| 56 | + if (this.pathCount == this.V) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + for (int v = 0; v < this.V; v++){ |
| 61 | + /** if connected **/ |
| 62 | + if (this.graph[vertex][v] == 1 ){ |
| 63 | + /** add to path **/ |
| 64 | + this.cycle[this.pathCount++] = v; |
| 65 | + |
| 66 | + /** remove connection **/ |
| 67 | + this.graph[vertex][v] = 0; |
| 68 | + this.graph[v][vertex] = 0; |
| 69 | + |
| 70 | + /** if vertex not already selected solve recursively **/ |
| 71 | + if (!isPresent(v)) { |
| 72 | + return isPathFound(v); |
| 73 | + } |
| 74 | + |
| 75 | + /** restore connection **/ |
| 76 | + this.graph[vertex][v] = 1; |
| 77 | + this.graph[v][vertex] = 1; |
| 78 | + |
| 79 | + /** remove path **/ |
| 80 | + this.cycle[--this.pathCount] = -1; |
| 81 | + } |
| 82 | + } |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + /** function to check if path is already selected |
| 87 | + * Check if path is already selected |
| 88 | + * @param vertex Starting vertex |
| 89 | + */ |
| 90 | + public boolean isPresent(int vertex){ |
| 91 | + |
| 92 | + for (int i = 0; i < pathCount - 1; i++) { |
| 93 | + if (cycle[i] == vertex) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
| 100 | +} |
0 commit comments