-
Notifications
You must be signed in to change notification settings - Fork 65
/
hamiltonprob.java
84 lines (63 loc) · 2.21 KB
/
hamiltonprob.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
//username DishaKhanapurkar
class Solution
{
public static int[][] printAdjacency(int n, int m, int[][] edges) {
// n - > number of nodes
// m - > number of edges
// adjacence list of type - for node 0 - the list is 1 ,4 5, 6 and not 0 , 1 , 4 ,5,6
int[][] adjlist = new int[n][];
ArrayList<Integer>[] arraylist = new ArrayList[n];
for (int i = 0; i < n; i++) {
arraylist[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
arraylist[edges[i][0]].add(edges[i][1]);
arraylist[edges[i][1]].add(edges[i][0]);
}
for (int i = 0; i < n; i++) {
int temp[] = new int[arraylist[i].size()];
//temp[0] = i;
arraylist[i].sort(Comparator.naturalOrder());
for (int j = 0; j < arraylist[i].size(); j++) {
temp[j] = arraylist[i].get(j);
}
adjlist[i] = temp;
}
return adjlist;
}
boolean meth(int N, int cur, int[][] adjlist, boolean[] visited, int count) {
visited[cur] = true;
count++;
if (N == count) {
return true;
}
for (int i = 0; i < adjlist[cur].length; i++) {
if (!visited[adjlist[cur][i]]) {
if (meth(N, adjlist[cur][i], adjlist, visited, count)) {
return true;
}
}
}
visited[cur] = false;
return false;
}
boolean check(int N, int M, ArrayList<ArrayList<Integer>> Edges)
{
// code here
int count = 0;
boolean[] visited = new boolean[N + 1];
int[][] edges = new int[Edges.size()][];
for (int i = 0; i < Edges.size(); i++) {
int[] temp = new int[Edges.get(i).size()];
for (int j = 0; j < Edges.get(i).size(); j++) {
temp[j] = Edges.get(i).get(j);
}
edges[i] = temp;
}
int[][] adjlist = printAdjacency(N + 1, M, edges);
for (int i = 1; i <= N; i++) {
if (meth(N , i, adjlist, visited, count)) return true;
}
return false;
}
}