TheAlgorithms/Go · error

coloring: same colors of neighbouring vertex

Error message

coloring: same colors of neighbouring vertex

What it means

ValidateColorsOfVertex found two adjacent vertices assigned the same Color, violating proper coloring. This means the graph is not bipartite (when called from BipartiteCheck) or the supplied coloring is invalid.

Source

Thrown at graph/coloring/graph.go:52

func (g *Graph) AddEdge(one, two int) {
	// Add vertices: one and two to the graph if they are not present
	g.AddVertex(one)
	g.AddVertex(two)

	// and finally add the edges: one->two and two->one for undirected graph
	g.edges[one][two] = struct{}{}
	g.edges[two][one] = struct{}{}
}

func (g *Graph) ValidateColorsOfVertex(colors map[int]Color) error {
	if g.vertices != len(colors) {
		return errors.New("coloring: not all vertices of graph are colored")
	}
	// check colors
	for vertex, neighbours := range g.edges {
		for nb := range neighbours {
			if colors[vertex] == colors[nb] {
				return errors.New("coloring: same colors of neighbouring vertex")
			}
		}
	}
	return nil
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Recolor using a proper 2-coloring (BFS/DFS alternating colors) before validating
  2. Treat the error as the answer to BipartiteCheck: the graph is not bipartite
  3. Fix the coloring assignment so neighbours always differ
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at graph/coloring/graph.go:52 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/083bdcdf1d64417c. Report an issue: GitHub.