import
java.io.*;
import
java.util.Scanner;
public
class
GFG {
private
int
vert, numCol;
private
int
[] colour;
private
int
[][] graph;
public
void
graphcolour(
int
[][] g,
int
c)
{
vert = g.length;
numCol = c;
colour =
new
int
[vert];
graph = g;
try
{
solve(
0
);
System.out.println(
"No solution"
);
}
catch
(Exception e) {
System.out.println(
"\nSolution exists "
);
display();
}
}
public
void
solve(
int
ve)
throws
Exception
{
if
(ve == vert)
throw
new
Exception(
"Solution found"
);
for
(
int
i =
1
; i <= numCol; i++) {
if
(isPossible(ve, i)) {
colour[ve] = i;
solve(ve +
1
);
colour[ve] =
0
;
}
}
}
public
boolean
isPossible(
int
ve,
int
c)
{
for
(
int
i =
0
; i < vert; i++)
if
(graph[ve][i] ==
1
&& c == colour[i])
return
false
;
return
true
;
}
public
void
display()
{
System.out.println(
"\nColours: "
);
for
(
int
i =
0
; i < vert; i++)
System.out.print(colour[i] +
" "
);
System.out.println();
}
public
static
void
main(String[] args)
{
System.out.println(
"Test"
);
GFG gc =
new
GFG();
int
vert =
4
;
int
[][] graph
= { {
0
,
1
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
},
{
1
,
0
,
1
,
0
,
0
,
0
,
1
,
0
,
0
,
0
},
{
0
,
1
,
0
,
1
,
0
,
0
,
0
,
1
,
0
,
0
},
{
0
,
0
,
1
,
0
,
1
,
0
,
0
,
0
,
1
,
0
} };
int
c =
2
;
gc.graphcolour(graph, c);
}
}