-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamiltonianCycle.java
102 lines (80 loc) · 2.79 KB
/
HamiltonianCycle.java
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
package Backtracking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// data structure to store graph edges
class Edge {
int source, dest;
public Edge(int source, int dest) {
this.source = source;
this.dest = dest;
}
};
// class to represent a graph object
class Graph {
// An array of Lists to represent adjacency list
List<List<Integer>> adjList = null;
// Constructor
Graph(List<Edge> edges, int N) {
adjList = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
adjList.add(i, new ArrayList<>());
}
// add edges to the undirected graph
for (int i = 0; i < edges.size(); i++) {
int src = edges.get(i).source;
int dest = edges.get(i).dest;
adjList.get(src).add(dest);
adjList.get(dest).add(src);
}
}
}
class HamiltonianCycle {
public static void printAllHamiltonianPaths(Graph g, int v, boolean[] visited, List<Integer> path, int N) {
// if all the vertices are visited, then
// hamiltonian path exists
if (path.size() == N) {
// print hamiltonian path
for (int i : path)
System.out.print(i + " ");
System.out.println();
return;
}
// Check if every edge starting from vertex v leads
// to a solution or not
for (int w : g.adjList.get(v)) {
// process only unvisited vertices as hamiltonian
// path visits each vertex exactly once
if (!visited[w]) {
visited[w] = true;
path.add(w);
// check if adding vertex w to the path leads
// to solution or not
printAllHamiltonianPaths(g, w, visited, path, N);
// Backtrack
visited[w] = false;
path.remove(path.size()-1);
}
}
}
public static void main(String[] args) {
// vector of graph edges as per above diagram
List<Edge> edges = Arrays.asList(
new Edge(0, 1), new Edge(0, 2), new Edge(0, 3),
new Edge(1, 2), new Edge(1, 3), new Edge(2, 3)
);
// Set number of vertices in the graph
final int N = 4;
// create a graph from edges
Graph g = new Graph(edges, N);
// starting node
int start = 0;
// add starting node to the path
List<Integer> path = new ArrayList<>();
path.add(start);
// mark start node as visited
boolean[] visited = new boolean[N];
visited[start] = true;
printAllHamiltonianPaths(g, start, visited, path, N);
}
}