-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMColoringProblem.java
More file actions
30 lines (25 loc) · 908 Bytes
/
MColoringProblem.java
File metadata and controls
30 lines (25 loc) · 908 Bytes
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
package graph;
public class MColoringProblem {
static boolean isSafe(int v, boolean[][] graph, int[] color, int c, int V) {
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
// A recursive utility function to solve m coloring problem.
static boolean graphColoringUtil(boolean[][] graph, int m, int[] color, int v, int V) {
if (v == V) return true;
for (int c = 1; c <= m; c++) {
if (isSafe(v, graph, color, c, V)) {
color[v] = c;
if (graphColoringUtil(graph, m, color, v + 1, V)) return true;
color[v] = 0;
}
}
return false;
}
public boolean graphColoring(boolean[][] graph, int m, int V) {
int[] color = new int[V];
return graphColoringUtil(graph, m, color, 0, V);
}
}