Backtracking

// 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
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.