-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapColoring.java
62 lines (53 loc) · 1.82 KB
/
MapColoring.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
import java.util.Arrays;
public class MapColoring {
// Function to check if it is safe to assign color c to vertex v
static boolean isSafe(int v, int[][] graph, int[] color, int c) {
for (int i = 0; i < graph.length; i++) {
if (graph[v][i] == 1 && c == color[i]) {
return false;
}
}
return true;
}
// Recursive function to solve map coloring problem using backtracking
static boolean graphColoringUtil(int[][] graph, int m, int[] color, int v) {
if (v == graph.length)
return true;
for (int c = 1; c <= m; c++) {
if (isSafe(v, graph, color, c)) {
color[v] = c;
if (graphColoringUtil(graph, m, color, v + 1))
return true;
color[v] = 0; // backtrack
}
}
return false;
}
// Main function to solve map coloring problem
static boolean graphColoring(int[][] graph, int m) {
int V = graph.length;
int[] color = new int[V];
Arrays.fill(color, 0);
if (!graphColoringUtil(graph, m, color, 0)) {
System.out.println("Solution does not exist");
return false;
}
// Print the solution
System.out.println("Solution exists and the coloring is:");
for (int i = 0; i < V; i++) {
System.out.print(color[i] + " ");
}
return true;
}
public static void main(String[] args) {
int[][] graph = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 1, 0},
{1, 1, 1, 0, 1},
{0, 1, 0, 1, 0}
};
int m = 3; // Number of colors
graphColoring(graph, m);
}
}