-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColoringGraph.java
More file actions
executable file
·65 lines (62 loc) · 2 KB
/
ColoringGraph.java
File metadata and controls
executable file
·65 lines (62 loc) · 2 KB
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
import java.util.*;
import java.io.*;
public class ColoringGraph{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int numvertices=Integer.parseInt(scan.nextLine());
int [][] graph=new int[numvertices][numvertices];
int [] color=new int[numvertices];
int total;
for(int i=0;i<numvertices;i++){
graph[i][i]=1;
String s[]=scan.nextLine().split(" ");
for(int j=0;j<s.length;j++){
int a=Integer.parseInt(s[j]);
graph[i][a]=1;
}
}
for(total=2;total<=numvertices;total++){
color[0]=1;
if(graphColor(graph,color,total,numvertices,0)){
System.out.println(total);
break;
}
}
}
public static boolean graphColor(int graph[][],int color[],int m,int numvertices,int k){
if(k==color.length){
return true;
}
if(color[k]!=0){
return graphColor(graph,color,m,numvertices,k+1);
}
for(int c=1;c<=m;c++){
boolean check=true;
for(int i=0;i<numvertices;i++){
if(graph[k][i]!=1 && color[i]!=c){
check=false;
}
}
if(is_safe(k,c,graph,color,numvertices)){
color[k]=c;
if(graphColor(graph,color,m,numvertices,k+1)) return true;
color[k]=0;
}
}
return false;
}
public static boolean is_safe(int k,int c,int graph[][],int color[],int numvertices){
for(int i=0;i<numvertices;i++){
if(graph[k][i]==1 && c==color[i]) return false;
}
return true;
}
public static void toString(int[][]array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}