-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColoringGraphs.java
88 lines (75 loc) · 1.7 KB
/
ColoringGraphs.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
import java.util.*;
import java.io.*;
public class ColoringGraphs{
/*
4
1 2
0 2 3
0 1
1
*/
static int[][] adjMatrix;
static int[] color;
/*
public static boolean Coloring(int v , int n){
Set<Integer> neighbours = new HashSet<Integer>();
if(v == n){
return true;
}
for(int i = 0 ; i<n ; i++){
if(adjMatrix[v][i] == 1 && k == color[i]){
return false;
}
}
Coloring(v+1,k,n);
return true;
}
*/
public static boolean Coloring(int v , int k, int n, int[] color){
Set<Integer> neighbours = new HashSet<Integer>();
if(v == n){
return true;
}
for(int i = 0 ; i<n ; i++){
if(adjMatrix[v][i] == 1){
neighbours = neighbours.add(color[i]);
//return false;
}
else{
color[v] = k;
if(Coloring(v+1,k,n,color)){
return true;
}
color[v] = 0;
}
}
return false;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
adjMatrix = new int[n][n];
String[] temp;
color = new int[n];
for(int i = 0 ; i<n ; i++){
color[i] = 0;
}
for(int i = 0 ; i<n ; i++){
temp = br.readLine().split(" ");
for(int j = 0 ; j<temp.length; j++){
adjMatrix[i][Integer.parseInt(temp[j])] = 1;
adjMatrix[Integer.parseInt(temp[j])][i] = 1;
}
}
for(int i = 1 ; i<=n ; i++){
Coloring(0,i,n,color);
if(Coloring(0,i,n,color) == true){
System.out.println(i);
break;
}
else{
System.out.println("impossible");
}
}
}
}