1
1
package com .thealgorithms .datastructures .graphs ;
2
2
3
+ import java .util .Arrays ;
4
+
3
5
/**
4
- * Java program for Hamiltonian Cycle
5
- * <a href="https://en.wikipedia.org/wiki/Hamiltonian_path">wikipedia</a>
6
+ * Java program to find a Hamiltonian Cycle in a graph.
7
+ * A Hamiltonian Cycle is a cycle that visits every vertex exactly once
8
+ * and returns to the starting vertex.
9
+ *
10
+ * <p>For more details, see the
11
+ * <a href="https://en.wikipedia.org/wiki/Hamiltonian_path">Wikipedia article</a>.
6
12
*
7
13
* @author <a href="https://github.com/itsAkshayDubey">Akshay Dubey</a>
8
14
*/
@@ -14,30 +20,30 @@ public class HamiltonianCycle {
14
20
private int [][] graph ;
15
21
16
22
/**
17
- * Find hamiltonian cycle for given graph G(V,E)
23
+ * Finds a Hamiltonian Cycle for the given graph.
18
24
*
19
- * @param graph Adjacency matrix of a graph G(V, E)
20
- * for which hamiltonian path is to be found
21
- * @return Array containing hamiltonian cycle
22
- * else returns 1D array with value -1 .
25
+ * @param graph Adjacency matrix representing the graph G(V, E), where V is
26
+ * the set of vertices and E is the set of edges.
27
+ * @return An array representing the Hamiltonian cycle if found, otherwise an
28
+ * array filled with -1 indicating no Hamiltonian cycle exists .
23
29
*/
24
30
public int [] findHamiltonianCycle (int [][] graph ) {
31
+ // Single vertex graph
32
+ if (graph .length == 1 ) {
33
+ return new int [] {0 , 0 };
34
+ }
35
+
25
36
this .vertex = graph .length ;
26
37
this .cycle = new int [this .vertex + 1 ];
27
38
28
- // Initialize path array with -1 value
29
- for (int i = 0 ; i < this .cycle .length ; i ++) {
30
- this .cycle [i ] = -1 ;
31
- }
39
+ // Initialize the cycle array with -1 to represent unvisited vertices
40
+ Arrays .fill (this .cycle , -1 );
32
41
33
42
this .graph = graph ;
34
-
35
43
this .cycle [0 ] = 0 ;
36
44
this .pathCount = 1 ;
37
45
if (!isPathFound (0 )) {
38
- for (int i = 0 ; i < this .cycle .length ; i ++) {
39
- this .cycle [i ] = -1 ;
40
- }
46
+ Arrays .fill (this .cycle , -1 );
41
47
} else {
42
48
this .cycle [this .cycle .length - 1 ] = this .cycle [0 ];
43
49
}
@@ -46,62 +52,55 @@ public int[] findHamiltonianCycle(int[][] graph) {
46
52
}
47
53
48
54
/**
49
- * function to find paths recursively
50
- * Find paths recursively from given vertex
55
+ * Recursively searches for a Hamiltonian cycle from the given vertex.
51
56
*
52
- * @param vertex Vertex from which path is to be found
53
- * @returns true if path is found false otherwise
57
+ * @param vertex The current vertex from which to explore paths.
58
+ * @return {@code true} if a Hamiltonian cycle is found, otherwise {@code false}.
54
59
*/
55
60
public boolean isPathFound (int vertex ) {
56
61
boolean isLastVertexConnectedToStart = this .graph [vertex ][0 ] == 1 && this .pathCount == this .vertex ;
57
62
if (isLastVertexConnectedToStart ) {
58
63
return true ;
59
64
}
60
65
61
- /* all vertices selected but last vertex not linked to 0 **/
66
+ // If all vertices are visited but the last vertex is not connected to the start
62
67
if (this .pathCount == this .vertex ) {
63
68
return false ;
64
69
}
65
70
66
71
for (int v = 0 ; v < this .vertex ; v ++) {
67
- /* if connected **/
68
- if (this .graph [vertex ][v ] == 1 ) {
69
- /* add to path **/
70
- this .cycle [this .pathCount ++] = v ;
71
-
72
- /* remove connection **/
72
+ if (this .graph [vertex ][v ] == 1 ) { // Check if there is an edge
73
+ this .cycle [this .pathCount ++] = v ; // Add the vertex to the cycle
73
74
this .graph [vertex ][v ] = 0 ;
74
75
this .graph [v ][vertex ] = 0 ;
75
76
76
- /* if vertex not already selected solve recursively **/
77
+ // Recursively attempt to complete the cycle
77
78
if (!isPresent (v )) {
78
79
return isPathFound (v );
79
80
}
80
81
81
- /* restore connection **/
82
+ // Restore the edge if the path does not work
82
83
this .graph [vertex ][v ] = 1 ;
83
84
this .graph [v ][vertex ] = 1 ;
84
85
85
- /* remove path **/
86
86
this .cycle [--this .pathCount ] = -1 ;
87
87
}
88
88
}
89
89
return false ;
90
90
}
91
91
92
92
/**
93
- * function to check if path is already selected
94
- * Check if path is already selected
93
+ * Checks if a vertex is already part of the current Hamiltonian path.
95
94
*
96
- * @param vertex Starting vertex
95
+ * @param vertex The vertex to check.
96
+ * @return {@code true} if the vertex is already in the path, otherwise {@code false}.
97
97
*/
98
98
public boolean isPresent (int vertex ) {
99
99
for (int i = 0 ; i < pathCount - 1 ; i ++) {
100
100
if (cycle [i ] == vertex ) {
101
101
return true ;
102
102
}
103
103
}
104
-
105
104
return false ;
106
105
}
107
106
}
0 commit comments