-
Notifications
You must be signed in to change notification settings - Fork 1
/
Greedy.java
143 lines (116 loc) · 4.88 KB
/
Greedy.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.*;
import java.util.LinkedList;
@SuppressWarnings("unchecked")
public class Greedy {
private LinkedList[] adjList; // The adjacency list is taken as a parameter of the constructor.
private int[] vertexPermutation; // The permutation is not set with the constructor, making the same object
// viable for computing a coloring with different permutations.
private int n; //number of vertices
public ArrayList<LinkedList<Integer>> S; // A set of color sets: my choice of format for representing a graph coloring.
public int chromaticNumber; // S and chromaticNumber will be considered output, accessible as public fields of the Greedy object.
public Greedy(LinkedList[] adjList) // constructor
{
this.adjList = adjList;
this.n = adjList.length;
S = new ArrayList<LinkedList<Integer>>();
}
public void search() // This method initializes the computation of a feasible coloring.
{ // A method call with no parameter will make the algorithm process the vertices in ascending order
S.clear();
int[] e = new int[n];
for (int i = 0; i < n; i++)
e[i] = i;
this.vertexPermutation = e;
greedyAlgorithm();
}
public void search(int[] inputPermutation) // Overloaded search method which allows us to input a specific ordering of vertices.
{
S.clear();
this.vertexPermutation = inputPermutation;
greedyAlgorithm();
}
public int[] toArray() // A method that returns a permutation of the vertices such that
{ // the previously colored vertices are clustered together by color.
int[] colorArray = new int[n];
int j = 0; // With this permutation we try to obtain better output from the Greedy algorithm.
for (LinkedList<Integer> cClass : S) {
for (Integer v : cClass) {
colorArray[j] = v;
j++;
}
}
return colorArray;
}
public void toArray(int[] array) // A void method, just the same as the one above. This method will change an array of sufficent size.
{
int j = 0;
for (LinkedList<Integer> cClass : S) {
for (Integer v : cClass) {
array[j] = v;
j++;
}
}
}
private void greedyAlgorithm()
{
for (int i = 0; i < n; i++)
{
int j = 0;
while ( j < S.size() )
{
if ( safeToAdd(vertexPermutation[i], S.get(j)) )
{
S.get(j).add((Integer) vertexPermutation[i]);
break;
} else
j++;
}
if (j >= S.size())
{
LinkedList<Integer> colorClass = new LinkedList();
colorClass.add((Integer) vertexPermutation[i]);
S.add(colorClass);
}
}
this.chromaticNumber = S.size();
}
public boolean safeToAdd (int vertex, LinkedList<Integer> colorClassJ)
{
for (Integer i : colorClassJ) {
if ( adjList[vertex].contains(i) )
return false;
}
return true;
}
}
/*
ArrayList<LinkedList<Integer>> greedyColoring = new ArrayList();
greedyColoring = greedyAlgorithm(greedyColoring, testCase, adjList);
int i = 1;
for (LinkedList<Integer> a : greedyColoring) {
System.out.println("\nColor "+i+": ");
for (Integer v : a) {
System.out.print(v+" ");
}
i++;
}
System.out.println("\nFirst iteration greedy number is: "+greedyColoring.size());
int[] newPerm = new int[n];
int j = 0;
for (LinkedList<Integer> cClass : greedyColoring) {
for (Integer v : cClass) {
newPerm[j] = v;
j++;
}
}
greedyColoring = greedyAlgorithm(new ArrayList(), newPerm, adjList );
int k = 1;
for (LinkedList<Integer> a : greedyColoring) {
System.out.println("\nColor "+i+": ");
for (Integer v : a) {
System.out.print(v+" ");
}
k++;
}
System.out.println("Second iteration gives: "+greedyColoring.size());
}*/