forked from Anjalijha12345/Java-Problem-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_Colouring_Problem.java
116 lines (103 loc) · 3.36 KB
/
N_Colouring_Problem.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Java program for solution of
N Coloring problem using backtracking */
public class nColoringProblem {
final int V = 4;
int color[];
/* A utility function to check
if the current color assignment
is safe for vertex v */
boolean isSafe(int v, int graph[][], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] == 1 && c == color[i])
return false;
return true;
}
/* A recursive utility function
to solve m coloring problem */
boolean graphColoringUtil(int graph[][], int m,
int color[], int v)
{
/* base case: If all vertices are
assigned a color then return true */
if (v == V)
return true;
/* Consider this vertex v and try
different colors */
for (int c = 1; c <= m; c++) {
/* Check if assignment of color c to v
is fine*/
if (isSafe(v, graph, color, c)) {
color[v] = c;
/* recur to assign colors to rest
of the vertices */
if (graphColoringUtil(graph, m, color,
v + 1))
return true;
/* If assigning color c doesn't lead
to a solution then remove it */
color[v] = 0;
}
}
/* If no color can be assigned to
this vertex then return false */
return false;
}
/* This function solves the m Coloring problem using
Backtracking. It mainly uses graphColoringUtil()
to solve the problem. It returns false if the m
colors cannot be assigned, otherwise return true
and prints assignments of colors to all vertices.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
boolean graphColoring(int graph[][], int m)
{
// Initialize all color values as 0. This
// initialization is needed correct
// functioning of isSafe()
color = new int[V];
for (int i = 0; i < V; i++)
color[i] = 0;
// Call graphColoringUtil() for vertex 0
if (!graphColoringUtil(graph, m, color, 0)) {
System.out.println("Solution does not exist");
return false;
}
// Print the solution
printSolution(color);
return true;
}
/* A utility function to print solution */
void printSolution(int color[])
{
System.out.println("Solution Exists: Following"
+ " are the assigned colors");
for (int i = 0; i < V; i++)
System.out.print(" " + color[i] + " ");
System.out.println();
}
// Driver code
public static void main(String args[])
{
mColoringProblem Coloring = new mColoringProblem();
/* Create following graph and
test whether it is
3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
int graph[][] = {
{ 0, 1, 1, 1 },
{ 1, 0, 1, 0 },
{ 1, 1, 0, 1 },
{ 1, 0, 1, 0 },
};
int m = 3; // Number of colors
// Function call
Coloring.graphColoring(graph, m);
}
}