-
Notifications
You must be signed in to change notification settings - Fork 0
/
colouring.go
351 lines (320 loc) · 9.86 KB
/
colouring.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package graph
import (
"container/heap"
"container/list"
)
//GreedyColor greedily colours the graph G colouring the vertices in the given order. That is, this reads the vertices in the given order and assigns to each vertex the minimum colour such that none of its neighbour have this colour.
func GreedyColor(g Graph, order []int) (int, []int) {
n := g.N()
if n != len(order) {
panic("order does not have length equal to g.N()")
}
c := make([]int, n)
for i := range c {
c[i] = -1
}
seenColours := make([]bool, n)
maxColour := -1
max := 0
for _, v := range order {
max = 0
for _, u := range g.Neighbours(v) {
if c[u] > -1 {
seenColours[c[u]] = true
if c[u] > max {
max = c[u]
}
}
}
i := 0
for i = 0; i < n; i++ {
if seenColours[i] == false {
c[v] = i
if i > maxColour {
maxColour = i
}
break
}
seenColours[i] = false
}
for ; i <= max; i++ {
seenColours[i] = false
}
}
return maxColour, c
}
//uncolouredVertex is a type used in the partialDsatur to store the information about an uncolouredVertex.
type uncolouredVertex struct {
v int
numberOfSeenColours int
seenColours []bool
degree int
}
//uncolouredVertices is a []uncolouredVertex.
type uncolouredVertices []uncolouredVertex
func (uv uncolouredVertices) Len() int { return len(uv) }
func (uv uncolouredVertices) Less(i, j int) bool {
//Order the vertices in descending numberOfSeenColours and descending degree if they have the same numberOfSeenColours.
if uv[i].numberOfSeenColours != uv[j].numberOfSeenColours {
return uv[i].numberOfSeenColours > uv[j].numberOfSeenColours
}
return uv[i].degree > uv[j].degree
}
func (uv uncolouredVertices) Swap(i, j int) {
uv[i], uv[j] = uv[j], uv[i]
}
func (uv *uncolouredVertices) Push(x interface{}) {
v := x.(uncolouredVertex)
*uv = append(*uv, v)
}
func (uv *uncolouredVertices) Pop() interface{} {
old := *uv
n := len(old)
v := old[n-1]
*uv = old[0 : n-1]
return v
}
//partialDsatur colours the graph g according to the DSATUR algorithm respecting the partialColouring and returns the colouring, the maximum colour used and the first vertex the algorithm colours and the colours this vertex can't take .
//partialColouring should be a []int of length g.N such that if v has colour k (which must be >= 0) partialColouring[v] = k and partialColouring[v] = -1 is v is uncoloured.
func partialDsatur(g Graph, partialColouring []int, ub int, passThreshold int) (colouring []int, maxColourUsed int, firstUncolouredVertex int, coloursSeen []bool) {
n := g.N()
colouring = make([]int, n)
maxColourUsed = -1
firstUncolouredVertex = -1
precolouredVertices := make([]int, 0, n)
degrees := g.Degrees()
uv := make(uncolouredVertices, 0, n)
var tmp []bool
for v, c := range partialColouring {
if c == -1 {
tmp = make([]bool, 0, ub+1)
uv = append(uv, uncolouredVertex{v, 0, tmp, degrees[v]})
} else {
if c > maxColourUsed {
maxColourUsed = c
}
colouring[v] = c
precolouredVertices = append(precolouredVertices, v)
}
}
for i := 0; i < len(uv); i++ {
v := uv[i].v
seenColours := make([]bool, ub+1)
numberOfSeenColours := 0
for _, u := range precolouredVertices {
if g.IsEdge(u, v) && !seenColours[colouring[u]] {
seenColours[colouring[u]] = true
numberOfSeenColours++
}
}
uv[i].numberOfSeenColours = numberOfSeenColours
uv[i].seenColours = seenColours
}
heap.Init(&uv)
for len(uv) > 0 {
vertex := uv[0]
//fmt.Println(vertex.numberOfSeenColours)
indexToRemove := 0
if maxColourUsed+1-vertex.numberOfSeenColours < passThreshold {
//PASS
//fmt.Println("h",maxColourUsed-vertex.numberOfSeenColours)
t := 0
for i := 0; i < len(uv); i++ {
if uv[i].numberOfSeenColours != vertex.numberOfSeenColours {
break
} else {
t++
}
}
T := uv[:t]
passValueToRemove := 0
for i := 0; i < len(T); i++ {
passValue := 0
for j := 0; j < len(T); j++ {
if T[i].v == T[j].v || g.IsEdge(T[i].v, T[j].v) {
continue
}
for k := 0; k < len(T[i].seenColours); k++ {
if T[i].seenColours[k] == false && T[j].seenColours[k] == false {
passValue++
}
}
}
if passValueToRemove < passValue {
passValueToRemove = passValue
indexToRemove = i
}
}
}
vertex = heap.Remove(&uv, indexToRemove).(uncolouredVertex)
v := vertex.v
if firstUncolouredVertex == -1 {
firstUncolouredVertex = v
coloursSeen = vertex.seenColours
}
toColour := -1
for j, b := range vertex.seenColours {
if !b {
toColour = j
break
}
}
if toColour == -1 || toColour == ub {
//Dsatur is using at least the ub number of colours so the upper bound can't be improved and there is no point continuing.
return colouring, ub + 1, firstUncolouredVertex, coloursSeen
} else if toColour > maxColourUsed {
maxColourUsed = toColour
}
colouring[v] = toColour
//Update the seen colours.
for i := 0; i < len(uv); i++ {
u := uv[i].v
if g.IsEdge(u, v) && !uv[i].seenColours[toColour] {
uv[i].seenColours[toColour] = true
uv[i].numberOfSeenColours++
}
heap.Fix(&uv, i)
}
}
return colouring, maxColourUsed, firstUncolouredVertex, coloursSeen
}
//ChromaticNumber returns the minimum number of colours needed in a proper vertex colouring of g (known as the Chromatic Number χ) and a colouring that uses this many colours ([0, 1, ..., χ -1]).
//Note that a colouring with the minimum number of colours is not necessarily unique and the colouring returned here is arbitrary.
func ChromaticNumber(g Graph) (chromaticNumber int, colouring []int) {
n := g.N()
if n == 0 {
return 0, []int{}
}
type partialColouringType struct {
pc []int
highestColour int
}
partialColourings := list.New()
pc := make([]int, n)
for i := range pc {
pc[i] = -1
}
partialColourings.PushFront(partialColouringType{pc, -1})
ub := n //Upper bound on the largest coloured used. There is certainly a colouring with n colours (so an upper bound of n - 1) but we don't have one so we set the upper bound as 1 higher.
lb := CliqueNumber(g) - 1 //This is a lower bound on the largest coloured used.
var bestColouring []int
iterations := 0
// fmt.Printf("Lower Bound: %d ** Upper Bound: %d ** Iterations: %d \n", lb+1, ub+1, iterations)
for partialColourings.Len() > 0 {
iterations++
//Use the first colouring in the list.
partialColouring := partialColourings.Remove(partialColourings.Front()).(partialColouringType)
if partialColouring.highestColour < ub {
var c []int
var maxColourUsed int
var branchPoint int
var seenColours []bool
if iterations > 1000 {
c, maxColourUsed, branchPoint, seenColours = partialDsatur(g, partialColouring.pc, ub, 4)
} else {
c, maxColourUsed, branchPoint, seenColours = partialDsatur(g, partialColouring.pc, ub, 0)
}
if maxColourUsed < ub {
ub = maxColourUsed
bestColouring = c
// fmt.Printf("Lower Bound: %d ** Upper Bound: %d ** Iterations: %d \n", lb+1, ub+1, iterations)
if ub == lb {
return ub + 1, c
}
}
m := partialColouring.highestColour + 1 //The largest colour the branch vertex is allowed to take.
if m == ub {
//Another colouring using ub colours is not helpful so force the number of colours used to be less.
m = ub - 1
}
seenColours = seenColours[0 : m+1] //Restrict to m colours.
for j, b := range seenColours {
if !b {
tmp := make([]int, n)
copy(tmp, partialColouring.pc)
tmp[branchPoint] = j
if j == m {
partialColourings.PushBack(partialColouringType{tmp, m})
if t := partialColourings.Front().Value.(partialColouringType).highestColour; t > lb {
lb = t
}
} else {
partialColourings.PushFront(partialColouringType{tmp, partialColouring.highestColour})
}
}
}
}
}
// fmt.Printf("Lower Bound: %d ** Upper Bound: %d ** Iterations: %d \n", lb+1, ub+1, iterations)
return ub + 1, bestColouring
}
//ChromaticIndex returns the minimum number of colours needed in a proper edge colouring of g (known as the Chromatic Index χ') and a colouring that uses this many colours ([1, ..., χ']).
//The colouring is returned in the form of an edge array with 0 for non-edges and a colour in [1, 2,..., χ'] for the edges.
//To stop the calculation send true to the channel stop.
//Note that a colouring with the minimum number of colours is not necessarily unique and the colouring returned here is arbitrary.
func ChromaticIndex(g Graph) (chromaticIndex int, colouredEdges []byte) {
h := LineGraphDense(g)
ci, colouring := ChromaticNumber(h)
if ci == -1 {
return -1, nil
}
n := 0
colouringIndex := 0
colouredEdges = make([]byte, n*(n-1)/2)
index := 0
for j := 1; j < n; j++ {
for i := 0; i < j; i++ {
if g.IsEdge(i, j) {
colouredEdges[i] = byte(colouring[colouringIndex] + 1)
colouringIndex++
}
index++
}
}
return ci, colouredEdges
}
//ChromaticPolynomial returns the coefficients of the chromatic polynomial.
//This is a very basic implementation.
func ChromaticPolynomial(g EditableGraph) []int {
n := g.N()
poly := make([]int, n+1)
type holder struct {
g EditableGraph
sign int
}
toCheck := make([]holder, 1)
toCheck[0] = holder{g, 1}
var hold holder
for len(toCheck) > 0 {
hold, toCheck = toCheck[len(toCheck)-1], toCheck[:len(toCheck)-1]
h := hold.g
//Check if we know the chromatic polynomial of this graph.
if h.M() == 0 {
poly[h.N()] += hold.sign
continue
}
//Choose an edge.
var i int
var j int
edgeLoop:
for i = 0; i < h.N(); i++ {
for j = 0; j < i; j++ {
if h.IsEdge(i, j) {
break edgeLoop
}
}
}
//Contract and delete the edge.
tmp := h.Copy()
tmp.RemoveEdge(i, j)
toCheck = append(toCheck, holder{tmp, hold.sign})
tmp = h.Copy()
neighbours := h.Neighbours(j)
for _, v := range neighbours {
tmp.AddEdge(i, v)
}
tmp.RemoveVertex(j)
toCheck = append(toCheck, holder{tmp, -hold.sign})
}
return poly
}