forked from striver79/StriversGraphSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcoloringgraphJava
28 lines (27 loc) · 870 Bytes
/
mcoloringgraphJava
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
class solve
{
private static boolean isSafe(int node, List<Integer>[] G, int[] color, int n, int col) {
for(int it: G[node]) {
if(color[it] == col) return false;
}
return true;
}
private static boolean solve(int node, List<Integer>[] G, int[] color, int n, int m) {
if(node == n) return true;
for(int i = 1;i<=m;i++) {
if(isSafe(node, G, color, n, i)) {
color[node] = i;
if(solve(node+1, G, color, n, m) == true) return true;
color[node] = 0;
}
}
return false;
}
public static boolean graphColoring(List<Integer>[] G, int[] color, int i, int m)
{
int n = G.length;
if(solve(0, G, color, n, m) == true) return true;
return false;
// Your code here
}
}