-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathGraphColoring.java
86 lines (77 loc) · 2.31 KB
/
GraphColoring.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
import java.util.*;
public class GraphColoring {
static int colorsCount;
static int verticesCount;
static int arr[][];
static int solnCount=0;
static int colorNumber[];
static char[] colorNames = new char[] {'R','G','B','Y','O','I'};
public static void main(String args[]) {
System.out.println("\n******Graph Coloring Problem*****");
Scanner sc = new Scanner(System.in);
System.out.printf("Enter no of vertices: ");
verticesCount=sc.nextInt();
arr= new int[verticesCount][verticesCount];
System.out.println("Enter adjacency matrix");
for(int i=0;i<verticesCount;i++) {
for(int j=0;j<verticesCount;j++) {
arr[i][j]=sc.nextInt();
}
}
colorNumber=new int[verticesCount];
System.out.printf("1) Provide the number of colors available.\n2) Color graph using minimum no. of colors\nEnter your choice: ");
int choice = sc.nextInt();
if(choice==1){
System.out.printf("Enter no of colors available: ");
colorsCount=sc.nextInt();
}
else if(choice==2)
colorsCount = findMinimumChromaticNumber();
System.out.println("Minimum count of colors required are: "+colorsCount);
graphColoringSolver(0);
System.out.println("No. of solutions is "+solnCount);
}
public static int findMinimumChromaticNumber() {
int assign [] = new int[verticesCount];
Arrays.fill(assign, -1);
for (int i = 0; i < verticesCount; i++) {
assign[i] = 1;
for(int j = 0;j<verticesCount; j++) {
if (arr[i][j] == 1 && assign[i] == assign[j]) {
assign[i]++;
}
}
}
int max = Integer.MIN_VALUE;
for(int i =0;i<assign.length;i++) {
if(assign[i]>max)
max = assign[i];
}
return max;
}
public static boolean canColorVertex(int v,int c) {
for (int i = 0; i < verticesCount; i++)
if (arr[v][i] == 1 && c == colorNumber[i])
return false;
return true;
}
public static void graphColoringSolver(int v){
if (v == verticesCount)
{
for (v = 0; v < verticesCount; v++)
System.out.printf(colorNames[colorNumber[v]-1]+" ");
System.out.println();
solnCount++;
return;
}
for (int c = 1; c <= colorsCount; c++)
{
if (canColorVertex( v, c))
{
colorNumber[v] = c;
graphColoringSolver(v + 1);
colorNumber[v] = 0;
}
}
}
}