Skip to content

Commit 22be348

Browse files
Add algorithm to find Hamiltonian cycle (TheAlgorithms#3151)
1 parent 6472d33 commit 22be348

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class HamiltonianCycleTest {
8+
9+
private HamiltonianCycle hamiltonianCycle = new HamiltonianCycle();
10+
11+
@Test
12+
void testFindHamiltonianCycleShouldReturnHamiltonianCycle() {
13+
int[] expectedArray = {0,1,2,4,3,0};
14+
int[][] inputArray = {
15+
{0, 1, 0, 1, 0},
16+
{1, 0, 1, 1, 1},
17+
{0, 1, 0, 0, 1},
18+
{1, 1, 0, 0, 1},
19+
{0, 1, 1, 1, 0}
20+
};
21+
22+
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
23+
}
24+
25+
@Test
26+
void testFindHamiltonianCycleShouldReturnInfinityArray() {
27+
int[] expectedArray = {-1,-1,-1,-1,-1,-1};
28+
29+
int[][] inputArray = {
30+
{0, 1, 0, 1, 0},
31+
{1, 0, 1, 1, 1},
32+
{0, 1, 0, 0, 1},
33+
{1, 1, 0, 0, 0},
34+
{0, 1, 1, 0, 0}
35+
};
36+
37+
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
38+
}
39+
}

0 commit comments

Comments
 (0)