forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacktracking.go
50 lines (45 loc) · 1.37 KB
/
backtracking.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
// This file contains the graph coloring implementation using backtracking
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
// ColorUsingBacktracking will return the Color of each vertex and the
// total number of different colors used, using backtracking
func (g *Graph) ColorUsingBacktracking() (map[int]Color, int) {
vertexColors := make(map[int]Color, g.vertices)
g.colorVertex(0, vertexColors)
colorsUsed := 0
for _, cr := range vertexColors {
if colorsUsed < int(cr) {
colorsUsed = int(cr)
}
}
return vertexColors, colorsUsed
}
// colorVertex will try to color provided vertex, v
func (g *Graph) colorVertex(v int, color map[int]Color) bool {
// If all vertices are colored, the colors store will be completely filled.
if len(color) == g.vertices {
return true
}
// As the upper bound of no. of colors is the no. of vertices in graph,
// try assigning each color to the vertex v
for cr := Color(1); cr <= Color(g.vertices); cr++ {
// Use the color, cr for vertex, v if it is safe to use, by
// checking its neighbours
safe := true
for nb := range g.edges[v] {
// cr, color is not safe if color of nb, crnb is not equal to cr
if crnb, ok := color[nb]; ok && crnb == cr {
safe = false
break
}
}
if safe {
color[v] = cr
if g.colorVertex(v+1, color) {
return true
}
delete(color, v)
}
}
return false
}