func NewColorHeap(src *graph.Node) Heap { h := Heap{Items: make([]Heaper, graph.NumColors-1)} for i := int(graph.Blank + 1); i < graph.NumColors; i++ { h.Items[i-1] = ColorItem{ Color: graph.NodeColor(i), } } for _, adj := range src.Adj { if adj.Color == graph.Blank { for i := graph.Blank + 1; i < graph.NumColors; i++ { priority := h.Items[i-1].(ColorItem) priority.Priority += adj.TakenColors[i] h.Items[i-1] = priority } } } heap.Init(&h) return h }
// Setup for graph coloring backtracking algorithm, // search heuristics are set based on heuristc flag func colorMap(g graph.Graph) bool { // For MRV, we'll use a heap var h nh.Heap if Heuristic.MRV { h = nh.NewNodeHeap(len(g), Heuristic.Degree) for _, n := range g { h.Items = append(h.Items, nh.NewNodeItem(n)) } heap.Init(&h) } // Init colors cache ColorsCache.Colors = make([][]graph.NodeColor, len(g)) for i := range g { ColorsCache.Colors[i] = make([]graph.NodeColor, 0, graph.NumColors-1) for color := graph.Blank + 1; color < graph.NumColors; color++ { ColorsCache.Colors[i] = append(ColorsCache.Colors[i], graph.NodeColor(color)) } } return colorMapBacktrack(g, 0, &h) }