forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreedy.go
52 lines (48 loc) · 1.67 KB
/
greedy.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
// This file contains the graph coloring implementation using Greedy Approach.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
import "sort"
// ColorUsingGreedyApproach will return the Color of each vertex and the
// total number of different colors used, using a greedy approach, based on
// the number of edges (or degree) from any vertex.
func (g *Graph) ColorUsingGreedyApproach() (map[int]Color, int) {
degreeOfVertex := make([]struct{ degree, vertex int }, 0, g.vertices)
for v, neighbours := range g.edges {
degreeOfVertex = append(degreeOfVertex,
struct{ degree, vertex int }{
vertex: v,
degree: len(neighbours),
},
)
}
// sort the degreeOfVertex in decreasing order of degrees
sort.Slice(degreeOfVertex, func(i, j int) bool {
return degreeOfVertex[i].degree > degreeOfVertex[j].degree
})
vertexColors := make(map[int]Color, g.vertices)
// Start with a color and assign the color to all possible vertices in the degreeOfVertex slice
// and then, re-iterate with new color for all those which are left
for color := 1; color <= g.vertices; color++ {
vertexLoop:
for _, val := range degreeOfVertex {
// skip, if already assigned
if _, ok := vertexColors[val.vertex]; ok {
continue vertexLoop
}
// Check its neighbours
for ng := range g.edges[val.vertex] {
if vertexColors[ng] == Color(color) {
// not possible to use this color for val.vertex
continue vertexLoop
}
}
// Assign color to the vertex
vertexColors[val.vertex] = Color(color)
}
// continue till all the vertices are colored
if len(vertexColors) == g.vertices {
return vertexColors, color
}
}
return vertexColors, g.vertices
}