-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamiltonian-path.js
64 lines (51 loc) · 1.55 KB
/
hamiltonian-path.js
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
class Graph {
constructor(vertices) {
this.vertices = vertices;
this.adjacencyMatrix = Array.from(Array(vertices), () => new Array(vertices).fill(0));
}
addEdge(u, v) {
this.adjacencyMatrix[u][v] = 1;
this.adjacencyMatrix[v][u] = 1;
}
hamiltonianPath() {
let path = [];
let visited = new Array(this.vertices).fill(false);
const isHamiltonian = (vertex, pos) => {
if (pos === this.vertices) return true;
for (let v = 0; v < this.vertices; v++) {
if (this.adjacencyMatrix[vertex][v] === 1 && !visited[v]) {
visited[v] = true;
path[pos] = v;
if (isHamiltonian(v, pos + 1)) {
return true;
}
visited[v] = false;
path[pos] = -1;
}
}
return false;
};
for (let i = 0; i < this.vertices; i++) {
path.fill(-1);
visited.fill(false);
path[0] = i;
visited[i] = true;
if (isHamiltonian(i, 1)) {
return path;
}
}
return [];
}
}
const graph = new Graph(5);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 0);
const hamiltonianPath = graph.hamiltonianPath();
if (hamiltonianPath.length > 0) {
console.log("Hamiltonian Path:", hamiltonianPath.join(" -> "));
} else {
console.log("No Hamiltonian Path found.");
}