-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamiltonianPath.java
More file actions
111 lines (83 loc) · 2.7 KB
/
HamiltonianPath.java
File metadata and controls
111 lines (83 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.*;
public class HamiltonianPath {
private int numVertices;
private int[] path;
private boolean[] visited;
private int[][] graph;
public HamiltonianPath(int[][] graph) {
this.numVertices = graph.length;
this.path = new int[numVertices];
this.visited = new boolean[numVertices];
this.graph = graph;
}
public void findHamiltonianPath() {
path[0] = 0; // Start with the first vertex
if (hamiltonianPathUtil(1)) {
System.out.println("Hamiltonian path exists:");
printPath();
} else {
System.out.println("No Hamiltonian path exists.");
}
}
private boolean hamiltonianPathUtil(int position) {
if (position == numVertices) {
// All vertices have been included in the path
// Check if there is an edge from the last vertex to the first vertex
if (graph[path[position - 1]][path[0]] == 1) {
return true;
}
return false;
}
for (int vertex = 1; vertex < numVertices; vertex++) {
if (isSafe(vertex, position)) {
path[position] = vertex;
visited[vertex] = true;
if (hamiltonianPathUtil(position + 1)) {
return true;
}
// Backtracking
path[position] = -1;
visited[vertex] = false;
}
}
return false;
}
private boolean isSafe(int vertex, int position) {
// Check if the vertex is adjacent to the previously added vertex
if (graph[path[position - 1]][vertex] == 0) {
return false;
}
// Check if the vertex has already been visited
if (visited[vertex]) {
return false;
}
return true;
}
private void printPath() {
for (int vertex : path) {
System.out.print(vertex + " ");
}
System.out.println(path[0]); // Print the first vertex again to complete the cycle
}
public static void main(String[] args) {
// No path
int[][] graph1 = {
{0, 1, 1, 1, 0},
{1, 0, 1, 0, 1},
{1, 1, 0, 1, 0},
{1, 0, 1, 0, 0},
{0, 1, 0, 0, 0}
};
HamiltonianPath hp1 = new HamiltonianPath(graph1);
hp1.findHamiltonianPath();
// Path exists
int[][] graph2 = {
{ 0, 1, 1, 1, 0 },
{ 1, 0, 1, 0, 1 },
{ 1, 1, 0, 1, 1 },
{ 1, 0, 1, 0, 0 }
};
HamiltonianPath hp2 = new HamiltonianPath(graph2);
hp2.findHamiltonianPath();
}
}